001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.servlet.request;
018
019import java.net.URI;
020import javax.servlet.DispatcherType;
021import javax.servlet.ServletContext;
022
023import org.springframework.http.HttpMethod;
024import org.springframework.mock.web.MockHttpServletRequest;
025import org.springframework.test.web.servlet.MvcResult;
026import org.springframework.test.web.servlet.RequestBuilder;
027
028/**
029 * Static factory methods for {@link RequestBuilder RequestBuilders}.
030 *
031 * <h3>Integration with the Spring TestContext Framework</h3>
032 * <p>Methods in this class will reuse a
033 * {@link org.springframework.mock.web.MockServletContext MockServletContext}
034 * that was created by the Spring TestContext Framework.
035 *
036 * <h3>Eclipse Users</h3>
037 * <p>Consider adding this class as a Java editor favorite. To navigate to
038 * this setting, open the Preferences and type "favorites".
039 *
040 * @author Arjen Poutsma
041 * @author Rossen Stoyanchev
042 * @author Greg Turnquist
043 * @author Sebastien Deleuze
044 * @author Sam Brannen
045 * @author Kamill Sokol
046 * @since 3.2
047 */
048public abstract class MockMvcRequestBuilders {
049
050        /**
051         * Create a {@link MockHttpServletRequestBuilder} for a GET request.
052         * @param urlTemplate a URL template; the resulting URL will be encoded
053         * @param uriVars zero or more URI variables
054         */
055        public static MockHttpServletRequestBuilder get(String urlTemplate, Object... uriVars) {
056                return new MockHttpServletRequestBuilder(HttpMethod.GET, urlTemplate, uriVars);
057        }
058
059        /**
060         * Create a {@link MockHttpServletRequestBuilder} for a GET request.
061         * @param uri the URL
062         * @since 4.0.3
063         */
064        public static MockHttpServletRequestBuilder get(URI uri) {
065                return new MockHttpServletRequestBuilder(HttpMethod.GET, uri);
066        }
067
068        /**
069         * Create a {@link MockHttpServletRequestBuilder} for a POST request.
070         * @param urlTemplate a URL template; the resulting URL will be encoded
071         * @param uriVars zero or more URI variables
072         */
073        public static MockHttpServletRequestBuilder post(String urlTemplate, Object... uriVars) {
074                return new MockHttpServletRequestBuilder(HttpMethod.POST, urlTemplate, uriVars);
075        }
076
077        /**
078         * Create a {@link MockHttpServletRequestBuilder} for a POST request.
079         * @param uri the URL
080         * @since 4.0.3
081         */
082        public static MockHttpServletRequestBuilder post(URI uri) {
083                return new MockHttpServletRequestBuilder(HttpMethod.POST, uri);
084        }
085
086        /**
087         * Create a {@link MockHttpServletRequestBuilder} for a PUT request.
088         * @param urlTemplate a URL template; the resulting URL will be encoded
089         * @param uriVars zero or more URI variables
090         */
091        public static MockHttpServletRequestBuilder put(String urlTemplate, Object... uriVars) {
092                return new MockHttpServletRequestBuilder(HttpMethod.PUT, urlTemplate, uriVars);
093        }
094
095        /**
096         * Create a {@link MockHttpServletRequestBuilder} for a PUT request.
097         * @param uri the URL
098         * @since 4.0.3
099         */
100        public static MockHttpServletRequestBuilder put(URI uri) {
101                return new MockHttpServletRequestBuilder(HttpMethod.PUT, uri);
102        }
103
104        /**
105         * Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
106         * @param urlTemplate a URL template; the resulting URL will be encoded
107         * @param uriVars zero or more URI variables
108         */
109        public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... uriVars) {
110                return new MockHttpServletRequestBuilder(HttpMethod.PATCH, urlTemplate, uriVars);
111        }
112
113        /**
114         * Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
115         * @param uri the URL
116         * @since 4.0.3
117         */
118        public static MockHttpServletRequestBuilder patch(URI uri) {
119                return new MockHttpServletRequestBuilder(HttpMethod.PATCH, uri);
120        }
121
122        /**
123         * Create a {@link MockHttpServletRequestBuilder} for a DELETE request.
124         * @param urlTemplate a URL template; the resulting URL will be encoded
125         * @param uriVars zero or more URI variables
126         */
127        public static MockHttpServletRequestBuilder delete(String urlTemplate, Object... uriVars) {
128                return new MockHttpServletRequestBuilder(HttpMethod.DELETE, urlTemplate, uriVars);
129        }
130
131        /**
132         * Create a {@link MockHttpServletRequestBuilder} for a DELETE request.
133         * @param uri the URL
134         * @since 4.0.3
135         */
136        public static MockHttpServletRequestBuilder delete(URI uri) {
137                return new MockHttpServletRequestBuilder(HttpMethod.DELETE, uri);
138        }
139
140        /**
141         * Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request.
142         * @param urlTemplate a URL template; the resulting URL will be encoded
143         * @param uriVars zero or more URI variables
144         */
145        public static MockHttpServletRequestBuilder options(String urlTemplate, Object... uriVars) {
146                return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, urlTemplate, uriVars);
147        }
148
149        /**
150         * Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request.
151         * @param uri the URL
152         * @since 4.0.3
153         */
154        public static MockHttpServletRequestBuilder options(URI uri) {
155                return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, uri);
156        }
157
158        /**
159         * Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
160         * @param urlTemplate a URL template; the resulting URL will be encoded
161         * @param uriVars zero or more URI variables
162         * @since 4.1
163         */
164        public static MockHttpServletRequestBuilder head(String urlTemplate, Object... uriVars) {
165                return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, uriVars);
166        }
167
168        /**
169         * Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
170         * @param uri the URL
171         * @since 4.1
172         */
173        public static MockHttpServletRequestBuilder head(URI uri) {
174                return new MockHttpServletRequestBuilder(HttpMethod.HEAD, uri);
175        }
176
177        /**
178         * Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
179         * @param method the HTTP method (GET, POST, etc)
180         * @param urlTemplate a URL template; the resulting URL will be encoded
181         * @param uriVars zero or more URI variables
182         */
183        public static MockHttpServletRequestBuilder request(HttpMethod method, String urlTemplate, Object... uriVars) {
184                return new MockHttpServletRequestBuilder(method, urlTemplate, uriVars);
185        }
186
187        /**
188         * Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
189         * @param httpMethod the HTTP method (GET, POST, etc)
190         * @param uri the URL
191         * @since 4.0.3
192         */
193        public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, URI uri) {
194                return new MockHttpServletRequestBuilder(httpMethod, uri);
195        }
196
197        /**
198         * Alternative factory method that allows for custom HTTP verbs (e.g. WebDAV).
199         * @param httpMethod the HTTP method
200         * @param uri the URL
201         * @since 4.3
202         */
203        public static MockHttpServletRequestBuilder request(String httpMethod, URI uri) {
204                return new MockHttpServletRequestBuilder(httpMethod, uri);
205        }
206
207        /**
208         * Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
209         * @param urlTemplate a URL template; the resulting URL will be encoded
210         * @param uriVars zero or more URI variables
211         */
212        public static MockMultipartHttpServletRequestBuilder fileUpload(String urlTemplate, Object... uriVars) {
213                return new MockMultipartHttpServletRequestBuilder(urlTemplate, uriVars);
214        }
215
216        /**
217         * Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
218         * @param uri the URL
219         * @since 4.0.3
220         */
221        public static MockMultipartHttpServletRequestBuilder fileUpload(URI uri) {
222                return new MockMultipartHttpServletRequestBuilder(uri);
223        }
224
225
226        /**
227         * Create a {@link RequestBuilder} for an async dispatch from the
228         * {@link MvcResult} of the request that started async processing.
229         * <p>Usage involves performing a request that starts async processing first:
230         * <pre class="code">
231         * MvcResult mvcResult = this.mockMvc.perform(get("/1"))
232         *      .andExpect(request().asyncStarted())
233         *      .andReturn();
234         *  </pre>
235         * <p>And then performing the async dispatch re-using the {@code MvcResult}:
236         * <pre class="code">
237         * this.mockMvc.perform(asyncDispatch(mvcResult))
238         *      .andExpect(status().isOk())
239         *      .andExpect(content().contentType(MediaType.APPLICATION_JSON))
240         *      .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
241         * </pre>
242         * @param mvcResult the result from the request that started async processing
243         */
244        public static RequestBuilder asyncDispatch(final MvcResult mvcResult) {
245
246                // There must be an async result before dispatching
247                mvcResult.getAsyncResult();
248
249                return new RequestBuilder() {
250                        @Override
251                        public MockHttpServletRequest buildRequest(ServletContext servletContext) {
252                                MockHttpServletRequest request = mvcResult.getRequest();
253                                request.setDispatcherType(DispatcherType.ASYNC);
254                                request.setAsyncStarted(false);
255                                return request;
256                        }
257                };
258        }
259
260}