Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#addHeader()
The following examples show how to use
org.springframework.mock.web.test.MockHttpServletRequest#addHeader() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CorsFilterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void sameOriginRequest() throws ServletException, IOException { MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "https://domain1.com/test.html"); request.addHeader(HttpHeaders.ORIGIN, "https://domain1.com"); request.setScheme("https"); request.setServerName("domain1.com"); request.setServerPort(443); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain filterChain = (filterRequest, filterResponse) -> { assertNull(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); assertNull(response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS)); }; filter.doFilter(request, response, filterChain); }
Example 2
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void responseBodyArgMismatch() throws Exception { initServlet(wac -> { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(A.class, B.class); try { marshaller.afterPropertiesSet(); } catch (Exception ex) { throw new BeanCreationException(ex.getMessage(), ex); } MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller); RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); adapterDef.getPropertyValues().add("messageConverters", messageConverter); wac.registerBeanDefinition("handlerAdapter", adapterDef); }, RequestBodyArgMismatchController.class); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); String requestBody = "<b/>"; request.setContent(requestBody.getBytes("UTF-8")); request.addHeader("Content-Type", "application/xml; charset=utf-8"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals(400, response.getStatus()); }
Example 3
Source File: ProducesRequestConditionTests.java From spring-analysis-note with MIT License | 6 votes |
@Test // gh-22853 public void matchAndCompare() { ContentNegotiationManager manager = new ContentNegotiationManager( new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML)); ProducesRequestCondition none = new ProducesRequestCondition(new String[0], null, manager); ProducesRequestCondition html = new ProducesRequestCondition(new String[] {"text/html"}, null, manager); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept", "*/*"); ProducesRequestCondition noneMatch = none.getMatchingCondition(request); ProducesRequestCondition htmlMatch = html.getMatchingCondition(request); assertEquals(1, noneMatch.compareTo(htmlMatch, request)); }
Example 4
Source File: ServletAnnotationControllerHandlerMethodTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void unsupportedRequestBody() throws Exception { initServlet(wac -> { RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter()); wac.registerBeanDefinition("handlerAdapter", adapterDef); }, RequestResponseBodyController.class); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); String requestBody = "Hello World"; request.setContent(requestBody.getBytes("UTF-8")); request.addHeader("Content-Type", "application/pdf"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals(415, response.getStatus()); assertNotNull("No Accept response header set", response.getHeader("Accept")); }
Example 5
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring-analysis-note with MIT License | 6 votes |
@Test // SPR-6877 public void overlappingMessageConvertersRequestBody() throws Exception { initServlet(wac -> { RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); messageConverters.add(new StringHttpMessageConverter()); messageConverters .add(new SimpleMessageConverter(new MediaType("application","json"), MediaType.ALL)); adapterDef.getPropertyValues().add("messageConverters", messageConverters); wac.registerBeanDefinition("handlerAdapter", adapterDef); }, RequestResponseBodyController.class); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); request.setContent("Hello World".getBytes("UTF-8")); request.addHeader("Content-Type", "text/plain; charset=utf-8"); request.addHeader("Accept", "application/json, text/javascript, */*"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("Invalid content-type", "application/json;charset=ISO-8859-1", response.getHeader("Content-Type")); }
Example 6
Source File: ServletAnnotationControllerHandlerMethodTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void responseBodyNoAcceptableMediaType() throws Exception { initServlet(wac -> { RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); StringHttpMessageConverter converter = new StringHttpMessageConverter(); adapterDef.getPropertyValues().add("messageConverters", converter); wac.registerBeanDefinition("handlerAdapter", adapterDef); }, RequestResponseBodyProducesController.class); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); String requestBody = "Hello World"; request.setContent(requestBody.getBytes("UTF-8")); request.addHeader("Content-Type", "text/plain; charset=utf-8"); request.addHeader("Accept", "application/pdf, application/msword"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals(406, response.getStatus()); }
Example 7
Source File: UriComponentsBuilderTests.java From spring-analysis-note with MIT License | 5 votes |
@Test // SPR-10701 public void fromHttpRequestWithForwardedHostIncludingPort() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(-1); request.setRequestURI("/mvc-showcase"); request.addHeader("X-Forwarded-Host", "webtest.foo.bar.com:443"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("webtest.foo.bar.com", result.getHost()); assertEquals(443, result.getPort()); }
Example 8
Source File: RequestMappingInfoHandlerMappingTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test(expected=HttpMediaTypeNotAcceptableException.class) public void requestMethodMatchFalsePositive() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/users"); request.addHeader("Accept", "application/xml"); this.handlerMapping.registerHandler(new UserController()); this.handlerMapping.getHandler(request); }
Example 9
Source File: ServletAnnotationControllerHandlerMethodTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void requestBodyResponseBody() throws ServletException, IOException { initServletWithControllers(RequestResponseBodyController.class); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); String requestBody = "Hello World"; request.setContent(requestBody.getBytes("UTF-8")); request.addHeader("Content-Type", "text/plain; charset=utf-8"); request.addHeader("Accept", "text/*, */*"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals(200, response.getStatus()); assertEquals(requestBody, response.getContentAsString()); }
Example 10
Source File: ProducesRequestConditionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void compareToMediaTypeAllWithParameter() { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept", "*/*;q=0.9"); ProducesRequestCondition condition1 = new ProducesRequestCondition(); ProducesRequestCondition condition2 = new ProducesRequestCondition("application/json"); assertTrue(condition1.compareTo(condition2, request) < 0); assertTrue(condition2.compareTo(condition1, request) > 0); }
Example 11
Source File: ServletAnnotationControllerHandlerMethodTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void optionalParamPresent() throws Exception { initServletWithControllers(OptionalParamController.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); request.addParameter("id", "val"); request.addParameter("flag", "true"); request.addHeader("header", "otherVal"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals("val-true-otherVal", response.getContentAsString()); }
Example 12
Source File: UriComponentsBuilderTests.java From java-technology-stack with MIT License | 5 votes |
@Test // SPR-11872 public void fromHttpRequestWithForwardedHostWithDefaultPort() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(10080); request.addHeader("X-Forwarded-Host", "example.org"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("example.org", result.getHost()); assertEquals(-1, result.getPort()); }
Example 13
Source File: UriComponentsBuilderTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void fromHttpRequestWithForwardedHost() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(-1); request.setRequestURI("/mvc-showcase"); request.addHeader("X-Forwarded-Host", "anotherHost"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("http://anotherHost/mvc-showcase", result.toString()); }
Example 14
Source File: ServletAnnotationMappingUtilsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void checkHeadersKeyValueMatch() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); request.addHeader("header1", "value1"); String[] headers = new String[]{"header1=value1"}; boolean result = ServletAnnotationMappingUtils.checkHeaders(headers, request); assertTrue("Invalid request method result", result); }
Example 15
Source File: HeadersRequestConditionTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void headerPresentNoMatch() { HeadersRequestCondition condition = new HeadersRequestCondition("foo"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("bar", ""); assertNull(condition.getMatchingCondition(request)); }
Example 16
Source File: ParamsRequestConditionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void paramPresentNoMatch() { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("bar", ""); assertNull(new ParamsRequestCondition("foo").getMatchingCondition(request)); }
Example 17
Source File: HeadersRequestConditionTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void headerCaseSensitiveValueMatch() { HeadersRequestCondition condition = new HeadersRequestCondition("foo=Bar"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("foo", "bar"); assertNull(condition.getMatchingCondition(request)); }
Example 18
Source File: UriComponentsBuilderTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void fromHttpRequestWithForwardedHostWithDefaultPort() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(10080); request.addHeader("X-Forwarded-Host", "example.org"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("example.org", result.getHost()); assertEquals(-1, result.getPort()); }
Example 19
Source File: UriComponentsBuilderTests.java From spring-analysis-note with MIT License | 5 votes |
@Test // SPR-14761 public void fromHttpRequestWithForwardedIPv4Host() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("https"); request.setServerName("localhost"); request.setServerPort(-1); request.setRequestURI("/mvc-showcase"); request.addHeader("Forwarded", "host=192.168.0.1"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("https://192.168.0.1/mvc-showcase", result.toString()); }
Example 20
Source File: ProducesRequestConditionTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void matchSingle() { ProducesRequestCondition condition = new ProducesRequestCondition("text/plain"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept", "application/xml"); assertNull(condition.getMatchingCondition(request)); }