Java Code Examples for org.springframework.http.ResponseEntity#getHeaders()

The following examples show how to use org.springframework.http.ResponseEntity#getHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringDataApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceedingCapacity() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceB", String.class);
    HttpHeaders headers = response.getHeaders();
    String key = "rate-limit-application_serviceB_127.0.0.1";
    assertHeaders(headers, key, false, false);
    assertEquals(OK, response.getStatusCode());

    for (int i = 0; i < 2; i++) {
        response = this.restTemplate.getForEntity("/serviceB", String.class);
    }

    assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
    assertNotEquals(SpringDataApplication.ServiceController.RESPONSE_BODY, response.getBody());

    await().pollDelay(2, TimeUnit.SECONDS).untilAsserted(() -> {
        final ResponseEntity<String> responseAfterReset = this.restTemplate
            .getForEntity("/serviceB", String.class);
        final HttpHeaders headersAfterReset = responseAfterReset.getHeaders();
        assertHeaders(headersAfterReset, key, false, false);
        assertEquals(OK, responseAfterReset.getStatusCode());
    });
}
 
Example 2
Source File: SpringDataApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleUrls() {
    String randomPath = UUID.randomUUID().toString();

    for (int i = 0; i < 12; i++) {

        if (i % 2 == 0) {
            randomPath = UUID.randomUUID().toString();
        }

        ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceD/" + randomPath, String.class);
        HttpHeaders headers = response.getHeaders();
        assertHeaders(headers, "rate-limit-application_serviceD_serviceD_" + randomPath, false, false);
        assertEquals(OK, response.getStatusCode());
    }
}
 
Example 3
Source File: ConsulApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceedingCapacity() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceB", String.class);
    HttpHeaders headers = response.getHeaders();
    String key = "rate-limit-application_serviceB_127.0.0.1";
    assertHeaders(headers, key, false, false);
    assertEquals(OK, response.getStatusCode());

    for (int i = 0; i < 2; i++) {
        response = this.restTemplate.getForEntity("/serviceB", String.class);
    }

    assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
    assertNotEquals(ConsulApplication.ServiceController.RESPONSE_BODY, response.getBody());

    await().pollDelay(2, TimeUnit.SECONDS).untilAsserted(() -> {
        final ResponseEntity<String> responseAfterReset = this.restTemplate
            .getForEntity("/serviceB", String.class);
        final HttpHeaders headersAfterReset = responseAfterReset.getHeaders();
        assertHeaders(headersAfterReset, key, false, false);
        assertEquals(OK, responseAfterReset.getStatusCode());
    });
}
 
Example 4
Source File: RedisApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceedingCapacity() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceB", String.class);
    HttpHeaders headers = response.getHeaders();
    String key = "rate-limit-application_serviceB_127.0.0.1";
    assertHeaders(headers, key, false, false);
    assertEquals(OK, response.getStatusCode());

    for (int i = 0; i < 2; i++) {
        response = this.restTemplate.getForEntity("/serviceB", String.class);
    }

    assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
    assertNotEquals(RedisApplication.ServiceController.RESPONSE_BODY, response.getBody());

    await().pollDelay(2, TimeUnit.SECONDS).untilAsserted(() -> {
        final ResponseEntity<String> responseAfterReset = this.restTemplate
            .getForEntity("/serviceB", String.class);
        final HttpHeaders headersAfterReset = responseAfterReset.getHeaders();
        assertHeaders(headersAfterReset, key, false, false);
        assertEquals(OK, responseAfterReset.getStatusCode());
    });
}
 
Example 5
Source File: SpringDataApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 5 votes vote down vote up
@Test
public void testUsingBreakOnMatchSpecificCase() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceF", String.class);
    HttpHeaders headers = response.getHeaders();
    String key = "rate-limit-application_serviceF_127.0.0.1_127.0.0.1";
    assertHeaders(headers, key, false, false);
    assertEquals(OK, response.getStatusCode());

    response = this.restTemplate.getForEntity("/serviceF", String.class);
    headers = response.getHeaders();
    assertHeaders(headers, key, false, false);
    assertEquals(OK, response.getStatusCode());
}
 
