if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) && !this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) { setContentType(value.toString());
private void contentType(MockHttpServletRequest request) { String contentType = header("Content-Type"); if (contentType == null) { FormEncodingType encodingType = this.webRequest.getEncodingType(); if (encodingType != null) { contentType = encodingType.getName(); } } request.setContentType(contentType != null ? contentType : MediaType.ALL_VALUE); }
request.setContentType(this.contentType);
@Test public void resolveWhenFormParameterIsPresentAndSupportedThenTokenIsResolved() { this.resolver.setAllowFormEncodedBodyParameter(true); MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); request.setContentType("application/x-www-form-urlencoded"); request.addParameter("access_token", TEST_TOKEN); assertThat(this.resolver.resolve(request)).isEqualTo(TEST_TOKEN); }
protected MockHttpServletResponse postAsServletResponse( String path, String body, String contentType, String charset) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("POST"); request.setContentType(contentType); request.setContent(body.getBytes("UTF-8")); return dispatch(request, charset); }
/** * Executes an ows request using the POST method with key value pairs form encoded. * * @param path The porition of the request after hte context, example: * 'wms?request=GetMap&version=1.1.1&..." * @return An input stream which is the result of the request. */ protected InputStream post(String path) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("POST"); request.setContentType("application/x-www-form-urlencoded"); request.setContent(new byte[] {}); MockHttpServletResponse response = dispatch(request); return new ByteArrayInputStream(response.getContentAsString().getBytes()); }
/** * Executes an ows request using the POST method with key value pairs form encoded. * * @param path The porition of the request after hte context, example: * 'wms?request=GetMap&version=1.1.1&..." * @return An input stream which is the result of the request. */ protected InputStream post(String path) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("POST"); request.setContentType("application/x-www-form-urlencoded"); request.setContent(new byte[] {}); MockHttpServletResponse response = dispatch(request); return new ByteArrayInputStream(response.getContentAsString().getBytes()); }
@Test public void testEmptyPost() throws Exception { MockHttpServletRequest req = makeRequest("", ""); // reset the content type req.setContentType(null); BufferedReader br = req.getReader(); while ((br.readLine()) != null) { /* clear out the body */ } // should not NPE like it did BufferedRequestWrapper wrapper = new BufferedRequestWrapper(req, "UTF-8", "".getBytes()); Map params = wrapper.getParameterMap(); assertEquals(0, params.size()); } }
@Test public void testNoContentType() throws Exception { String body = "a=1&b=2"; String queryString = "c=3&d=4"; MockHttpServletRequest req = makeRequest(body, queryString); // reset the content type req.setContentType(null); BufferedReader br = req.getReader(); while ((br.readLine()) != null) { /* clear out the body */ } // should not NPE like it did BufferedRequestWrapper wrapper = new BufferedRequestWrapper(req, "UTF-8", body.getBytes()); Map params = wrapper.getParameterMap(); assertEquals(0, params.size()); }
@Test public void setContentTypeUTF8() { String contentType = "test/plain;charset=UTF-8"; request.setContentType(contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader(HttpHeaders.CONTENT_TYPE)); assertEquals("UTF-8", request.getCharacterEncoding()); }
/** * Executes an ows request using the POST method, with xml as body content. * * @param path The porition of the request after the context ( no query string ), example: * 'wms'. * @param xml The body content, often xml for OGC services * @param contentType * @return the servlet response */ protected MockHttpServletResponse postAsServletResponse( String path, String body, String contentType) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("POST"); request.setContentType(contentType); request.setContent(body.getBytes("UTF-8")); return dispatch(request); }
@Test public void setContentType() { String contentType = "test/plain"; request.setContentType(contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader(HttpHeaders.CONTENT_TYPE)); assertNull(request.getCharacterEncoding()); }
protected MockHttpServletResponse postAsServletResponse( String path, String body, String contentType) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("POST"); request.setContentType(contentType); request.setContent(body.getBytes("UTF-8")); request.addHeader("Content-type", contentType); return dispatch(request); }
@Test public void resolveWhenFormParameterIsPresentAndNotSupportedThenTokenIsNotResolved() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); request.setContentType("application/x-www-form-urlencoded"); request.addParameter("access_token", TEST_TOKEN); assertThat(this.resolver.resolve(request)).isNull(); }
protected MockHttpServletResponse postAsServletResponse( String path, byte[] body, String contentType) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("POST"); request.setContentType(contentType); request.setContent(body); return dispatch(request); }
protected MockHttpServletResponse putAsServletResponse( String path, byte[] body, String contentType) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("PUT"); request.setContentType(contentType); request.setContent(body); return dispatch(request); }
@Test public void setContentTypeThenCharacterEncoding() { request.setContentType("test/plain"); request.setCharacterEncoding("UTF-8"); assertEquals("test/plain", request.getContentType()); assertEquals("test/plain;charset=UTF-8", request.getHeader(HttpHeaders.CONTENT_TYPE)); assertEquals("UTF-8", request.getCharacterEncoding()); }
@Test public void setCharacterEncodingThenContentType() { request.setCharacterEncoding("UTF-8"); request.setContentType("test/plain"); assertEquals("test/plain", request.getContentType()); assertEquals("test/plain;charset=UTF-8", request.getHeader(HttpHeaders.CONTENT_TYPE)); assertEquals("UTF-8", request.getCharacterEncoding()); }
@Test public void resolveWhenValidHeaderIsPresentTogetherWithFormParameterThenAuthenticationExceptionIsThrown() { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Authorization", "Bearer " + TEST_TOKEN); request.setMethod("POST"); request.setContentType("application/x-www-form-urlencoded"); request.addParameter("access_token", TEST_TOKEN); assertThatCode(() -> this.resolver.resolve(request)).isInstanceOf(OAuth2AuthenticationException.class) .hasMessageContaining("Found multiple bearer tokens in the request"); }
protected MockHttpServletResponse putAsServletResponse( String path, byte[] body, String contentType) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("PUT"); request.setContentType(contentType); request.setContent(body); request.addHeader("Content-type", contentType); return dispatch(request); }