下面列出了org.springframework.http.HttpMethod#values ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initAllowHeader() {
Collection<String> allowedMethods;
if (this.supportedMethods == null) {
allowedMethods = new ArrayList<>(HttpMethod.values().length - 1);
for (HttpMethod method : HttpMethod.values()) {
if (method != HttpMethod.TRACE) {
allowedMethods.add(method.name());
}
}
}
else if (this.supportedMethods.contains(HttpMethod.OPTIONS.name())) {
allowedMethods = this.supportedMethods;
}
else {
allowedMethods = new ArrayList<>(this.supportedMethods);
allowedMethods.add(HttpMethod.OPTIONS.name());
}
this.allowHeader = StringUtils.collectionToCommaDelimitedString(allowedMethods);
}
@Test
public void wrapPutPatchAndDeleteOnly() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
request.setContent("foo=bar".getBytes("ISO-8859-1"));
request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
this.filterChain = new MockFilterChain();
this.filter.doFilter(request, this.response, this.filterChain);
if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
assertNotSame(request, this.filterChain.getRequest());
}
else {
assertSame(request, this.filterChain.getRequest());
}
}
}
public static Method build(String methodName) {
HttpMethod httpMethod = null;
try {
httpMethod = HttpMethod.valueOf(methodName.toUpperCase());
} catch (IllegalArgumentException e) {
for (HttpMethod checkMethod : HttpMethod.values()) {
if (methodName.startsWith(checkMethod.name().toLowerCase())) {
httpMethod = checkMethod;
break;
}
}
}
if (httpMethod == null) {
throw new IllegalArgumentException(methodName + " method name should start with http method (get, post, ...)");
}
return new Method(httpMethod, methodName);
}
private void initAllowHeader() {
Collection<String> allowedMethods;
if (this.supportedMethods == null) {
allowedMethods = new ArrayList<>(HttpMethod.values().length - 1);
for (HttpMethod method : HttpMethod.values()) {
if (method != HttpMethod.TRACE) {
allowedMethods.add(method.name());
}
}
}
else if (this.supportedMethods.contains(HttpMethod.OPTIONS.name())) {
allowedMethods = this.supportedMethods;
}
else {
allowedMethods = new ArrayList<>(this.supportedMethods);
allowedMethods.add(HttpMethod.OPTIONS.name());
}
this.allowHeader = StringUtils.collectionToCommaDelimitedString(allowedMethods);
}
@Test
public void wrapPutPatchAndDeleteOnly() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
request.setContent("foo=bar".getBytes("ISO-8859-1"));
request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
this.filterChain = new MockFilterChain();
this.filter.doFilter(request, this.response, this.filterChain);
if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
assertNotSame(request, this.filterChain.getRequest());
}
else {
assertSame(request, this.filterChain.getRequest());
}
}
}
private void initAllowHeader() {
Collection<String> allowedMethods;
if (this.supportedMethods == null) {
allowedMethods = new ArrayList<String>(HttpMethod.values().length - 1);
for (HttpMethod method : HttpMethod.values()) {
if (!HttpMethod.TRACE.equals(method)) {
allowedMethods.add(method.name());
}
}
}
else if (this.supportedMethods.contains(HttpMethod.OPTIONS.name())) {
allowedMethods = this.supportedMethods;
}
else {
allowedMethods = new ArrayList<String>(this.supportedMethods);
allowedMethods.add(HttpMethod.OPTIONS.name());
}
this.allowHeader = StringUtils.collectionToCommaDelimitedString(allowedMethods);
}
private void assertHttpMethod(String method) {
for (HttpMethod httpMethod : HttpMethod.values()) {
if (httpMethod.name().equalsIgnoreCase(method)) {
return;
}
}
throw new IllegalArgumentException("Invalid HTTP method: " + method);
}
@Test
public void resolvePathWithTraversal() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
this.request = new MockHttpServletRequest("GET", "");
this.response = new MockHttpServletResponse();
testResolvePathWithTraversal(method);
}
}
@Test
public void resourceNotFound() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
this.request = new MockHttpServletRequest("GET", "");
this.response = new MockHttpServletResponse();
resourceNotFound(method);
}
}
private void assertHttpMethod(String method) {
for (HttpMethod httpMethod : HttpMethod.values()) {
if (httpMethod.name().equalsIgnoreCase(method)) {
return;
}
}
throw new IllegalArgumentException("Invalid HTTP method: " + method);
}
@Test
public void resolvePathWithTraversal() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
this.request = new MockHttpServletRequest("GET", "");
this.response = new MockHttpServletResponse();
testResolvePathWithTraversal(method);
}
}
@Test
public void resourceNotFound() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
this.request = new MockHttpServletRequest("GET", "");
this.response = new MockHttpServletResponse();
resourceNotFound(method);
}
}
private void assertHttpMethod(String method) {
for (HttpMethod httpMethod : HttpMethod.values()) {
if (httpMethod.name().equalsIgnoreCase(method)) {
return;
}
}
throw new IllegalArgumentException("Invalid HTTP method: " + method);
}
private void assertHttpMethod(String method) {
for (HttpMethod httpMethod : HttpMethod.values()) {
if (httpMethod.name().equalsIgnoreCase(method)) {
return;
}
}
throw new IllegalArgumentException("Invalid HTTP method: " + method);
}
@Test
public void wrapPutAndPatchOnly() throws Exception {
request.setContent("".getBytes("ISO-8859-1"));
for (HttpMethod method : HttpMethod.values()) {
request.setMethod(method.name());
filterChain = new MockFilterChain();
filter.doFilter(request, response, filterChain);
if (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH)) {
assertNotSame("Should wrap HTTP method " + method, request, filterChain.getRequest());
}
else {
assertSame("Should not wrap for HTTP method " + method, request, filterChain.getRequest());
}
}
}
/**
* Gets the available Http request method names as list.
* @return
*/
private String[] getHttpMethodOptions() {
List<String> methodNames = new ArrayList<String>();
for (HttpMethod method : HttpMethod.values()) {
methodNames.add(method.name());
}
return methodNames.toArray(new String[methodNames.size()]);
}
@Test
public void testResolvePathWithTraversal() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
testResolvePathWithTraversal(method);
}
}
@Test
public void resourceNotFound() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
resourceNotFound(method);
}
}
@Test
public void testResolvePathWithTraversal() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
testResolvePathWithTraversal(method);
}
}
@Test
public void resourceNotFound() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
resourceNotFound(method);
}
}