Skip to content

Commit 0760594

Browse files
committed
Merge pull request #205 from acogoluegnes/SPR-10093
* tmp: Add support for HTTP OPTIONS in Spring MVC Test
2 parents c7d0054 + a56d8f2 commit 0760594

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/MockMvcBuilderSupport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ public abstract class MockMvcBuilderSupport {
4343

4444
protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servletConfig,
4545
WebApplicationContext webAppContext, RequestBuilder defaultRequestBuilder,
46-
List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers) {
46+
List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers, Boolean dispatchOptions) {
4747

4848
ServletContext servletContext = webAppContext.getServletContext();
4949

5050
TestDispatcherServlet dispatcherServlet = new TestDispatcherServlet(webAppContext);
51+
dispatcherServlet.setDispatchOptionsRequest(dispatchOptions);
5152
try {
5253
dispatcherServlet.init(servletConfig);
5354
}

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public static MockHttpServletRequestBuilder delete(String urlTemplate, Object...
8080
return new MockHttpServletRequestBuilder(HttpMethod.DELETE, urlTemplate, urlVariables);
8181
}
8282

83+
/**
84+
* Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request.
85+
*
86+
* @param urlTemplate a URL template; the resulting URL will be encoded
87+
* @param urlVariables zero or more URL variables
88+
*/
89+
public static MockHttpServletRequestBuilder options(String urlTemplate, Object... urlVariables) {
90+
return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, urlTemplate, urlVariables);
91+
}
92+
93+
8394
/**
8495
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
8596
*

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilder.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.test.web.servlet.ResultMatcher;
3232
import org.springframework.util.Assert;
3333
import org.springframework.web.context.WebApplicationContext;
34+
import org.springframework.web.servlet.DispatcherServlet;
3435

3536
/**
3637
* An concrete implementation of {@link MockMvcBuilder} with methods for
@@ -53,6 +54,8 @@ public class DefaultMockMvcBuilder<Self extends MockMvcBuilder> extends MockMvcB
5354
private final List<ResultMatcher> globalResultMatchers = new ArrayList<ResultMatcher>();
5455

5556
private final List<ResultHandler> globalResultHandlers = new ArrayList<ResultHandler>();
57+
58+
private Boolean dispatchOptions = Boolean.FALSE;
5659

5760

5861
/**
@@ -177,6 +180,17 @@ public final <T extends Self> T alwaysDo(ResultHandler resultHandler) {
177180
this.globalResultHandlers.add(resultHandler);
178181
return (T) this;
179182
}
183+
184+
/**
185+
* Should the {@link DispatcherServlet} dispatch OPTIONS request to controllers.
186+
* @param dispatchOptions
187+
* @see {@link DispatcherServlet#setDispatchOptionsRequest(boolean)}
188+
*/
189+
@SuppressWarnings("unchecked")
190+
public final <T extends Self> T dispatchOptions(boolean dispatchOptions) {
191+
this.dispatchOptions = dispatchOptions;
192+
return (T) this;
193+
}
180194

181195
/**
182196
* Build a {@link MockMvc} instance.
@@ -191,7 +205,7 @@ public final MockMvc build() {
191205
Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]);
192206

193207
return super.createMockMvc(filterArray, mockServletConfig, this.webAppContext,
194-
this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers);
208+
this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers,this.dispatchOptions);
195209
}
196210

197211
/**
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.test.web.servlet;
17+
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20+
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
21+
22+
import java.util.concurrent.atomic.AtomicInteger;
23+
24+
import org.junit.Assert;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.stereotype.Controller;
32+
import org.springframework.test.context.ContextConfiguration;
33+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34+
import org.springframework.test.context.web.WebAppConfiguration;
35+
import org.springframework.web.bind.annotation.RequestMapping;
36+
import org.springframework.web.bind.annotation.RequestMethod;
37+
import org.springframework.web.bind.annotation.ResponseBody;
38+
import org.springframework.web.context.WebApplicationContext;
39+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
40+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
41+
42+
/**
43+
* Tests for SPR-10093 (support for OPTIONS requests).
44+
* @author Arnaud Cogoluègnes
45+
*/
46+
@RunWith(SpringJUnit4ClassRunner.class)
47+
@WebAppConfiguration
48+
@ContextConfiguration
49+
public class Spr10093Tests {
50+
51+
@Autowired
52+
private WebApplicationContext wac;
53+
54+
private MockMvc mockMvc;
55+
56+
@Before
57+
public void setup() {
58+
this.mockMvc = webAppContextSetup(this.wac).dispatchOptions(true).build();
59+
}
60+
61+
@Test
62+
public void test() throws Exception {
63+
MyController controller = this.wac.getBean(MyController.class);
64+
int initialCount = controller.counter.get();
65+
this.mockMvc.perform(options("/myUrl")).andExpect(status().isOk());
66+
67+
Assert.assertEquals(initialCount+1,controller.counter.get());
68+
}
69+
70+
71+
@Configuration
72+
@EnableWebMvc
73+
static class WebConfig extends WebMvcConfigurerAdapter {
74+
75+
@Bean
76+
public MyController myController() {
77+
return new MyController();
78+
}
79+
}
80+
81+
@Controller
82+
private static class MyController {
83+
84+
private AtomicInteger counter = new AtomicInteger(0);
85+
86+
@RequestMapping(value="/myUrl",method=RequestMethod.OPTIONS)
87+
@ResponseBody
88+
public void handle() {
89+
counter.incrementAndGet();
90+
}
91+
}
92+
93+
94+
}

0 commit comments

Comments
 (0)