@Override public Collection<String> getHeaders(String name) { Collection<String> headerValues = this.headers.get(name); return (headerValues != null ? headerValues : Collections.emptyList()); }
@Test public void preFlightRequestWithAmbiguousMapping() throws Exception { this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET"); ResponseEntity<String> entity = performOptions("/ambiguous", this.headers, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("http://localhost:9000", entity.getHeaders().getAccessControlAllowOrigin()); assertThat(entity.getHeaders().getAccessControlAllowMethods(), contains(HttpMethod.GET)); assertEquals(true, entity.getHeaders().getAccessControlAllowCredentials()); assertThat(entity.getHeaders().get(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN, HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS)); }
@Test public void headersCopy() { HttpHeaders customHeaders = new HttpHeaders(); customHeaders.set("X-CustomHeader", "vale"); ResponseEntity<Void> responseEntity = ResponseEntity.ok().headers(customHeaders).build(); HttpHeaders responseHeaders = responseEntity.getHeaders(); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(1, responseHeaders.size()); assertEquals(1, responseHeaders.get("X-CustomHeader").size()); assertEquals("vale", responseHeaders.getFirst("X-CustomHeader")); }
@Override public List<String> header(String headerName) { List<String> headerValues = delegate().get(headerName); return (headerValues != null ? headerValues : Collections.emptyList()); }
@Test public void checkNotModifiedHeaderAlreadySet() { MockServerHttpRequest request = get("/").ifModifiedSince(currentDate.toEpochMilli()).build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); exchange.getResponse().getHeaders().add("Last-Modified", CURRENT_TIME); assertTrue(exchange.checkNotModified(currentDate)); assertEquals(304, exchange.getResponse().getStatusCode().value()); assertEquals(1, exchange.getResponse().getHeaders().get("Last-Modified").size()); assertEquals(CURRENT_TIME, exchange.getResponse().getHeaders().getFirst("Last-Modified")); }
@Override public List<String> header(String headerName) { List<String> headerValues = delegate().get(headerName); return headerValues != null ? headerValues : Collections.emptyList(); }
@Test public void preflightRequestMatchedAllowedMethod() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET")); this.conf.addAllowedOrigin("*"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertNull(response.getStatusCode()); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals("GET,HEAD", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)); }
private void applyCookiesIfNecessary() { if (this.headers.get(HttpHeaders.COOKIE) == null) { this.cookies.values().stream().flatMap(Collection::stream) .forEach(cookie -> this.headers.add(HttpHeaders.COOKIE, cookie.toString())); } }
@Override public Collection<String> getHeaders(String name) { Collection<String> headerValues = this.headers.get(name); return (headerValues != null ? headerValues : Collections.emptyList()); }
@Override public HttpHeaders getHeaders() { String headerName = HttpHeaders.SET_COOKIE; if (!getCookies().isEmpty() && this.headers.get(headerName) == null) { getCookies().values().stream().flatMap(Collection::stream) .forEach(cookie -> getHeaders().add(headerName, cookie.toString())); } return this.headers; }
private void applyCookiesIfNecessary() { if (this.headers.get(HttpHeaders.COOKIE) == null) { this.cookies.values().stream().flatMap(Collection::stream) .forEach(cookie -> this.headers.add(HttpHeaders.COOKIE, cookie.toString())); } }
@Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { for (String headerName : this.headers.keySet()) { request.addHeader(headerName, this.headers.get(headerName)); } return request; } }
@Override public ClientHttpResponse execute() throws IOException { List<String> headerValues = getHeaders().get(headerName); assertEquals(2, headerValues.size()); assertEquals(headerValue, headerValues.get(0)); assertEquals(otherValue, headerValues.get(1)); return super.execute(); } };
@Override public HttpHeaders getHeaders() { String headerName = HttpHeaders.SET_COOKIE; if (!getCookies().isEmpty() && this.headers.get(headerName) == null) { getCookies().values().stream().flatMap(Collection::stream) .forEach(cookie -> getHeaders().add(headerName, cookie.toString())); } return this.headers; }
@Test public void expireWhenMultipleInvocationThenSetsSingleEmptyHeader() { this.idResolver.expireSession(this.exchange); this.idResolver.expireSession(this.exchange); assertEquals(Arrays.asList(""), this.exchange.getResponse().getHeaders().get(HeaderWebSessionIdResolver.DEFAULT_HEADER_NAME)); }
@Test public void header() throws Exception { Rendering rendering = Rendering.view("foo").header("foo", "bar").build(); assertEquals(1, rendering.headers().size()); assertEquals(Collections.singletonList("bar"), rendering.headers().get("foo")); }
@Test public void preflightRequestWrongAllowedMethod() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from( preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "DELETE")); this.conf.addAllowedOrigin("*"); this.processor.process(this.conf, exchange); ServerHttpResponse response = exchange.getResponse(); assertThat(response.getHeaders().get(VARY), contains(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS)); assertEquals(HttpStatus.FORBIDDEN, 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 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()); }