/** * Whether the request has any Forwarded headers. * @param request the request */ protected boolean hasForwardedHeaders(ServerHttpRequest request) { HttpHeaders headers = request.getHeaders(); for (String headerName : FORWARDED_HEADER_NAMES) { if (headers.containsKey(headerName)) { return true; } } return false; }
@Override public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) { headers.setBasicAuth(this.username, this.password, this.charset); } return execution.execute(request, body); }
@Override public void onSuccess(HttpHeaders result) { assertTrue("No Content-Type header", result.containsKey("Content-Type")); } @Override
@Override protected void applyHeaders() { HttpHeaders headers = getHeaders(); headers.forEach((key, value) -> value.forEach(v -> this.jettyRequest.header(key, v))); if (!headers.containsKey(HttpHeaders.ACCEPT)) { this.jettyRequest.header(HttpHeaders.ACCEPT, "*/*"); } }
/** * Expect that the header with the given name is present. * @since 5.0.3 */ public WebTestClient.ResponseSpec exists(String name) { if (!getHeaders().containsKey(name)) { String message = getMessage(name) + " does not exist"; this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message)); } return this.responseSpec; }
@Test public void headForHeaders() throws Exception { Future<HttpHeaders> headersFuture = template.headForHeaders(baseUrl + "/get"); HttpHeaders headers = headersFuture.get(); assertTrue("No Content-Type header", headers.containsKey("Content-Type")); }
/** * Expect that the header with the given name is not present. */ public WebTestClient.ResponseSpec doesNotExist(String name) { if (getHeaders().containsKey(name)) { String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]"; this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message)); } return this.responseSpec; }
@Test public void writeWithError() throws Exception { TestServerHttpResponse response = new TestServerHttpResponse(); response.getHeaders().setContentLength(12); IllegalStateException error = new IllegalStateException("boo"); response.writeWith(Flux.error(error)).onErrorResume(ex -> Mono.empty()).block(); assertFalse(response.statusCodeWritten); assertFalse(response.headersWritten); assertFalse(response.cookiesWritten); assertFalse(response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)); assertTrue(response.body.isEmpty()); }
@Test public void emptyCacheControl() { Integer entity = 42; ResponseEntity<Integer> responseEntity = ResponseEntity.status(HttpStatus.OK) .cacheControl(CacheControl.empty()) .body(entity); assertNotNull(responseEntity); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertFalse(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL)); assertEquals(entity, responseEntity.getBody()); }
@Test public void headForHeadersCallbackWithLambdas() throws Exception { ListenableFuture<HttpHeaders> headersFuture = template.headForHeaders(baseUrl + "/get"); headersFuture.addCallback(result -> assertTrue("No Content-Type header", result.containsKey("Content-Type")), ex -> fail(ex.getMessage())); waitTillDone(headersFuture); }
@Test public void createdLocation() throws URISyntaxException { URI location = new URI("location"); ResponseEntity<Void> responseEntity = ResponseEntity.created(location).build(); assertNotNull(responseEntity); assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode()); assertTrue(responseEntity.getHeaders().containsKey("Location")); assertEquals(location.toString(), responseEntity.getHeaders().getFirst("Location")); assertNull(responseEntity.getBody()); ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body("Hello World"); }
@Test public void actualRequestWithOriginHeader() throws Exception { ServerWebExchange exchange = actualRequest(); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void actualRequestWithOriginHeaderAndNullConfig() throws Exception { ServerWebExchange exchange = actualRequest(); this.processor.process(null, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertNull(response.getStatusCode()); }
@Test public void actualRequestCaseInsensitiveOriginMatch() throws Exception { ServerWebExchange exchange = actualRequest(); this.conf.addAllowedOrigin("http://DOMAIN2.com"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertNull(response.getStatusCode()); }
@Test public void preflightRequestTestWithOriginButWithoutOtherHeaders() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest()); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void preflightRequestWithoutRequestMethod() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void get() { RequestEntity<Void> requestEntity = RequestEntity.get(URI.create("http://example.com")).accept( MediaType.IMAGE_GIF, MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG).build(); assertNotNull(requestEntity); assertEquals(HttpMethod.GET, requestEntity.getMethod()); assertTrue(requestEntity.getHeaders().containsKey("Accept")); assertEquals("image/gif, image/jpeg, image/png", requestEntity.getHeaders().getFirst("Accept")); assertNull(requestEntity.getBody()); }
@Test public void preflightRequestWithRequestAndMethodHeaderButNoConfig() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from(preFlightRequest() .header(ACCESS_CONTROL_REQUEST_METHOD, "GET") .header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1")); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void preflightRequestWithNullConfig() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET")); this.conf.addAllowedOrigin("*"); this.processor.process(null, exchange); ServerHttpResponse response = exchange.getResponse(); assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); }
@Test public void preExistingHeadersFromHttpServletResponse() { String headerName = "Access-Control-Allow-Origin"; String headerValue = "localhost:8080"; this.mockResponse.addHeader(headerName, headerValue); this.response = new ServletServerHttpResponse(this.mockResponse); assertEquals(headerValue, this.response.getHeaders().getFirst(headerName)); assertEquals(Collections.singletonList(headerValue), this.response.getHeaders().get(headerName)); assertTrue(this.response.getHeaders().containsKey(headerName)); assertEquals(headerValue, this.response.getHeaders().getFirst(headerName)); assertEquals(headerValue, this.response.getHeaders().getAccessControlAllowOrigin()); }