Example 6
Source File: ResponseHeaderLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenResponseEntityConstructorAndMultipleHeadersRequest_thenObtainResponseWithCorrectHeaders() {
    final String requestUrl = "/response-entity-contructor-multiple-headers";
    ResponseEntity<String> response = template.getForEntity(SINGLE_BASE_URL + requestUrl, String.class);
    HttpHeaders responseHeaders = response.getHeaders();

    assertThat(responseHeaders).isNotEmpty();
    assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ResponseEntityConstructorAndHeaders"));
    assertThat(responseHeaders).containsEntry("Accept", Arrays.asList(MediaType.APPLICATION_JSON.toString()));
}
 
Example 7
Source File: ExceptionTranslator.java    From e-commerce-microservice with Apache License 2.0 5 votes vote down vote up
/**
 * Post-process the Problem payload to add the message key for the front-end if needed
 */
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    if (entity == null) {
        return entity;
    }
    Problem problem = entity.getBody();
    if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
        return entity;
    }
    ProblemBuilder builder = Problem.builder()
        .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
        .withStatus(problem.getStatus())
        .withTitle(problem.getTitle())
        .with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());

    if (problem instanceof ConstraintViolationProblem) {
        builder
            .with("violations", ((ConstraintViolationProblem) problem).getViolations())
            .with("message", ErrorConstants.ERR_VALIDATION);
    } else {
        builder
            .withCause(((DefaultProblem) problem).getCause())
            .withDetail(problem.getDetail())
            .withInstance(problem.getInstance());
        problem.getParameters().forEach(builder::with);
        if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
            builder.with("message", "error.http." + problem.getStatus().getStatusCode());
        }
    }
    return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
}
 
Example 8
Source File: ExceptionTranslator.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Post-process the Problem payload to add the message key for the front-end if needed.
 */
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    if (entity == null) {
        return entity;
    }
    Problem problem = entity.getBody();
    if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
        return entity;
    }
    ProblemBuilder builder = Problem.builder()
        .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
        .withStatus(problem.getStatus())
        .withTitle(problem.getTitle())
        .with(PATH_KEY, request.getNativeRequest(HttpServletRequest.class).getRequestURI());

    if (problem instanceof ConstraintViolationProblem) {
        builder
            .with(VIOLATIONS_KEY, ((ConstraintViolationProblem) problem).getViolations())
            .with(MESSAGE_KEY, ErrorConstants.ERR_VALIDATION);
    } else {
        builder
            .withCause(((DefaultProblem) problem).getCause())
            .withDetail(problem.getDetail())
            .withInstance(problem.getInstance());
        problem.getParameters().forEach(builder::with);
        if (!problem.getParameters().containsKey(MESSAGE_KEY) && problem.getStatus() != null) {
            builder.with(MESSAGE_KEY, "error.http." + problem.getStatus().getStatusCode());
        }
    }
    return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
}
 
Example 9
Source File: SpringDataApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotExceedingCapacityRequest() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceA", String.class);
    HttpHeaders headers = response.getHeaders();
    assertHeaders(headers, "rate-limit-application_serviceA_127.0.0.1", false, false);
    assertEquals(OK, response.getStatusCode());
}
 
Example 10
Source File: ExceptionTranslator.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
/**
 * Post-process the Problem payload to add the message key for the front-end if needed
 */
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    if (entity == null) {
        return entity;
    }
    Problem problem = entity.getBody();
    if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
        return entity;
    }
    ProblemBuilder builder = Problem.builder()
        .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
        .withStatus(problem.getStatus())
        .withTitle(problem.getTitle())
        .with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());

    if (problem instanceof ConstraintViolationProblem) {
        builder
            .with("violations", ((ConstraintViolationProblem) problem).getViolations())
            .with("message", ErrorConstants.ERR_VALIDATION);
    } else {
        builder
            .withCause(((DefaultProblem) problem).getCause())
            .withDetail(problem.getDetail())
            .withInstance(problem.getInstance());
        problem.getParameters().forEach(builder::with);
        if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
            builder.with("message", "error.http." + problem.getStatus().getStatusCode());
        }
    }
    return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
}
 
