下面列出了org.springframework.http.HttpHeaders#setAllow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void optionsForAllow() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.OPTIONS)).willReturn(request);
given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(false);
HttpHeaders responseHeaders = new HttpHeaders();
EnumSet<HttpMethod> expected = EnumSet.of(HttpMethod.GET, HttpMethod.POST);
responseHeaders.setAllow(expected);
given(response.getHeaders()).willReturn(responseHeaders);
HttpStatus status = HttpStatus.OK;
given(response.getStatusCode()).willReturn(status);
given(response.getStatusText()).willReturn(status.getReasonPhrase());
Set<HttpMethod> result = template.optionsForAllow("http://example.com");
assertEquals("Invalid OPTIONS result", expected, result);
verify(response).close();
}
@API(status = INTERNAL)
@ExceptionHandler
default Mono<ResponseEntity<Problem>> handleRequestMethodNotSupportedException(
final MethodNotAllowedException exception,
final ServerWebExchange request) {
final Set<HttpMethod> methods = exception.getSupportedMethods();
if (methods.isEmpty()) {
return create(Status.METHOD_NOT_ALLOWED, exception, request);
}
final HttpHeaders headers = new HttpHeaders();
headers.setAllow(methods);
return create(Status.METHOD_NOT_ALLOWED, exception, request, headers);
}
@API(status = INTERNAL)
@ExceptionHandler
default ResponseEntity<Problem> handleRequestMethodNotSupportedException(
final HttpRequestMethodNotSupportedException exception,
final NativeWebRequest request) {
@WTF
@Facepalm("Nullable arrays... great work from Spring :/")
@Nullable final String[] methods = exception.getSupportedMethods();
if (methods == null || methods.length == 0) {
return create(Status.METHOD_NOT_ALLOWED, exception, request);
}
final HttpHeaders headers = new HttpHeaders();
headers.setAllow(requireNonNull(exception.getSupportedHttpMethods()));
return create(Status.METHOD_NOT_ALLOWED, exception, request, headers);
}
@Test
public void httpHeaders() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAllow(new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS)));
MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
HandlerResult result = handlerResult(headers, returnType);
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
assertEquals(1, exchange.getResponse().getHeaders().size());
assertEquals("GET,POST,OPTIONS", exchange.getResponse().getHeaders().getFirst("Allow"));
assertResponseBodyIsEmpty(exchange);
}
/**
* Customize the response for HttpRequestMethodNotSupportedException.
* <p>This method logs a warning, sets the "Allow" header, and delegates to
* {@link #handleExceptionInternal}.
* @param ex the exception
* @param headers the headers to be written to the response
* @param status the selected response status
* @param request the current request
* @return a {@code ResponseEntity} instance
*/
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
pageNotFoundLogger.warn(ex.getMessage());
Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
if (!CollectionUtils.isEmpty(supportedMethods)) {
headers.setAllow(supportedMethods);
}
return handleExceptionInternal(ex, null, headers, status, request);
}
@Test
public void optionsForAllow() throws Exception {
mockSentRequest(OPTIONS, "https://example.com");
mockResponseStatus(HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
EnumSet<HttpMethod> expected = EnumSet.of(GET, POST);
responseHeaders.setAllow(expected);
given(response.getHeaders()).willReturn(responseHeaders);
Set<HttpMethod> result = template.optionsForAllow("https://example.com");
assertEquals("Invalid OPTIONS result", expected, result);
verify(response).close();
}
@Test
public void httpHeaders() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAllow(new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS)));
MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
HandlerResult result = handlerResult(headers, returnType);
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
assertEquals(1, exchange.getResponse().getHeaders().size());
assertEquals("GET,POST,OPTIONS", exchange.getResponse().getHeaders().getFirst("Allow"));
assertResponseBodyIsEmpty(exchange);
}
/**
* Customize the response for HttpRequestMethodNotSupportedException.
* <p>This method logs a warning, sets the "Allow" header, and delegates to
* {@link #handleExceptionInternal}.
* @param ex the exception
* @param headers the headers to be written to the response
* @param status the selected response status
* @param request the current request
* @return a {@code ResponseEntity} instance
*/
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
pageNotFoundLogger.warn(ex.getMessage());
Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
if (!CollectionUtils.isEmpty(supportedMethods)) {
headers.setAllow(supportedMethods);
}
return handleExceptionInternal(ex, null, headers, status, request);
}
@Test
public void optionsForAllow() throws Exception {
mockSentRequest(OPTIONS, "http://example.com");
mockResponseStatus(HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
EnumSet<HttpMethod> expected = EnumSet.of(GET, POST);
responseHeaders.setAllow(expected);
given(response.getHeaders()).willReturn(responseHeaders);
Set<HttpMethod> result = template.optionsForAllow("http://example.com");
assertEquals("Invalid OPTIONS result", expected, result);
verify(response).close();
}
/**
* Customize the response for HttpRequestMethodNotSupportedException.
* <p>This method logs a warning, sets the "Allow" header, and delegates to
* {@link #handleExceptionInternal}.
* @param ex the exception
* @param headers the headers to be written to the response
* @param status the selected response status
* @param request the current request
* @return a {@code ResponseEntity} instance
*/
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
pageNotFoundLogger.warn(ex.getMessage());
Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
if (!CollectionUtils.isEmpty(supportedMethods)) {
headers.setAllow(supportedMethods);
}
return handleExceptionInternal(ex, null, headers, status, request);
}
/**
* Customize the response for HttpRequestMethodNotSupportedException.
* <p>This method logs a warning, sets the "Allow" header, and delegates to
* {@link #handleExceptionInternal}.
* @param ex the exception
* @param headers the headers to be written to the response
* @param status the selected response status
* @param request the current request
* @return a {@code ResponseEntity} instance
*/
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
pageNotFoundLogger.warn(ex.getMessage());
Set<HttpMethod> supportedMethods = ex.getSupportedHttpMethods();
if (!supportedMethods.isEmpty()) {
headers.setAllow(supportedMethods);
}
return handleExceptionInternal(ex, null, headers, status, request);
}
@RequestMapping(value = "/api", method = RequestMethod.OPTIONS)
public HttpEntity<String> simpleOptions() {
HttpHeaders headers = new HttpHeaders();
Set<HttpMethod> allowedOps = new HashSet<>();
allowedOps.add(HttpMethod.GET);
allowedOps.add(HttpMethod.POST);
allowedOps.add(HttpMethod.PUT);
allowedOps.add(HttpMethod.DELETE);
allowedOps.add(HttpMethod.PATCH);
allowedOps.add(HttpMethod.OPTIONS);
headers.setAllow(allowedOps);
return new HttpEntity<>("Hello World", headers);
}
@Override
protected HttpHeaders createHeaders(HttpRequestMethodNotSupportedException ex, HttpServletRequest req) {
HttpHeaders headers = super.createHeaders(ex, req);
if (!isEmpty(ex.getSupportedMethods())) {
headers.setAllow(ex.getSupportedHttpMethods());
}
return headers;
}