Skip to content

Commit

Permalink
Explicit HEAD sorted higher than implicit GET match
Browse files Browse the repository at this point in the history
Issue: SPR-14182
  • Loading branch information
rstoyanchev committed Apr 17, 2016
1 parent ff38224 commit 7cdcc10
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Expand Up @@ -39,8 +39,8 @@
*/
public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {

private static final RequestMethodsRequestCondition HEAD_CONDITION =
new RequestMethodsRequestCondition(RequestMethod.HEAD);
private static final RequestMethodsRequestCondition GET_CONDITION =
new RequestMethodsRequestCondition(RequestMethod.GET);


private final Set<RequestMethod> methods;
Expand Down Expand Up @@ -140,7 +140,7 @@ private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue
}
}
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
return HEAD_CONDITION;
return GET_CONDITION;
}
}
return null;
Expand All @@ -159,7 +159,18 @@ private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue
*/
@Override
public int compareTo(RequestMethodsRequestCondition other, HttpServletRequest request) {
return (other.methods.size() - this.methods.size());
if (other.methods.size() != this.methods.size()) {
return other.methods.size() - this.methods.size();
}
else if (this.methods.size() == 1) {
if (this.methods.contains(RequestMethod.HEAD) && other.methods.contains(RequestMethod.GET)) {
return -1;
}
else if (this.methods.contains(RequestMethod.GET) && other.methods.contains(RequestMethod.HEAD)) {
return 1;
}
}
return 0;
}

}
Expand Up @@ -53,7 +53,7 @@ public void getMatchingCondition() {
@Test
public void getMatchingConditionWithHttpHead() {
testMatch(new RequestMethodsRequestCondition(HEAD), HEAD);
testMatch(new RequestMethodsRequestCondition(GET), HEAD);
testMatch(new RequestMethodsRequestCondition(GET), GET);
testNoMatch(new RequestMethodsRequestCondition(POST), HEAD);
}

Expand Down
Expand Up @@ -1776,6 +1776,18 @@ public void httpHead() throws ServletException, IOException {
assertEquals("body", response.getContentAsString());
}

@Test
public void httpHeadExplicit() throws ServletException, IOException {
initServletWithControllers(ResponseEntityController.class);

MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/stores");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);

assertEquals(200, response.getStatus());
assertEquals("v1", response.getHeader("h1"));
}

@Test
public void httpOptions() throws ServletException, IOException {
initServletWithControllers(ResponseEntityController.class);
Expand Down Expand Up @@ -3100,6 +3112,16 @@ public ResponseEntity<Void> bar() {
public ResponseEntity<String> baz() {
return ResponseEntity.ok().header("MyResponseHeader", "MyValue").body("body");
}

@RequestMapping(path = "/stores", method = RequestMethod.HEAD)
public ResponseEntity<Void> headResource() {
return ResponseEntity.ok().header("h1", "v1").build();
}

@RequestMapping(path = "/stores", method = RequestMethod.GET)
public ResponseEntity<String> getResource() {
return ResponseEntity.ok().body("body");
}
}

@Controller
Expand Down

0 comments on commit 7cdcc10

Please sign in to comment.