Example 11
Source File: SpringDataApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 5 votes vote down vote up
@Test
public void testExceedingQuotaCapacityRequest() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceE", String.class);
    HttpHeaders headers = response.getHeaders();
    String key = "rate-limit-application_serviceE_127.0.0.1";
    assertHeaders(headers, key, false, true);
    assertEquals(OK, response.getStatusCode());

    response = this.restTemplate.getForEntity("/serviceE", String.class);
    headers = response.getHeaders();
    assertHeaders(headers, key, false, true);
    assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
}
 
Example 12
Source File: ExceptionTranslator.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * Post-process Problem payload to add the message key for front-end if needed
 */
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    if (entity == null || entity.getBody() == null) {
        return entity;
    }
    Problem problem = entity.getBody();
    if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
        return entity;
    }
    ProblemBuilder builder = Problem.builder()
        .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
        .withStatus(problem.getStatus())
        .withTitle(problem.getTitle())
        .with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());

    if (problem instanceof ConstraintViolationProblem) {
        builder
            .with("violations", ((ConstraintViolationProblem) problem).getViolations())
            .with("message", ErrorConstants.ERR_VALIDATION);
        return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
    } else {
        builder
            .withCause(((DefaultProblem) problem).getCause())
            .withDetail(problem.getDetail())
            .withInstance(problem.getInstance());
        problem.getParameters().forEach(builder::with);
        if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
            builder.with("message", "error.http." + problem.getStatus().getStatusCode());
        }
        return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
    }
}
 
Example 13
Source File: RedisApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 5 votes vote down vote up
@Test
public void testExceedingQuotaCapacityRequest() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceE", String.class);
    HttpHeaders headers = response.getHeaders();
    String key = "rate-limit-application_serviceE_127.0.0.1";
    assertHeaders(headers, key, false, true);
    assertEquals(OK, response.getStatusCode());

    response = this.restTemplate.getForEntity("/serviceE", String.class);
    headers = response.getHeaders();
    assertHeaders(headers, key, false, true);
    assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());
}
 
Example 14
Source File: ExceptionTranslator.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Post-process the Problem payload to add the message key for the front-end if needed
 */
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    if (entity == null) {
        return entity;
    }
    Problem problem = entity.getBody();
    if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
        return entity;
    }
    ProblemBuilder builder = Problem.builder()
        .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
        .withStatus(problem.getStatus())
        .withTitle(problem.getTitle())
        .with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());

    if (problem instanceof ConstraintViolationProblem) {
        builder
            .with("violations", ((ConstraintViolationProblem) problem).getViolations())
            .with("message", ErrorConstants.ERR_VALIDATION);
    } else {
        builder
            .withCause(((DefaultProblem) problem).getCause())
            .withDetail(problem.getDetail())
            .withInstance(problem.getInstance());
        problem.getParameters().forEach(builder::with);
        if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
            builder.with("message", "error.http." + problem.getStatus().getStatusCode());
        }
    }
    return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
}
 
Example 15
Source File: GreetingControllerUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestExceedingCapacity_thenReturnTooManyRequestsResponse() throws InterruptedException {
    ResponseEntity<String> response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
    HttpHeaders headers = response.getHeaders();
    String key = "rate-limit-application_serviceAdvanced_127.0.0.1";
    assertHeaders(headers, key, false, false);
    assertEquals(OK, response.getStatusCode());

    for (int i = 0; i < 2; i++) {
        response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
    }

    headers = response.getHeaders();
    String limit = headers.getFirst(HEADER_LIMIT + key);
    String remaining = headers.getFirst(HEADER_REMAINING + key);
    String reset = headers.getFirst(HEADER_RESET + key);

    assertEquals(limit, "1");
    assertEquals(remaining, "0");
    assertNotEquals(reset, "2000");

    assertEquals(TOO_MANY_REQUESTS, response.getStatusCode());

    TimeUnit.SECONDS.sleep(2);

    response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class);
    headers = response.getHeaders();
    assertHeaders(headers, key, false, false);
    assertEquals(OK, response.getStatusCode());
}
 
