Skip to content

Commit 27d17e9

Browse files
committed
Fix unit tests and improve interceptor handling
1 parent 9b61d4d commit 27d17e9

2 files changed

Lines changed: 95 additions & 25 deletions

File tree

library/src/androidTest/java/com/proxerme/library/connection/ProxerConnectionTest.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.proxerme.library.connection.notifications.entitiy.News;
88
import com.proxerme.library.connection.notifications.request.NewsRequest;
9+
import com.proxerme.library.test.BuildConfig;
910
import com.proxerme.library.test.R;
1011
import com.squareup.moshi.Moshi;
1112

@@ -47,9 +48,12 @@ public class ProxerConnectionTest {
4748
"Listener was not unregistered.";
4849
private static final String ERROR_WRONG_LISTENER_CALLED = "Wrong listener called.";
4950

50-
private static final String API_KEY_HEADER = "proxer-api-key";
5151
private static final String API_KEY = "test";
5252

53+
private static final String USER_AGENT_HEADER = "User-Agent";
54+
private static final String DEFAULT_USER_AGENT = "ProxerLibAndroid/" + BuildConfig.VERSION_NAME;
55+
private static final String CUSTOM_USER_AGENT = "Test/123";
56+
5357
private static final long TIMEOUT = 123456L;
5458

5559
private static MockWebServer server = new MockWebServer();
@@ -67,13 +71,28 @@ public static void tearDownServer() throws IOException {
6771
}
6872

6973
@Test
70-
public void testApiKeyHeader() throws Exception {
74+
public void testUserAgentHeader() throws Exception {
7175
server.enqueue(new MockResponse().setBody(loadResponse(R.raw.news)));
7276

7377
connection.executeSynchronized(new NewsRequest(0)
7478
.withCustomHost(buildHostUrl(server.url(URL))));
7579

76-
assertEquals(API_KEY, server.takeRequest().getHeader(API_KEY_HEADER));
80+
assertEquals(DEFAULT_USER_AGENT, server.takeRequest().getHeader(USER_AGENT_HEADER));
81+
}
82+
83+
@Test
84+
public void testCustomUserAgentHeader() throws Exception {
85+
ProxerConnection testConnection = new ProxerConnection.Builder(API_KEY,
86+
InstrumentationRegistry.getContext())
87+
.withCustomUserAgent(CUSTOM_USER_AGENT)
88+
.build();
89+
90+
server.enqueue(new MockResponse().setBody(loadResponse(R.raw.news)));
91+
92+
testConnection.executeSynchronized(new NewsRequest(0)
93+
.withCustomHost(buildHostUrl(server.url(URL))));
94+
95+
assertEquals(CUSTOM_USER_AGENT, server.takeRequest().getHeader(USER_AGENT_HEADER));
7796
}
7897

7998
@Test
@@ -118,6 +137,8 @@ public void testSuccessfulExecution() throws Exception {
118137
.withCustomHost(buildHostUrl(server.url(URL))));
119138

120139
assertNotNull(news);
140+
141+
server.takeRequest();
121142
}
122143

123144
@Test(timeout = 3000)
@@ -140,6 +161,8 @@ public void onError(ProxerException exception) {
140161
});
141162

142163
lock.await();
164+
165+
server.takeRequest();
143166
}
144167

145168
@Test
@@ -169,6 +192,8 @@ public void testProxerException() throws Exception {
169192
} catch (ProxerException exception) {
170193
assertEquals(ProxerException.PROXER, exception.getErrorCode());
171194
}
195+
196+
server.takeRequest();
172197
}
173198

174199
@Test
@@ -183,6 +208,8 @@ public void testProxerExceptionMessage() throws Exception {
183208
} catch (ProxerException exception) {
184209
assertEquals(exception.getMessage(), "News konnten nicht abgerufen werden.");
185210
}
211+
212+
server.takeRequest();
186213
}
187214

188215
@Test
@@ -197,6 +224,8 @@ public void testProxerExceptionCode() throws Exception {
197224
} catch (ProxerException exception) {
198225
assertEquals((Integer) ProxerException.NEWS, exception.getProxerErrorCode());
199226
}
227+
228+
server.takeRequest();
200229
}
201230

202231
@Test
@@ -211,6 +240,8 @@ public void testUnparsableException() throws Exception {
211240
} catch (ProxerException exception) {
212241
assertEquals(ProxerException.UNPARSABLE, exception.getErrorCode());
213242
}
243+
244+
server.takeRequest();
214245
}
215246

216247
@Test
@@ -237,6 +268,8 @@ public void onError(ProxerException exception) {
237268

238269
call.cancel();
239270
lock.await();
271+
272+
server.takeRequest();
240273
}
241274

242275
@Test(timeout = 3000)
@@ -261,6 +294,8 @@ public void onError(@NonNull ProxerException exception) {
261294
null, null);
262295

