66
77import com .proxerme .library .connection .notifications .entitiy .News ;
88import com .proxerme .library .connection .notifications .request .NewsRequest ;
9+ import com .proxerme .library .test .BuildConfig ;
910import com .proxerme .library .test .R ;
1011import 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}
0 commit comments