private void content(MockHttpServletRequest request, Charset charset) { String requestBody = this.webRequest.getRequestBody(); if (requestBody == null) { return; } request.setContent(requestBody.getBytes(charset)); }
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); }
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 // SPR-16505 public void getInputStreamTwice() throws IOException { byte[] bytes = "body".getBytes(Charset.defaultCharset()); request.setContent(bytes); assertSame(request.getInputStream(), request.getInputStream()); }
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); }
@Test // SPR-16505 public void getReaderTwice() throws IOException { byte[] bytes = "body".getBytes(Charset.defaultCharset()); request.setContent(bytes); assertSame(request.getReader(), request.getReader()); }
request.setContent(this.content); request.setContentType(this.contentType);
@Test public void printRequestWithCharacterEncoding() throws Exception { this.request.setCharacterEncoding("UTF-8"); this.request.setContent("text".getBytes("UTF-8")); this.handler.handle(this.mvcResult); assertValue("MockHttpServletRequest", "Body", "text"); }
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 setContentAndGetInputStream() throws IOException { byte[] bytes = "body".getBytes(Charset.defaultCharset()); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset())); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset())); }
/** * 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 setContentAndGetContentAsByteArray() { byte[] bytes = "request body".getBytes(); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals(bytes, request.getContentAsByteArray()); }
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); }
@Test public void setContentAndGetReader() throws IOException { byte[] bytes = "body".getBytes(Charset.defaultCharset()); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals("body", FileCopyUtils.copyToString(request.getReader())); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals("body", FileCopyUtils.copyToString(request.getReader())); }
@Test public void setContentAndGetContentAsStringWithExplicitCharacterEncoding() throws IOException { String palindrome = "ablE was I ere I saw Elba"; byte[] bytes = palindrome.getBytes("UTF-16"); request.setCharacterEncoding("UTF-16"); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals(palindrome, request.getContentAsString()); }
@Test public void printRequest() throws Exception { this.request.addParameter("param", "paramValue"); this.request.addHeader("header", "headerValue"); this.request.setCharacterEncoding("UTF-16"); String palindrome = "ablE was I ere I saw Elba"; byte[] bytes = palindrome.getBytes("UTF-16"); this.request.setContent(bytes); this.request.getSession().setAttribute("foo", "bar"); this.handler.handle(this.mvcResult); HttpHeaders headers = new HttpHeaders(); headers.set("header", "headerValue"); MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("param", "paramValue"); assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod()); assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI()); assertValue("MockHttpServletRequest", "Parameters", params); assertValue("MockHttpServletRequest", "Headers", headers); assertValue("MockHttpServletRequest", "Body", palindrome); assertValue("MockHttpServletRequest", "Session Attrs", Collections.singletonMap("foo", "bar")); }
@Test public void printRequestWithEmptySessionMock() throws Exception { this.request.addParameter("param", "paramValue"); this.request.addHeader("header", "headerValue"); this.request.setCharacterEncoding("UTF-16"); String palindrome = "ablE was I ere I saw Elba"; byte[] bytes = palindrome.getBytes("UTF-16"); this.request.setContent(bytes); this.request.setSession(Mockito.mock(HttpSession.class)); this.handler.handle(this.mvcResult); HttpHeaders headers = new HttpHeaders(); headers.set("header", "headerValue"); MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("param", "paramValue"); assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod()); assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI()); assertValue("MockHttpServletRequest", "Parameters", params); assertValue("MockHttpServletRequest", "Headers", headers); assertValue("MockHttpServletRequest", "Body", palindrome); }
@Test public void printRequestWithoutSession() throws Exception { this.request.addParameter("param", "paramValue"); this.request.addHeader("header", "headerValue"); this.request.setCharacterEncoding("UTF-16"); String palindrome = "ablE was I ere I saw Elba"; byte[] bytes = palindrome.getBytes("UTF-16"); this.request.setContent(bytes); this.handler.handle(this.mvcResult); HttpHeaders headers = new HttpHeaders(); headers.set("header", "headerValue"); MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("param", "paramValue"); assertValue("MockHttpServletRequest", "HTTP Method", this.request.getMethod()); assertValue("MockHttpServletRequest", "Request URI", this.request.getRequestURI()); assertValue("MockHttpServletRequest", "Parameters", params); assertValue("MockHttpServletRequest", "Headers", headers); assertValue("MockHttpServletRequest", "Body", palindrome); }
/** * Executes an ows request using the GET method. * * @param path The porition of the request after hte context, example: * 'wms?request=GetMap&version=1.1.1&..." * @param charset The character set of the response. * @return the mock servlet response */ protected MockHttpServletResponse getAsServletResponse(String path, String charset) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("GET"); request.setContent(new byte[] {}); return dispatch(request, charset); }
/** * Executes an ows request using the GET method. * * @param path The porition of the request after hte context, example: * 'wms?request=GetMap&version=1.1.1&..." * @param charset The character set of the response. * @return the mock servlet response */ protected MockHttpServletResponse getAsServletResponse(String path, String charset) throws Exception { MockHttpServletRequest request = createRequest(path); request.setMethod("GET"); request.setContent(new byte[] {}); return dispatch(request, charset); }