Skip to content

Commit 11b4e3b

Browse files
committed
Consistent HttpMethod resolution against underlying HttpServletRequest
1 parent 90409cb commit 11b4e3b

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public <T> T getNativeResponse(Class<T> requiredType) {
106106
* @since 4.0.2
107107
*/
108108
public HttpMethod getHttpMethod() {
109-
return HttpMethod.valueOf(getRequest().getMethod().trim().toUpperCase());
109+
return HttpMethod.valueOf(getRequest().getMethod());
110110
}
111111

112112
@Override

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
*/
5858
public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodProcessor {
5959

60-
6160
/**
6261
* Basic constructor with converters only. Suitable for resolving
6362
* {@code HttpEntity}. For handling {@code ResponseEntity} consider also
@@ -169,11 +168,12 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType,
169168
if (!entityHeaders.isEmpty()) {
170169
outputMessage.getHeaders().putAll(entityHeaders);
171170
}
171+
172172
Object body = responseEntity.getBody();
173173
if (responseEntity instanceof ResponseEntity) {
174174
outputMessage.setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
175-
if ("GET".equals(inputMessage.getServletRequest().getMethod())
176-
&& isResourceNotModified(inputMessage, outputMessage)) {
175+
if (inputMessage.getMethod() == HttpMethod.GET &&
176+
isResourceNotModified(inputMessage, outputMessage)) {
177177
outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED);
178178
// Ensure headers are flushed, no body should be written.
179179
outputMessage.flush();
@@ -190,7 +190,6 @@ && isResourceNotModified(inputMessage, outputMessage)) {
190190
}
191191

192192
private boolean isResourceNotModified(ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) {
193-
194193
List<String> ifNoneMatch = inputMessage.getHeaders().getIfNoneMatch();
195194
long ifModifiedSince = inputMessage.getHeaders().getIfModifiedSince();
196195
String eTag = addEtagPadding(outputMessage.getHeaders().getETag());
@@ -212,10 +211,10 @@ else if (StringUtils.hasLength(eTag)) {
212211
private boolean isETagNotModified(List<String> ifNoneMatch, String etag) {
213212
if (StringUtils.hasLength(etag)) {
214213
for (String clientETag : ifNoneMatch) {
215-
// compare weak/strong ETags as per https://tools.ietf.org/html/rfc7232#section-2.3
214+
// Compare weak/strong ETags as per https://tools.ietf.org/html/rfc7232#section-2.3
216215
if (StringUtils.hasLength(clientETag) &&
217-
(clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", ""))
218-
|| clientETag.equals("*"))) {
216+
(clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", "")) ||
217+
clientETag.equals("*"))) {
219218
return true;
220219
}
221220
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void setup() throws Exception {
9696
this.webAppContext = new StaticWebApplicationContext();
9797
this.handlerAdapter = new RequestMappingHandlerAdapter();
9898
this.handlerAdapter.setApplicationContext(this.webAppContext);
99-
this.request = new MockHttpServletRequest();
99+
this.request = new MockHttpServletRequest("GET", "/");
100100
this.response = new MockHttpServletResponse();
101101
}
102102

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,16 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.servlet.mvc.method.annotation;
1718

1819
import java.lang.reflect.Method;
1920
import java.util.Arrays;
2021
import java.util.EnumSet;
2122
import java.util.List;
22-
import javax.servlet.http.HttpServletRequest;
2323

2424
import org.junit.Before;
2525
import org.junit.Test;
26+
import org.mockito.Mockito;
2627

2728
import org.springframework.beans.ConversionNotSupportedException;
2829
import org.springframework.beans.TypeMismatchException;
@@ -56,7 +57,6 @@
5657
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
5758

5859
import static org.junit.Assert.*;
59-
import org.mockito.Mockito;
6060

6161
/**
6262
* Test fixture for {@link ResponseEntityExceptionHandler}.
@@ -71,14 +71,14 @@ public class ResponseEntityExceptionHandlerTests {
7171

7272
private WebRequest request;
7373

74-
private HttpServletRequest servletRequest;
74+
private MockHttpServletRequest servletRequest;
7575

7676
private MockHttpServletResponse servletResponse;
7777

7878

7979
@Before
8080
public void setup() {
81-
this.servletRequest = new MockHttpServletRequest();
81+
this.servletRequest = new MockHttpServletRequest("GET", "/");
8282
this.servletResponse = new MockHttpServletResponse();
8383
this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);
8484

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.ArrayList;
2323
import java.util.Collections;
2424
import java.util.List;
25-
2625
import javax.servlet.http.HttpServletResponse;
2726

2827
import org.junit.Test;
@@ -66,7 +65,7 @@ public class ServletInvocableHandlerMethodTests {
6665

6766
private final ModelAndViewContainer mavContainer = new ModelAndViewContainer();
6867

69-
private final MockHttpServletRequest request = new MockHttpServletRequest();
68+
private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
7069

7170
private final MockHttpServletResponse response = new MockHttpServletResponse();
7271

0 commit comments

Comments
 (0)