Example 16
Source File: SteviaHttpClient.java    From stevia with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Posts data to the url given.
 *
    * @param url the URL the POST request is sent
 * @param postData list of input data for the POST requests
 * @param numberOfTimes the number of POST requests
 * @param cookies the cookies to set on the request, optional - null is a valid value
 * @return the a list of HTTP Responses
 * @throws IOException, InterruptedException 
 */
public List<HttpResponse> post(String url, List<HttpPostData> postData, int numberOfTimes, List<HttpCookie> cookies) throws IOException, InterruptedException{
	Assert.hasLength(url, "URL cannot be null");
	Assert.notEmpty(postData,"POST request cannot have empty post data");
	Assert.isTrue(numberOfTimes > 0, "numberOfTimes cannot be 0");

	List<HttpResponse> responses = new ArrayList<HttpResponse>(numberOfTimes);

       RestTemplate restTemplate = new RestTemplate();
       HttpHeaders headers = new HttpHeaders();

       for (HttpCookie cookie : cookies) {
           headers.add("Cookie", cookie.getKey()+"="+cookie.getValue());
       }


       for (int i = 0; i < numberOfTimes; i++) {
           HttpEntity<String> entity = new HttpEntity<String>(addParameters(postData), headers);

           ResponseEntity<String> response = restTemplate.exchange(url,
                   HttpMethod.POST, entity, String.class);

           HttpResponse httpResponse = new HttpResponse(response.getStatusCode(),response.getBody(),response.getHeaders());
           responses.add(httpResponse);
       }


	return responses;
}
 
Example 17
Source File: ExceptionTranslator.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * Post-process Problem payload to add the message key for front-end if needed
 */
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    if (entity == null || entity.getBody() == null) {
        return entity;
    }
    Problem problem = entity.getBody();
    if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
        return entity;
    }
    ProblemBuilder builder = Problem.builder()
        .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType())
        .withStatus(problem.getStatus())
        .withTitle(problem.getTitle())
        .with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());

    if (problem instanceof ConstraintViolationProblem) {
        builder
            .with("violations", ((ConstraintViolationProblem) problem).getViolations())
            .with("message", ErrorConstants.ERR_VALIDATION);
        return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
    } else {
        builder
            .withCause(((DefaultProblem) problem).getCause())
            .withDetail(problem.getDetail())
            .withInstance(problem.getInstance());
        problem.getParameters().forEach(builder::with);
        if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
            builder.with("message", "error.http." + problem.getStatus().getStatusCode());
        }
        return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
    }
}
 
Example 18
Source File: ResponseHeaderLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenFilterWithExtraHeaderRequest_thenObtainResponseWithCorrectHeaders() {
    final String requestUrl = "/extra-header";
    ResponseEntity<String> response = template.getForEntity(FILTER_BASE_URL + requestUrl, String.class);
    HttpHeaders responseHeaders = response.getHeaders();

    assertThat(responseHeaders).isNotEmpty();
    assertThat(responseHeaders).containsEntry(SERVICE_FILTER_RESPONSE_HEADER, Arrays.asList("Value-Filter"));
    assertThat(responseHeaders).containsEntry(SERVICE_SINGLE_RESPONSE_HEADER, Arrays.asList("Value-ExtraHeader"));
}
 
Example 19
Source File: Bucket4jJCacheApplicationTestIT.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoRateLimit() {
    ResponseEntity<String> response = this.restTemplate.getForEntity("/serviceC", String.class);
    HttpHeaders headers = response.getHeaders();
    assertHeaders(headers, "rate-limit-application_serviceC", true, false);
    assertEquals(OK, response.getStatusCode());
}
 
Example 20
Source File: RestTemplateResponse.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
public RestTemplateResponse(ResponseEntity<String> response) {
    this.body = response.getBody();
    this.status = response.getStatusCodeValue();
    this.message = response.getStatusCode().getReasonPhrase();
    this.headers = response.getHeaders();
}