/** * Return the (new) location of a resource * as specified by the {@code Location} header. * <p>Returns {@code null} when the location is unknown. */ @Nullable public URI getLocation() { String value = getFirst(LOCATION); return (value != null ? URI.create(value) : null); }
@Override @Nullable public Header getContentEncoding() { String contentEncoding = this.headers.getFirst("Content-Encoding"); return (contentEncoding != null ? new BasicHeader("Content-Encoding", contentEncoding) : null); }
@Override @Nullable public String getFirst(String headerName) { String value = servletResponse.getHeader(headerName); if (value != null) { return value; } else { return super.getFirst(headerName); } }
private String getRequiredValue(String name) { String value = getHeaders().getFirst(name); if (value == null) { AssertionErrors.fail(getMessage(name) + " not found"); } return value; }
@Test // SPR-17330 public void matchDateFormattedWithHttpHeaders() throws Exception { long epochMilli = ZonedDateTime.of(2018, 10, 5, 0, 0, 0, 0, ZoneId.of("GMT")).toInstant().toEpochMilli(); HttpHeaders headers = new HttpHeaders(); headers.setDate("myDate", epochMilli); this.response.setHeader("d", headers.getFirst("myDate")); this.matchers.dateValue("d", epochMilli).match(this.mvcResult); }
/** * 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 // SPR-12894 public void useDefaultContentType() throws Exception { Resource body = new ClassPathResource("logo.png", getClass()); MethodParameter type = on(TestController.class).resolveReturnType(Resource.class); this.resultHandler.writeBody(body, type, this.exchange).block(Duration.ofSeconds(5)); assertEquals("image/png", this.exchange.getResponse().getHeaders().getFirst("Content-Type")); }
@Test public void accept() { MediaType mediaType1 = new MediaType("text", "html"); MediaType mediaType2 = new MediaType("text", "plain"); List<MediaType> mediaTypes = new ArrayList<>(2); mediaTypes.add(mediaType1); mediaTypes.add(mediaType2); headers.setAccept(mediaTypes); assertEquals("Invalid Accept header", mediaTypes, headers.getAccept()); assertEquals("Invalid Accept header", "text/html, text/plain", headers.getFirst("Accept")); }
@Test public void allow() { EnumSet<HttpMethod> methods = EnumSet.of(HttpMethod.GET, HttpMethod.POST); headers.setAllow(methods); assertEquals("Invalid Allow header", methods, headers.getAllow()); assertEquals("Invalid Allow header", "GET,POST", headers.getFirst("Allow")); }
@Test public void hostNoPort() { InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 0); headers.setHost(host); assertEquals("Invalid Host header", host, headers.getHost()); assertEquals("Invalid Host header", "localhost", headers.getFirst("Host")); }
@Test public void httpHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); String body = "foo"; HttpEntity<String> entity = new HttpEntity<>(body, headers); assertEquals(body, entity.getBody()); assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); }
@Test public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception { long secondLater = this.currentTime + 1000; String expected = this.dateFormat.format(new Date(secondLater)); assertIncorrectResponseHeader(header().string(LAST_MODIFIED, expected), expected); assertIncorrectResponseHeader(header().string(LAST_MODIFIED, equalTo(expected)), expected); // Comparison by date uses HttpHeaders to format the date in the error message. HttpHeaders headers = new HttpHeaders(); headers.setDate("expected", secondLater); assertIncorrectResponseHeader(header().dateValue(LAST_MODIFIED, secondLater), headers.getFirst("expected")); }
@Test(expected = IllegalArgumentException.class) public void illegalETag() { String eTag = "v2.6"; headers.setETag(eTag); assertEquals("Invalid ETag header", eTag, headers.getETag()); assertEquals("Invalid ETag header", "\"v2.6\"", headers.getFirst("ETag")); }
@Test public void invalidRange() throws Exception { testWrite(get("/").header(HttpHeaders.RANGE, "invalid").build()); assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes")); assertThat(this.response.getStatusCode(), is(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)); }
@Test public void multiValueMap() { MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.set("Content-Type", "text/plain"); String body = "foo"; HttpEntity<String> entity = new HttpEntity<>(body, map); assertEquals(body, entity.getBody()); assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); }
@Test public void cacheControlBuilder() { headers.setCacheControl(CacheControl.noCache()); assertEquals("Invalid Cache-Control header", "no-cache", headers.getCacheControl()); assertEquals("Invalid Cache-Control header", "no-cache", headers.getFirst("cache-control")); }
@Test public void actualRequestWithGlobalCorsConfig() throws Exception { CorsConfiguration mappedConfig = new CorsConfiguration(); mappedConfig.addAllowedOrigin("*"); this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/welcome.html", mappedConfig)); String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.GET, "/welcome.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertSame(this.welcomeController, actual); assertEquals("*", exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); }
@Test public void preFlightRequestWithGlobalCorsConfig() throws Exception { CorsConfiguration mappedConfig = new CorsConfiguration(); mappedConfig.addAllowedOrigin("*"); this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/welcome.html", mappedConfig)); String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.OPTIONS, "/welcome.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertNotSame(this.welcomeController, actual); assertEquals("*", exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); }
@Test public void requestHeaderAndCookie() { this.builder.build().get().uri("/path").accept(MediaType.APPLICATION_JSON) .cookies(cookies -> cookies.add("id", "123")) // SPR-16178 .exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("application/json", request.headers().getFirst("Accept")); assertEquals("123", request.cookies().getFirst("id")); verifyNoMoreInteractions(this.exchangeFunction); }
@Test public void defaultHeaderAndCookie() { WebClient client = this.builder .defaultHeader("Accept", "application/json").defaultCookie("id", "123") .build(); client.get().uri("/path").exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("application/json", request.headers().getFirst("Accept")); assertEquals("123", request.cookies().getFirst("id")); verifyNoMoreInteractions(this.exchangeFunction); }