/** * Set the (new) value of the {@code If-None-Match} header. */ public void setIfNoneMatch(String ifNoneMatch) { set(IF_NONE_MATCH, ifNoneMatch); }
@Test public void exchangePost() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<>(helloWorld, requestHeaders); HttpEntity<Void> result = template.exchange(baseUrl + "/{method}", POST, entity, Void.class, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); assertFalse(result.hasBody()); }
@Test public void date() { Calendar calendar = new GregorianCalendar(2008, 11, 18, 11, 20); calendar.setTimeZone(TimeZone.getTimeZone("CET")); long date = calendar.getTimeInMillis(); headers.setDate(date); assertEquals("Invalid Date header", date, headers.getDate()); assertEquals("Invalid Date header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("date")); // RFC 850 headers.set("Date", "Thu, 18 Dec 2008 10:20:00 GMT"); assertEquals("Invalid Date header", date, headers.getDate()); }
/** * Set the length of the body in bytes, as specified by the * {@code Content-Length} header. */ public void setContentLength(long contentLength) { set(CONTENT_LENGTH, Long.toString(contentLength)); }
/** * Set the given date under the given header name after formatting it as a string * using the RFC-1123 date-time formatter. The equivalent of * {@link #set(String, String)} but for date headers. * @since 5.0 */ public void setZonedDateTime(String headerName, ZonedDateTime date) { set(headerName, DATE_FORMATTERS[0].format(date)); }
/** * Set the (new) value of the {@code Access-Control-Allow-Credentials} response header. */ public void setAccessControlAllowCredentials(boolean allowCredentials) { set(ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.toString(allowCredentials)); }
/** * Set the (new) value of the {@code Connection} header. */ public void setConnection(List<String> connection) { set(CONNECTION, toCommaDelimitedString(connection)); }
/** * Set the request header names (e.g. "Accept-Language") for which the * response is subject to content negotiation and variances based on the * value of those request headers. * @param requestHeaders the request header names * @since 4.3 */ public void setVary(List<String> requestHeaders) { set(VARY, toCommaDelimitedString(requestHeaders)); }
/** * Set the set of allowed {@link HttpMethod HTTP methods}, * as specified by the {@code Allow} header. */ public void setAllow(Set<HttpMethod> allowedMethods) { set(ALLOW, StringUtils.collectionToCommaDelimitedString(allowedMethods)); }
/** * Sets the (new) value of the {@code Range} header. */ public void setRange(List<HttpRange> ranges) { String value = HttpRange.toString(ranges); set(RANGE, value); }
@Override public void setSessionId(ServerWebExchange exchange, String id) { Assert.notNull(id, "'id' is required."); exchange.getResponse().getHeaders().set(getHeaderName(), id); }
@Override protected ResponseEntity<Object> handleServletRequestBindingException( ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { headers.set("someHeader", "someHeaderValue"); return handleExceptionInternal(ex, "error content", headers, status, request); } }
@GetMapping("cookie-echo") ResponseEntity<Void> handleCookie(@CookieValue("k1") String cookieValue) { HttpHeaders headers = new HttpHeaders(); headers.set("Set-Cookie", "k1=" + cookieValue); return new ResponseEntity<>(headers, HttpStatus.OK); } }
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) { if (logger.isErrorEnabled()) { String version = request.getHeaders().getFirst("Sec-WebSocket-Version"); logger.error("Handshake failed due to unsupported WebSocket version: " + version + ". Supported versions: " + Arrays.toString(getSupportedVersions())); } response.setStatusCode(HttpStatus.UPGRADE_REQUIRED); response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION, StringUtils.arrayToCommaDelimitedString(getSupportedVersions())); }
@Test public void exchangeGet() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); HttpEntity<String> requestEntity = new HttpEntity<>(requestHeaders); ResponseEntity<String> response = template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get"); assertEquals("Invalid content", helloWorld, response.getBody()); }
@Test public void acceptCharsetWildcard() { headers.set("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); assertEquals("Invalid Accept header", Arrays.asList(StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8), headers.getAcceptCharset()); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void exchangeGetCallbackWithLambdas() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); HttpEntity<?> requestEntity = new HttpEntity(requestHeaders); ListenableFuture<ResponseEntity<String>> responseFuture = template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get"); responseFuture.addCallback(result -> assertEquals("Invalid content", helloWorld, result.getBody()), ex -> fail(ex.getMessage())); waitTillDone(responseFuture); }
@Test // SPR-10648 (example is from INT-3063) public void expiresInvalidDate() { headers.set("Expires", "-1"); assertEquals(-1, headers.getExpires()); }
@Test public void preFlightRequestWithCorsRestricted() throws Exception { this.headers.set(HttpHeaders.ORIGIN, "http://foo"); this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET"); ResponseEntity<String> entity = performOptions("/cors-restricted", this.headers, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("http://foo", entity.getHeaders().getAccessControlAllowOrigin()); assertThat(entity.getHeaders().getAccessControlAllowMethods(), contains(HttpMethod.GET, HttpMethod.POST)); }
@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")); }