提交 df223eb1 编写于 作者: R Rossen Stoyanchev

Polish custom HTTP verb in Spring MVC Test

Issue: SPR-13719
上级 35543489
/*
* 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.
......@@ -113,6 +113,11 @@ public class MockHttpServletRequestBuilder
/**
* Package private constructor. To get an instance, use static factory
* methods in {@link MockMvcRequestBuilders}.
* <p>Although this class cannot be extended, additional ways to initialize
* the {@code MockHttpServletRequest} can be plugged in via
* {@link #with(RequestPostProcessor)}.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url a URL template; the resulting URL will be encoded
* @param vars zero or more URL variables
......@@ -122,25 +127,18 @@ public class MockHttpServletRequestBuilder
}
/**
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url a URL template; the resulting URL will be encoded
* @param vars zero or more URL variables
* @since 4.3
*/
MockHttpServletRequestBuilder(String httpMethod, String url, Object... vars) {
this(httpMethod, UriComponentsBuilder.fromUriString(url).buildAndExpand(vars).encode().toUri());
}
/**
* Alternative to {@link #MockHttpServletRequestBuilder(HttpMethod, String, Object...)}
* with a pre-built URI.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url the URL
* @since 4.0.3
*/
MockHttpServletRequestBuilder(HttpMethod httpMethod, URI url) {
this(httpMethod.name(), url);
this(httpMethod.name(), url);
}
/**
* Alternative constructor for custom HTTP methods.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param url the URL
* @since 4.3
......
/*
* 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.
......@@ -50,10 +50,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a GET request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockHttpServletRequestBuilder get(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.GET, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder get(String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(HttpMethod.GET, urlTemplate, urlVars);
}
/**
......@@ -68,10 +68,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a POST request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockHttpServletRequestBuilder post(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.POST, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder post(String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(HttpMethod.POST, urlTemplate, urlVars);
}
/**
......@@ -86,10 +86,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a PUT request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockHttpServletRequestBuilder put(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.PUT, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder put(String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(HttpMethod.PUT, urlTemplate, urlVars);
}
/**
......@@ -104,10 +104,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, urlTemplate, urlVars);
}
/**
......@@ -122,10 +122,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a DELETE request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockHttpServletRequestBuilder delete(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.DELETE, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder delete(String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(HttpMethod.DELETE, urlTemplate, urlVars);
}
/**
......@@ -140,10 +140,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for an OPTIONS request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockHttpServletRequestBuilder options(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder options(String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(HttpMethod.OPTIONS, urlTemplate, urlVars);
}
/**
......@@ -158,11 +158,11 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a HEAD request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
* @since 4.1
*/
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder head(String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(HttpMethod.HEAD, urlTemplate, urlVars);
}
/**
......@@ -176,23 +176,12 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param method the HTTP method (GET, POST, etc)
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockHttpServletRequestBuilder request(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(httpMethod, urlTemplate, urlVariables);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
* @param httpMethod the HTTP method (GET, POST, etc)
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @since 4.3
*/
public static MockHttpServletRequestBuilder request(String httpMethod, String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(httpMethod, urlTemplate, urlVariables);
public static MockHttpServletRequestBuilder request(HttpMethod method, String urlTemplate, Object... urlVars) {
return new MockHttpServletRequestBuilder(method, urlTemplate, urlVars);
}
/**
......@@ -206,8 +195,8 @@ public abstract class MockMvcRequestBuilders {
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a request with the given HTTP method.
* @param httpMethod the HTTP method (GET, POST, etc)
* Alternative factory method that allows for custom HTTP verbs (e.g. WebDAV).
* @param httpMethod the HTTP method
* @param uri the URL
* @since 4.3
*/
......@@ -218,10 +207,10 @@ public abstract class MockMvcRequestBuilders {
/**
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
* @param urlVars zero or more URL variables
*/
public static MockMultipartHttpServletRequestBuilder fileUpload(String urlTemplate, Object... urlVariables) {
return new MockMultipartHttpServletRequestBuilder(urlTemplate, urlVariables);
public static MockMultipartHttpServletRequestBuilder fileUpload(String urlTemplate, Object... urlVars) {
return new MockMultipartHttpServletRequestBuilder(urlTemplate, urlVars);
}
/**
......
/*
* 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.
......@@ -25,7 +25,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
......@@ -43,8 +42,12 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.support.SessionFlashMapManager;
import org.springframework.web.util.UriComponentsBuilder;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* Unit tests for building a {@link MockHttpServletRequest} with
......@@ -429,7 +432,7 @@ public class MockHttpServletRequestBuilderTests {
@Test
public void sessionAttributes() {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
this.builder.sessionAttrs(map);
......@@ -471,9 +474,8 @@ public class MockHttpServletRequestBuilderTests {
assertEquals(user, request.getUserPrincipal());
}
/**
* See SPR-12945
*/
// SPR-12945
@Test
public void mergeInvokesDefaultRequestPostProcessorFirst() {
final String ATTR = "ATTR";
......@@ -494,17 +496,13 @@ public class MockHttpServletRequestBuilderTests {
assertEquals(EXEPCTED, request.getAttribute(ATTR));
}
/**
* See SPR-13719
*/
// SPR-13719
@Test
public void arbitraryMethod() {
/*
* http method is case-sensitive
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1
*/
String httpMethod = "REPort";
this.builder = new MockHttpServletRequestBuilder(httpMethod, "/foo/{bar}", 42);
URI url = UriComponentsBuilder.fromPath("/foo/{bar}").buildAndExpand(42).toUri();
this.builder = new MockHttpServletRequestBuilder(httpMethod, url);
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册