263296
lock.await();
297+
298+
server.takeRequest();
264299
}
265300

266301
@Test(timeout = 3000)
@@ -290,6 +325,8 @@ public void onError(@NonNull ProxerException exception) {
290325
null, null);
291326

292327
lock.await();
328+
329+
server.takeRequest();
293330
}
294331

295332
@Test(timeout = 3000)
@@ -318,6 +355,8 @@ public void onError(ProxerException exception) {
318355
});
319356

320357
lock.await();
358+
359+
server.takeRequest();
321360
}
322361

323362
@Test(timeout = 3000)
@@ -346,6 +385,8 @@ public void onError(ProxerException exception) {
346385
});
347386

348387
lock.await();
388+
389+
server.takeRequest();
349390
}
350391

351392
@Test(timeout = 3000)
@@ -377,5 +418,7 @@ public void onError(ProxerException exception) {
377418
});
378419

379420
lock.await();
421+
422+
server.takeRequest();
380423
}
381424
}

library/src/main/java/com/proxerme/library/connection/ProxerConnection.java

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,13 @@ public interface ErrorListener {
307307
public static class Builder {
308308

309309
private static final String API_KEY_HEADER = "proxer-api-key";
310+
private static final String DEFAULT_USER_AGENT = "ProxerLibAndroid";
310311
private static final String USER_AGENT_HEADER = "User-Agent";
311312

312313
private String apiKey;
313314
private Context context;
314315
private boolean deliverCancelledRequests;
315-
private String userAgent = "ProxerLibAndroid/" + BuildConfig.VERSION_NAME;
316+
private String userAgent;
316317

317318
private Moshi moshi;
318319
private CookieJar cookieJar;
@@ -350,6 +351,7 @@ public Builder(@NonNull String apiKey, @NonNull CookieJar cookieJar) {
350351
public ProxerConnection build() {
351352
configureMoshi();
352353
configureCookieJar();
354+
configureUserAgent();
353355
configureOkHttp();
354356

355357
return new ProxerConnection(apiKey, moshi, httpClient, deliverCancelledRequests);
@@ -418,6 +420,12 @@ private void configureCookieJar() {
418420
}
419421
}
420422

423+
private void configureUserAgent() {
424+
if (userAgent == null) {
425+
userAgent = DEFAULT_USER_AGENT + "/" + BuildConfig.VERSION_NAME;
426+
}
427+
}
428+
421429
private void configureOkHttp() {
422430
OkHttpClient.Builder builder;
423431

@@ -428,28 +436,47 @@ private void configureOkHttp() {
428436
}
429437

430438
httpClient = builder.cookieJar(cookieJar)
431-
.addInterceptor(new Interceptor() {
432-
@Override
433-
public Response intercept(Chain chain) throws IOException {
434-
if (chain.request().url().host()
435-
.equals(ProxerUrlHolder.getBaseApiHost().host())) {
436-
return chain.proceed(chain.request().newBuilder()
437-
.header(API_KEY_HEADER, apiKey)
438-
.build());
439-
} else {
440-
return chain.proceed(chain.request());
441-
}
442-
}
443-
})
444-
.addInterceptor(new Interceptor() {
445-
@Override
446-
public Response intercept(Chain chain) throws IOException {
447-
return chain.proceed(chain.request().newBuilder()
448-
.header(USER_AGENT_HEADER, userAgent)
449-
.build());
450-
}
451-
})
439+
.addInterceptor(new ApiKeyInterceptor(apiKey))
440+
.addInterceptor(new UserAgentInterceptor(userAgent))
452441
.build();
453442
}
443+
444+
private class ApiKeyInterceptor implements Interceptor {
445+
446+
private String apiKey;
447+
448+
public ApiKeyInterceptor(String apiKey) {
449+
this.apiKey = apiKey;
450+
}
451+
452+
@Override
453+
public Response intercept(Chain chain) throws IOException {
454+
if (chain.request().url().host()
455+
.equals(ProxerUrlHolder.getBaseApiHost().host())) {
456+
return chain.proceed(chain.request().newBuilder()
457+
.header(API_KEY_HEADER, apiKey)
458+
.build());
459+
} else {
460+
return chain.proceed(chain.request());
461+
}
462+
}
463+
}
464+
465+
private class UserAgentInterceptor implements Interceptor {
466+
467+
private String userAgent;
468+
469+
UserAgentInterceptor(@NonNull String userAgent) {
470+
this.userAgent = userAgent;
471+
}
472+
473+
@Override
474+
public Response intercept(Chain chain) throws IOException {
475+
return chain.proceed(chain.request().newBuilder()
476+
.header(USER_AGENT_HEADER, userAgent)
477+
.build());
478+
}
479+
}
480+
454481
}
455482
}

0 commit comments

Comments
 (0)