Support HTTP OPTIONS
Issue: SPR-13130
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -862,6 +862,7 @@ public class DispatcherServletTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "OPTIONS", "/foo");
|
||||
MockHttpServletResponse response = spy(new MockHttpServletResponse());
|
||||
DispatcherServlet servlet = new DispatcherServlet();
|
||||
servlet.setDispatchOptionsRequest(false);
|
||||
servlet.service(request, response);
|
||||
verify(response, never()).getHeader(anyString()); // SPR-10341
|
||||
assertThat(response.getHeader("Allow"), equalTo("GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH"));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -1966,6 +1966,17 @@ public class ServletAnnotationControllerTests {
|
||||
assertEquals("1-2", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpOptions() throws ServletException, IOException {
|
||||
initServlet(ResponseEntityController.class);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals(404, response.getStatus());
|
||||
}
|
||||
|
||||
|
||||
public static class ListEditorRegistrar implements PropertyEditorRegistrar {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -82,12 +82,13 @@ public class RequestMethodsRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noDeclaredMethodsMatchesAllMethods() {
|
||||
public void noDeclaredMethodsMatchesAllMethodsExceptOptions() {
|
||||
RequestCondition condition = new RequestMethodsRequestCondition();
|
||||
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("GET", "")));
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("POST", "")));
|
||||
assertNotNull(condition.getMatchingCondition(new MockHttpServletRequest("HEAD", "")));
|
||||
assertNull(condition.getMatchingCondition(new MockHttpServletRequest("OPTIONS", "")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -44,8 +45,11 @@ import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.method.support.InvocableHandlerMethod;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
@@ -172,6 +176,14 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
testHttpMediaTypeNotSupportedException("/person/1.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHandlerHttpOptions() throws Exception {
|
||||
testHttpOptions("/foo", "GET,HEAD");
|
||||
testHttpOptions("/person/1", "PUT");
|
||||
testHttpOptions("/persons", "GET,HEAD");
|
||||
testHttpOptions("/something", "PUT,POST");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHandlerTestInvalidContentType() throws Exception {
|
||||
try {
|
||||
@@ -388,6 +400,19 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
}
|
||||
}
|
||||
|
||||
private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", requestURI);
|
||||
HandlerMethod handlerMethod = getHandler(request);
|
||||
|
||||
ServletWebRequest webRequest = new ServletWebRequest(request);
|
||||
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
|
||||
Object result = new InvocableHandlerMethod(handlerMethod).invokeForRequest(webRequest, mavContainer);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(HttpHeaders.class, result.getClass());
|
||||
assertEquals(allowHeader, ((HttpHeaders) result).getFirst("Allow"));
|
||||
}
|
||||
|
||||
private void testHttpMediaTypeNotAcceptableException(String url) throws Exception {
|
||||
try {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
|
||||
@@ -468,6 +493,13 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
public String nonXmlContent() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/something", method = RequestMethod.OPTIONS)
|
||||
public HttpHeaders fooOptions() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Allow", "PUT,POST");
|
||||
return headers;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
@@ -1771,6 +1771,19 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
||||
assertEquals("body", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpOptions() throws ServletException, IOException {
|
||||
initServletWithControllers(ResponseEntityController.class);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/baz");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals("GET,HEAD", response.getHeader("Allow"));
|
||||
assertTrue(response.getContentAsByteArray().length == 0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Controllers
|
||||
@@ -3059,7 +3072,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
||||
return ResponseEntity.notFound().header("MyResponseHeader", "MyValue").build();
|
||||
}
|
||||
|
||||
@RequestMapping("/baz")
|
||||
@RequestMapping(path = "/baz", method = RequestMethod.GET)
|
||||
public ResponseEntity<String> baz() {
|
||||
return ResponseEntity.ok().header("MyResponseHeader", "MyValue").body("body");
|
||||
}
|
||||
|
||||
@@ -97,6 +97,31 @@ public class ResourceHttpRequestHandlerTests {
|
||||
assertEquals("h1 { color:red; }", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getResourceHttpHeader() throws Exception {
|
||||
this.request.setMethod("HEAD");
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
|
||||
this.handler.handleRequest(this.request, this.response);
|
||||
|
||||
assertEquals(200, this.response.getStatus());
|
||||
assertEquals("text/css", this.response.getContentType());
|
||||
assertEquals(17, this.response.getContentLength());
|
||||
assertEquals("max-age=3600", this.response.getHeader("Cache-Control"));
|
||||
assertTrue(this.response.containsHeader("Last-Modified"));
|
||||
assertEquals(this.response.getHeader("Last-Modified"), resourceLastModifiedDate("test/foo.css"));
|
||||
assertEquals(0, this.response.getContentAsByteArray().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getResourceHttpOptions() throws Exception {
|
||||
this.request.setMethod("OPTIONS");
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
|
||||
this.handler.handleRequest(this.request, this.response);
|
||||
|
||||
assertEquals(200, this.response.getStatus());
|
||||
assertEquals("GET,HEAD", this.response.getHeader("Allow"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getResourceNoCache() throws Exception {
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
|
||||
|
||||
Reference in New Issue
Block a user