@Override
 public ClientHttpResponse intercept(
     HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
   if (logger.isDebugEnabled()) {
     if (body.length > 0) {
       String tmp = IOUtils.toString(body, "UTF-8");
       if (StringUtils.hasText(tmp)) {
         logger.debug(
             "request uri : {} , param : {} ",
             request.getURI().toString(),
             tmp.substring(0, Math.min(tmp.length(), 1024)));
       } else {
         logger.debug("request uri : {}", request.getURI().toString());
       }
     } else {
       logger.debug("request uri : {}", request.getURI().toString());
     }
   }
   try {
     if (Util.getRequest() != null) {
       request.getHeaders().add("principal", Util.getPrincipal());
       request.getHeaders().add("admin", Util.isAdmin() + "");
       request.getHeaders().add("loginIp", Util.getLoginIp());
     }
   } catch (Exception e) {
     logger.error("", e);
   }
   return execution.execute(request, body);
 }
  @Test
  public void testInterceptWithConfiguration() throws Exception {
    prepareExecution("http://localhost:8080", "TestResponse", "markdown");

    ClientHttpRequestExecution configureExecution = Mockito.mock(ClientHttpRequestExecution.class);

    when(configureExecution.execute(any(HttpRequest.class), any(byte[].class)))
        .thenAnswer(
            new Answer<ClientHttpResponse>() {
              @Override
              public ClientHttpResponse answer(InvocationOnMock invocation) throws Throwable {
                interceptor.intercept(
                    (HttpRequest) invocation.getArguments()[0],
                    (byte[]) invocation.getArguments()[1],
                    execution);

                return response;
              }
            });

    CitrusRestDocsSupport.restDocsConfigurer(restDocumentation)
        .snippets()
        .withTemplateFormat(TemplateFormats.markdown())
        .intercept(request, "TestMessage".getBytes(), configureExecution);

    assertExpectedSnippetFilesExist(
        "markdown", "http-request.md", "http-response.md", "curl-request.md");
  }
Ejemplo n.º 3
0
  /** {@inheritDoc} */
  public ClientHttpResponse intercept(
      HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    handleRequest(getRequestContent(request, new String(body)));

    ClientHttpResponse response = execution.execute(request, body);
    CachingClientHttpResponseWrapper bufferedResponse =
        new CachingClientHttpResponseWrapper(response);
    handleResponse(getResponseContent(bufferedResponse));

    return bufferedResponse;
  }
  private void prepareExecution(
      String uri, String responseBody, String identifier, Snippet... snippets) throws IOException {
    when(execution.execute(any(HttpRequest.class), any(byte[].class))).thenReturn(response);
    when(request.getURI()).thenReturn(URI.create(uri));
    when(request.getMethod()).thenReturn(HttpMethod.GET);
    when(request.getHeaders()).thenReturn(new HttpHeaders());

    when(response.getHeaders()).thenReturn(new HttpHeaders());
    when(response.getStatusCode()).thenReturn(HttpStatus.OK);
    when(response.getBody()).thenReturn(new ByteArrayInputStream(responseBody.getBytes()));

    this.interceptor = CitrusRestDocsSupport.restDocsInterceptor(identifier, snippets);
  }
 /* Method to intercept Request and response call.
  * @see org.springframework.http.client.ClientHttpRequestInterceptor#intercept(org.springframework.http.HttpRequest, byte[], org.springframework.http.client.ClientHttpRequestExecution)
  */
 @Override
 public ClientHttpResponse intercept(
     HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
   logRequest(request);
   LOGGER.debug("Body of the request message is: {}", body);
   LOGGER.info("Unique ID for this service call is: {}", UUID.randomUUID());
   long starTime = new Date().getTime();
   ClientHttpResponse response = execution.execute(request, body);
   LOGGER.debug(
       "Total time taken for the service call is: {}ms", (new Date().getTime() - starTime));
   logResponse(response);
   return response;
 }
  @Override
  public synchronized ClientHttpResponse intercept(
      HttpRequest request, byte[] byteArray, ClientHttpRequestExecution execution)
      throws IOException {
    List<String> setCookies = request.getHeaders().get(COOKIE);

    // If the header doesn't exist and we have stored cookies, add any existing, saved cookies.
    if (setCookies == null && hasCookies()) {
      for (String cookie : cookies) {
        request.getHeaders().add(COOKIE, cookie);
      }
    }

    // Execute the request.
    ClientHttpResponse response = execution.execute(request, byteArray);
    // Pull any cookies off and store them.
    cookies = response.getHeaders().get(SET_COOKIE);

    return response;
  }
 @Override
 public ClientHttpResponse intercept(
     HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
   request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
   return execution.execute(request, body);
 }
 @Override
 public ClientHttpResponse intercept(
     HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
   return execution.execute(request, data);
 }