@Test public void shouldReturnCookies() { Map<String, Cookie> cookies = new HashMap<>(); Cookie cookie1 = new Cookie("someName", "someValue"); Cookie cookie2 = new Cookie("someOtherName", "someOtherValue"); Cookie sessionCookie = new Cookie(HttpSessionImpl.COOKIE_NAME, "sessionId"); cookies.put("someName", cookie1); cookies.put("someOtherName", cookie2); cookies.put(HttpSessionImpl.COOKIE_NAME, sessionCookie); httpServletRequestImpl = builder.withCookies(cookies).build(); assertThat(httpServletRequestImpl.getCookies().length, is(cookies.size())); assertThat(Arrays.asList(httpServletRequestImpl.getCookies()), hasItems(cookie1, cookie2, sessionCookie)); assertThat(httpServletRequestImpl.getCookie("someName"), is(cookie1)); assertThat(httpServletRequestImpl.getCookie("someOtherName"), is(cookie2)); assertThat(httpServletRequestImpl.getCookie(HttpSessionImpl.COOKIE_NAME), is(sessionCookie)); assertThat(httpServletRequestImpl.getRequestedSessionId(), is("sessionId")); assertThat(httpServletRequestImpl.getCookie("inexistingName"), is(nullValue())); }
private void assignSocketMetadata(final Socket socket, final HttpServletRequestImpl.Builder builder) { builder.withSecure(false) .withScheme(DEFAULT_SCHEME) .withRemoteAddr(socket.getInetAddress().getHostAddress()) .withRemotePort(((InetSocketAddress) socket.getRemoteSocketAddress()).getPort()) .withRemoteHost(((InetSocketAddress) socket.getRemoteSocketAddress()).getHostName()) .withLocalAddr(socket.getLocalAddress().getHostAddress()) .withLocalPort(socket.getLocalPort()).withServerPort(socket.getLocalPort()) .withLocalName(socket.getLocalAddress().getHostName()) .withServerName(socket.getInetAddress().getHostName()); }
private void handlePostMultipartRequest(final HttpServletRequestImpl.Builder builder, final Headers headers, final InputStream in, final int postLength) throws IOException, MalformedInputException { String boundary = headers.getHeader(Headers.HEADER_CONTENT_TYPE); int boundaryPosition = boundary.toLowerCase().indexOf(BOUNDARY_START); builder.withMultipart(true); if (boundaryPosition > -1) { int boundaryStartPos = boundaryPosition + BOUNDARY_START.length(); if (boundaryStartPos < boundary.length()) { boundary = boundary.substring(boundaryStartPos, boundary.length()); MultipartRequestHandler mrh = new MultipartRequestHandler(multipartHeadersPartParser, in, postLength, boundary, tempPath, MULTIPART_BUFFER_LENGTH); mrh.handle(); builder.withPostParameters(mrh.getPost()).withUploadedFiles(mrh.getUploadedFiles()); } } } }
builder.withGetParameters(queryStringParser.parse(status.getQueryString())); } catch (MalformedInputException e) { builder.withServletContext(getImplicitServletContext()) .withInputStream(in) .withStatus(status) .withPathTranslated(status.getUri()) // TODO There is no way to make it work under Android .withPathInfo("") .withRemoteUser(null) .withPrincipal(null) .withHeaders(headers) .withCookies(getCookies(headers)); return builder.build();
.withStatus(requestStatus) .withPostParameters(postParameters) .withGetParameters(getParameters) .withScheme("http") .withCookies(cookies) .withHeaders(headers) .withInputStream(inputStream) .withLocalPort(123) .withLocalAddr("localAddr") .withLocalName("localName") .withRemotePort(987) .withRemoteAddr("remoteAddr") .withRemoteHost("remoteHost") .withServerPort(8080) .withServerName("serverName") .withSecure(true) .withServletContext(servletContext) .withMultipart(true) .withUploadedFiles(new HashSet<UploadedFile>()); httpServletRequestImpl = builder.build();
private void handlePostPlainRequest(final HttpServletRequestImpl.Builder builder, final InputStream in, final int postLength) throws IOException, MalformedInputException { byte[] buffer; buffer = new byte[1]; StringBuilder postLine = new StringBuilder(); while (in.read(buffer, 0, buffer.length) != -1) { postLine.append((char) buffer[0]); if (postLine.length() == postLength) { break; } } Statistics.addBytesReceived(postLine.length()); builder.withPostParameters(queryStringParser.parse(postLine.toString())); }
@Test public void shouldParseUrlFromHost() { Headers headers = new Headers(); headers.setHeader(Headers.HEADER_HOST, "example.com:3366"); httpServletRequestImpl = builder.withHeaders(headers).build(); assertThat(httpServletRequestImpl.getRequestURL().toString(), is("http://example.com:8080/someuri")); }
@Test public void shouldReturnContentLength() { Headers headers = new Headers(); headers.setHeader(Headers.HEADER_CONTENT_LENGTH, "1234"); httpServletRequestImpl = builder.withHeaders(headers).build(); assertThat(httpServletRequestImpl.getContentLength(), is(1234)); }
@Test public void shouldParseDateHeader() { Headers headers = new Headers(); headers.setHeader("If-Modified-Since", "Thu, 15 Jan 2015 16:30:13 GMT"); headers.setHeader("If-Modified-Since-MALFORMED", "Malformed Value"); httpServletRequestImpl = builder.withHeaders(headers).build(); assertThat(httpServletRequestImpl.getDateHeader("If-Modified-Since"), is(1421339413000L)); assertThat(httpServletRequestImpl.getDateHeader("If-Modified-Since-MALFORMED"), is(-1L)); assertThat(httpServletRequestImpl.getDateHeader("Inexisting"), is(-1L)); }
@Test public void shouldParseNumericHeader() { Headers headers = new Headers(); headers.setHeader("intKey", "3333"); headers.setHeader("unableToParseKey", "AAAA"); httpServletRequestImpl = builder.withHeaders(headers).build(); assertThat(httpServletRequestImpl.getIntHeader("missingIntKey"), is(-1)); assertThat(httpServletRequestImpl.getIntHeader("intKey"), is(3333)); assertThat(httpServletRequestImpl.getIntHeader("unableToParseKey"), is(0)); }
@Test public void shouldReturnTheSameSessionForConsecutiveCalls() { Map<String, Cookie> cookies = new HashMap<>(); Cookie sessionCookie = new Cookie(HttpSessionImpl.COOKIE_NAME, "sessionId"); cookies.put(HttpSessionImpl.COOKIE_NAME, sessionCookie); httpServletRequestImpl = builder.withCookies(cookies).withServletContext(servletContext).build(); when(servletContext.getSession("sessionId")).thenReturn(new HttpSessionImpl("sessionId", System.currentTimeMillis())); assertThat(httpServletRequestImpl.getSession(), is(instanceOf(HttpSessionImpl.class))); assertThat(httpServletRequestImpl.getSession().equals(httpServletRequestImpl.getSession()), is(true)); }
@Test public void shouldReturnSession() { Map<String, Cookie> cookies = new HashMap<>(); Cookie sessionCookie = new Cookie(HttpSessionImpl.COOKIE_NAME, "sessionId"); cookies.put(HttpSessionImpl.COOKIE_NAME, sessionCookie); httpServletRequestImpl = builder.withCookies(cookies).withServletContext(servletContext).build(); when(servletContext.getSession("sessionId")).thenReturn(new HttpSessionImpl("sessionId", System.currentTimeMillis())); assertThat(httpServletRequestImpl.getSession(), is(instanceOf(HttpSessionImpl.class))); }
@Test public void shouldReturnContentType() { Headers headers = new Headers(); headers.setHeader(Headers.HEADER_CONTENT_TYPE, "SOME_TYPE/TEXT"); httpServletRequestImpl = builder.withHeaders(headers).build(); assertThat(httpServletRequestImpl.getContentType(), is("SOME_TYPE/TEXT")); }