@Test public void interceptorDoesAddUserAgent() throws Exception { Request request = new Request.Builder().url("https://test.desk.com").build(); assertFalse(doesHaveUserAgent(request)); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.interceptors().add(userAgentInterceptor); Call call = builder.build().newCall(request); Response response = call.execute(); assertTrue(doesHaveUserAgent(response.request())); assertEquals(TEST_USER_AGENT, response.request().header(HEADER_USER_AGENT)); }
@Override public T call() throws Exception { Request request = null; Response response = null; Exception exception = null; Call call = null; try { MNSLog.logD("[call] - "); if (context.getCancellationHandler().isCancelled()) { throw new InterruptedIOException("This task is cancelled!"); } Request.Builder requestBuilder = new Request.Builder(); // build request url String url = message.buildCanonicalURL(); MNSUtils.signRequest(message); requestBuilder = requestBuilder.url(url); // set request headers for (String key : message.getHeaders().keySet()) { requestBuilder = requestBuilder.addHeader(key, message.getHeaders().get(key)); } String contentType = message.getHeaders().get(MNSHeaders.CONTENT_TYPE); String content = message.getContent(); // set request body if (message.getContent() != null) { MNSUtils.assertTrue(contentType != null, "Content type can't be null when send data!"); requestBuilder = requestBuilder.method( message.getMethod().toString(), new ProgressTouchableRequestBody( message.getContent().getBytes(), contentType, context.getProgressCallback())); } else { switch (message.getMethod()) { case PUT: requestBuilder = requestBuilder.method( message.getMethod().toString(), RequestBody.create(null, new byte[0])); break; case GET: requestBuilder = requestBuilder.get(); break; case HEAD: requestBuilder = requestBuilder.head(); break; case DELETE: requestBuilder = requestBuilder.delete(); break; default: break; } } request = requestBuilder.build(); if (MNSLog.isEnableLog()) { MNSLog.logD("request url: " + request.url()); Map<String, List<String>> headerMap = request.headers().toMultimap(); for (String key : headerMap.keySet()) { MNSLog.logD("requestHeader " + key + ": " + headerMap.get(key).get(0)); } } call = client.newCall(request); context.getCancellationHandler().setCall(call); // send request response = call.execute(); if (MNSLog.isEnableLog()) { MNSLog.logD("response code: " + response.code() + " for url: " + request.url()); Map<String, List<String>> headerMap = response.headers().toMultimap(); for (String key : headerMap.keySet()) { MNSLog.logD("responseHeader " + key + ": " + headerMap.get(key).get(0)); } } } catch (Exception e) { MNSLog.logE("Encounter local execpiton: " + e.toString()); if (MNSLog.isEnableLog()) { e.printStackTrace(); } exception = new ClientException(e.getMessage(), e); } if (exception == null && (response.code() == 203 || response.code() >= 300)) { try { exception = ResponseParsers.parseResponseErrorXML(response); } catch (IOException e) { exception = new ClientException(e.getMessage(), e); } } else if (exception == null) { try { T result = responseParser.parse(response); if (context.getCompletedCallback() != null) { context.getCompletedCallback().onSuccess(context.getRequest(), result); } return result; } catch (IOException e) { exception = new ClientException(e.getMessage(), e); } } // reconstruct exception caused by manually cancelling if ((call != null && call.isCanceled()) || context.getCancellationHandler().isCancelled()) { exception = new ClientException("Task is cancelled!", exception.getCause(), true); } if (exception instanceof ClientException) { if (context.getCompletedCallback() != null) { context .getCompletedCallback() .onFailure(context.getRequest(), (ClientException) exception, null); } } else { if (context.getCompletedCallback() != null) { context .getCompletedCallback() .onFailure(context.getRequest(), null, (ServiceException) exception); } } throw exception; }