SPR-6934 - AnnotationMethodHandlerAdapter should take into account request accept header ordering
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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,6 +25,7 @@ import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
/**
|
||||
@@ -47,7 +48,7 @@ public class RequestMappingInfoComparatorTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchMethodException {
|
||||
comparator = new AnnotationMethodHandlerAdapter.RequestMappingInfoComparator(new MockComparator());
|
||||
comparator = new AnnotationMethodHandlerAdapter.RequestMappingInfoComparator(new MockComparator(), new MockHttpServletRequest());
|
||||
|
||||
emptyInfo = new AnnotationMethodHandlerAdapter.RequestMappingInfo();
|
||||
|
||||
@@ -85,6 +86,42 @@ public class RequestMappingInfoComparatorTests {
|
||||
assertEquals(emptyInfo, infos.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptHeaders() {
|
||||
AnnotationMethodHandlerAdapter.RequestMappingInfo html = new AnnotationMethodHandlerAdapter.RequestMappingInfo();
|
||||
html.headers = new String[] {"accept=text/html"};
|
||||
|
||||
AnnotationMethodHandlerAdapter.RequestMappingInfo xml = new AnnotationMethodHandlerAdapter.RequestMappingInfo();
|
||||
xml.headers = new String[] {"accept=application/xml"};
|
||||
|
||||
AnnotationMethodHandlerAdapter.RequestMappingInfo none = new AnnotationMethodHandlerAdapter.RequestMappingInfo();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/xml, text/html");
|
||||
comparator = new AnnotationMethodHandlerAdapter.RequestMappingInfoComparator(new MockComparator(), request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) > 0);
|
||||
assertTrue(comparator.compare(xml, html) < 0);
|
||||
assertTrue(comparator.compare(xml, none) < 0);
|
||||
assertTrue(comparator.compare(none, xml) > 0);
|
||||
assertTrue(comparator.compare(html, none) < 0);
|
||||
assertTrue(comparator.compare(none, html) > 0);
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/xml, text/*");
|
||||
comparator = new AnnotationMethodHandlerAdapter.RequestMappingInfoComparator(new MockComparator(), request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) > 0);
|
||||
assertTrue(comparator.compare(xml, html) < 0);
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept", "application/pdf");
|
||||
comparator = new AnnotationMethodHandlerAdapter.RequestMappingInfoComparator(new MockComparator(), request);
|
||||
|
||||
assertTrue(comparator.compare(html, xml) == 0);
|
||||
assertTrue(comparator.compare(xml, html) == 0);
|
||||
}
|
||||
|
||||
|
||||
private static class MockComparator implements Comparator<String> {
|
||||
|
||||
|
||||
@@ -1200,21 +1200,44 @@ public class ServletAnnotationControllerTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void headers() throws ServletException, IOException {
|
||||
initServlet(HeadersController.class);
|
||||
public void contentTypeHeaders() throws ServletException, IOException {
|
||||
initServlet(ContentTypeHeadersController.class);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/something");
|
||||
request.addHeader("Content-Type", "application/pdf");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("pdf", response.getContentAsString());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/something");
|
||||
request = new MockHttpServletRequest("POST", "/something");
|
||||
request.addHeader("Content-Type", "text/html");
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("text", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptHeaders() throws ServletException, IOException {
|
||||
initServlet(AcceptHeadersController.class);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
|
||||
request.addHeader("Accept", "text/html");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("html", response.getContentAsString());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/something");
|
||||
request.addHeader("Accept", "application/xml");
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("xml", response.getContentAsString());
|
||||
|
||||
request = new MockHttpServletRequest("GET", "/something");
|
||||
request.addHeader("Accept", "application/xml, text/html");
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("xml", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseStatus() throws ServletException, IOException {
|
||||
@@ -2235,7 +2258,7 @@ public class ServletAnnotationControllerTests {
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class HeadersController {
|
||||
public static class ContentTypeHeadersController {
|
||||
|
||||
@RequestMapping(value = "/something", headers = "content-type=application/pdf")
|
||||
public void handlePdf(Writer writer) throws IOException {
|
||||
@@ -2248,6 +2271,20 @@ public class ServletAnnotationControllerTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class AcceptHeadersController {
|
||||
|
||||
@RequestMapping(value = "/something", headers = "accept=text/html")
|
||||
public void handleHtml(Writer writer) throws IOException {
|
||||
writer.write("html");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/something", headers = "accept=application/xml")
|
||||
public void handleXml(Writer writer) throws IOException {
|
||||
writer.write("xml");
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class ResponseStatusController {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user