Skip to content

Commit 642997b

Browse files
committed
add tests for auth-datafile
1 parent e0c6ba0 commit 642997b

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

core-httpclient-impl/src/main/java/com/optimizely/ab/config/HttpProjectConfigManager.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,7 @@ static ProjectConfig parseProjectConfig(String datafile) throws ConfigParseExcep
113113

114114
@Override
115115
protected ProjectConfig poll() {
116-
HttpGet httpGet = new HttpGet(uri);
117-
118-
if (authDatafileToken != null) {
119-
httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + authDatafileToken);
120-
}
121-
122-
if (datafileLastModified != null) {
123-
httpGet.setHeader(HttpHeaders.IF_MODIFIED_SINCE, datafileLastModified);
124-
}
116+
HttpGet httpGet = createHttpRequest();
125117

126118
logger.debug("Fetching datafile from: {}", httpGet.getURI());
127119
try {
@@ -138,6 +130,20 @@ protected ProjectConfig poll() {
138130
return null;
139131
}
140132

133+
HttpGet createHttpRequest() {
134+
HttpGet httpGet = new HttpGet(uri);
135+
136+
if (authDatafileToken != null) {
137+
httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + authDatafileToken);
138+
}
139+
140+
if (datafileLastModified != null) {
141+
httpGet.setHeader(HttpHeaders.IF_MODIFIED_SINCE, datafileLastModified);
142+
}
143+
144+
return httpGet;
145+
}
146+
141147
public static Builder builder() {
142148
return new Builder();
143149
}

core-httpclient-impl/src/test/java/com/optimizely/ab/OptimizelyFactoryTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ public void newDefaultInstanceWithFallback() throws Exception {
191191
assertTrue(optimizely.isValid());
192192
}
193193

194+
@Test
195+
public void newDefaultInstanceWithAuthDatafileToken() throws Exception {
196+
String datafileString = Resources.toString(Resources.getResource("valid-project-config-v4.json"), Charsets.UTF_8);
197+
optimizely = OptimizelyFactory.newDefaultInstance("sdk-key", "auth-token", datafileString);
198+
assertTrue(optimizely.isValid());
199+
}
200+
194201
@Test
195202
public void newDefaultInstanceWithProjectConfig() throws Exception {
196203
optimizely = OptimizelyFactory.newDefaultInstance(() -> null);

core-httpclient-impl/src/test/java/com/optimizely/ab/config/HttpProjectConfigManagerTest.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
import static java.util.concurrent.TimeUnit.SECONDS;
4444
import static org.junit.Assert.*;
4545
import static org.mockito.Matchers.any;
46-
import static org.mockito.Mockito.mock;
47-
import static org.mockito.Mockito.reset;
48-
import static org.mockito.Mockito.when;
46+
import static org.mockito.Mockito.*;
4947

5048
@RunWith(MockitoJUnitRunner.class)
5149
public class HttpProjectConfigManagerTest {
@@ -125,6 +123,58 @@ public void testHttpGetByCustomUrl() throws Exception {
125123
assertEquals(new URI(expected), actual);
126124
}
127125

126+
@Test
127+
public void testHttpGetBySdkKeyForAuthDatafile() throws Exception {
128+
projectConfigManager = builder()
129+
.withOptimizelyHttpClient(mockHttpClient)
130+
.withSdkKey("sdk-key")
131+
.withAuthDatafileToken("auth-token")
132+
.build();
133+
134+
URI actual = projectConfigManager.getUri();
135+
assertEquals(new URI("https://www.optimizely-cdn.com/datafiles/auth/sdk-key.json"), actual);
136+
}
137+
138+
@Test
139+
public void testHttpGetByCustomUrlForAuthDatafile() throws Exception {
140+
String expected = "https://custom.optimizely.com/custom-location.json";
141+
142+
projectConfigManager = builder()
143+
.withOptimizelyHttpClient(mockHttpClient)
144+
.withUrl(expected)
145+
.withSdkKey("sdk-key")
146+
.withAuthDatafileToken("auth-token")
147+
.build();
148+
149+
URI actual = projectConfigManager.getUri();
150+
assertEquals(new URI(expected), actual);
151+
}
152+
153+
@Test
154+
public void testCreateHttpRequest() throws Exception {
155+
projectConfigManager = builder()
156+
.withOptimizelyHttpClient(mockHttpClient)
157+
.withSdkKey("sdk-key")
158+
.build();
159+
160+
HttpGet request = projectConfigManager.createHttpRequest();
161+
assertEquals(request.getURI().toString(), "https://cdn.optimizely.com/datafiles/sdk-key.json");
162+
assertEquals(request.getHeaders("Authorization").length, 0);
163+
}
164+
165+
@Test
166+
public void testCreateHttpRequestForAuthDatafile() throws Exception {
167+
projectConfigManager = builder()
168+
.withOptimizelyHttpClient(mockHttpClient)
169+
.withSdkKey("sdk-key")
170+
.withAuthDatafileToken("auth-token")
171+
.build();
172+
173+
HttpGet request = projectConfigManager.createHttpRequest();
174+
assertEquals(request.getURI().toString(), "https://www.optimizely-cdn.com/datafiles/auth/sdk-key.json");
175+
assertEquals(request.getHeaders("Authorization")[0].getValue(), "Bearer auth-token");
176+
}
177+
128178
@Test
129179
public void testPoll() throws Exception {
130180
projectConfigManager = builder()

0 commit comments

Comments
 (0)