|
17 | 17 | package com.cloudant.sync.internal.replication; |
18 | 18 |
|
19 | 19 | import com.cloudant.common.CouchTestBase; |
| 20 | +import com.cloudant.http.HttpConnectionInterceptorContext; |
20 | 21 | import com.cloudant.http.HttpConnectionRequestInterceptor; |
21 | 22 | import com.cloudant.http.HttpConnectionResponseInterceptor; |
22 | 23 | import com.cloudant.http.internal.interceptors.CookieInterceptor; |
|
40 | 41 | import java.io.File; |
41 | 42 | import java.io.UnsupportedEncodingException; |
42 | 43 | import java.lang.reflect.Field; |
| 44 | +import java.lang.reflect.InvocationTargetException; |
| 45 | +import java.lang.reflect.Method; |
43 | 46 | import java.net.MalformedURLException; |
44 | 47 | import java.net.URISyntaxException; |
45 | 48 | import java.net.URLEncoder; |
| 49 | +import java.nio.charset.Charset; |
46 | 50 | import java.util.ArrayList; |
47 | 51 | import java.util.List; |
48 | 52 |
|
@@ -266,25 +270,27 @@ protected PullResult pull(PullFilter filter) throws Exception { |
266 | 270 | } |
267 | 271 |
|
268 | 272 | protected void assertCookieInterceptorPresent(ReplicatorBuilder p, String expectedRequestBody) |
269 | | - throws NoSuchFieldException, IllegalAccessException { |
| 273 | + throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, |
| 274 | + InvocationTargetException { |
270 | 275 | // peek inside these private fields to see that interceptors have been set |
271 | 276 | Field reqI = ReplicatorBuilder.class.getDeclaredField("requestInterceptors"); |
272 | 277 | Field respI = ReplicatorBuilder.class.getDeclaredField("responseInterceptors"); |
273 | 278 | reqI.setAccessible(true); |
274 | 279 | respI.setAccessible(true); |
275 | | - List<HttpConnectionRequestInterceptor> reqIList = (List)reqI.get(p); |
276 | | - List<HttpConnectionRequestInterceptor> respIList = (List)respI.get(p); |
| 280 | + List<HttpConnectionRequestInterceptor> reqIList = (List) reqI.get(p); |
| 281 | + List<HttpConnectionRequestInterceptor> respIList = (List) respI.get(p); |
277 | 282 | // Note this introspection happens before the interceptors are passed to the CouchClient so |
278 | 283 | // excludes any other interceptors (e.g. UserAgentInterceptor that might be added there). |
279 | 284 | Assert.assertEquals(1, reqIList.size()); |
280 | 285 | Assert.assertEquals(CookieInterceptor.class, reqIList.get(0).getClass()); |
281 | 286 | Assert.assertEquals(1, respIList.size()); |
282 | 287 | Assert.assertEquals(CookieInterceptor.class, respIList.get(0).getClass()); |
283 | | - CookieInterceptorBase ci = (CookieInterceptorBase)reqIList.get(0); |
284 | | - Field srbField = CookieInterceptorBase.class.getDeclaredField("sessionRequestBody"); |
285 | | - srbField.setAccessible(true); |
286 | | - byte[] srb = (byte[])srbField.get(ci); |
287 | | - String srbString = new String(srb); |
| 288 | + CookieInterceptor ci = (CookieInterceptor) reqIList.get(0); |
| 289 | + Method getPayloadMethod = CookieInterceptorBase.class.getDeclaredMethod( |
| 290 | + "getSessionRequestPayload", new Class[]{HttpConnectionInterceptorContext.class}); |
| 291 | + getPayloadMethod.setAccessible(true); |
| 292 | + byte[] srb = (byte[]) getPayloadMethod.invoke(ci, new Object[]{null}); |
| 293 | + String srbString = new String(srb, Charset.forName("UTF-8")); |
288 | 294 | Assert.assertEquals(expectedRequestBody, srbString); |
289 | 295 | } |
290 | 296 |
|
|
0 commit comments