/** * {@inheritDoc} */ @Override public String getRequestedSessionId() { Cookie sessionCookie = getCookie(HttpSessionImpl.COOKIE_NAME); if (sessionCookie == null) { return null; } return sessionCookie.getValue(); }
/** * {@inheritDoc} */ @Override public int getContentLength() { if (!headers.containsHeader(Headers.HEADER_CONTENT_LENGTH)) { return -1; } return getIntHeader(Headers.HEADER_CONTENT_LENGTH); }
/** * Returns requested host name. * * @return */ private String getHost() { String host; if (headers.containsHeader(Headers.HEADER_HOST)) { // Strip port number host = headers.getHeader(Headers.HEADER_HOST).split(":")[0]; } else { host = getLocalAddr(); } return host; }
/** * {@inheritDoc} */ @Override public StringBuilder getRequestURL() { StringBuilder url = new StringBuilder(); url.append(getScheme()).append("://").append(getHost()); int port = getServerPort(); if (port != HTTP_PORT && port != HTTPS_PORT) { url.append(':').append(port); } url.append(status.getUri()); return url; }
@Test public void shouldReturnCorrectValuesGetters() { requestStatus.setMethod(HttpServletRequest.METHOD_GET); httpServletRequestImpl.setAttribute("name", "value"); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getAttributeNames())), hasSize(1)); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getAttributeNames())), hasItems("name")); assertThat((String) httpServletRequestImpl.getAttribute("name"), is("value")); httpServletRequestImpl.removeAttribute("name"); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getAttributeNames())), hasSize(0)); assertThat(httpServletRequestImpl.getRequestURI(), is(requestStatus.getUri())); assertThat(httpServletRequestImpl.getQueryString(), is(requestStatus.getQueryString())); assertThat(httpServletRequestImpl.getRequestedSessionId(), is(nullValue())); assertThat(httpServletRequestImpl.getContentLength(), is(-1)); assertThat(httpServletRequestImpl.getCharacterEncoding(), is("UTF-8")); httpServletRequestImpl.setCharacterEncoding("ISO 8859-1"); assertThat(httpServletRequestImpl.getCharacterEncoding(), is("ISO 8859-1")); assertThat(httpServletRequestImpl.getLocalPort(), is(123)); assertThat(httpServletRequestImpl.getLocalAddr(), is("localAddr")); assertThat(httpServletRequestImpl.getLocalName(), is("localName")); assertThat(httpServletRequestImpl.getRemotePort(), is(987)); assertThat(httpServletRequestImpl.getRemoteAddr(), is("remoteAddr")); assertThat(httpServletRequestImpl.getRemoteHost(), is("remoteHost")); assertThat(httpServletRequestImpl.getServerPort(), is(8080)); assertThat(httpServletRequestImpl.getServerName(), is("serverName")); assertThat(httpServletRequestImpl.isSecure(), is(true)); assertThat(httpServletRequestImpl.getRequestURL().toString(), is("http://localAddr:8080/someuri")); assertThat(httpServletRequestImpl.getIntHeader("someInexistentHeader"), is(-1)); assertThat(httpServletRequestImpl.getInputStream(), is(inputStream)); assertThat(httpServletRequestImpl.getProtocol(), is(requestStatus.getProtocol()));
@Test public void shouldReturnPostRequestParametersMapOnPutMethod() { requestStatus.setMethod(HttpServletRequest.METHOD_PUT); assertThat(httpServletRequestImpl.getPostParameter("postKey"), is("postValue")); assertThat(((Map<String, String>) httpServletRequestImpl.getParameterMap()), hasKey("postKey")); assertThat(((Map<String, String>) httpServletRequestImpl.getParameterMap()), not(hasKey("getKey"))); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), hasSize(1)); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), hasItems("postKey")); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), not(hasItems("getKey"))); }
@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())); }
@Test public void shouldReturnGetRequestParametersMapOnGetMethod() { requestStatus.setMethod(HttpServletRequest.METHOD_GET); assertThat(httpServletRequestImpl.getParameter("getKey"), is("getValue")); assertThat(((Map<String, String>) httpServletRequestImpl.getParameterMap()), hasKey("getKey")); assertThat(((Map<String, String>) httpServletRequestImpl.getParameterMap()), not(hasKey("postKey"))); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), hasSize(1)); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), hasItems("getKey")); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), not(hasItems("postKey"))); }
/** * {@inheritDoc} */ @Override public void load(final String path, final HttpServletRequestImpl request, final HttpServletResponseImpl response) throws IOException { File file = getFile(path); // A server MUST ignore a Range header field received with a request method other than GET. boolean isGetRequest = request.getMethod().equals(HttpServletRequest.METHOD_GET); boolean isPartialRequest = isGetRequest && request.getHeaders().containsHeader(Headers.HEADER_RANGE); if (isPartialRequest) { loadPartialContent(request, response, file); } else { loadCompleteContent(request, response, file); } }
@Test public void shouldAssignNoCookieAndNoHeadersOnNoHeadersString() throws Exception { when(socket.getInputStream()).thenReturn(new ByteArrayInputStream("GET / HTTP/1.0\r\n\r\n".getBytes())); HttpServletRequestImpl request = factory.createFromSocket(socket); assertThat(request.getCookies().length, is(0)); assertThat(request.getHeaders().keySet().size(), is(0)); } }
request.getMethod(), request.getRequestURI() }); String requestedPath = request.getRequestURI();
throws IOException, ProtocolException { HttpServletRequestImpl.Builder builder = HttpServletRequestImpl.createNewBuilder();
/** * {@inheritDoc} */ @Override public Locale getLocale() { Enumeration<Locale> locales = getLocales(); if (locales == null) { return null; } return locales.nextElement(); }
/** * {@inheritDoc} */ @Override public Enumeration getParameterNames() { return Collections.enumeration(getParameterMap().keySet()); }
private HttpSession getSessionInstance() { if (!sessionWasRequested) { sessionWasRequested = true; String sessionId = getRequestedSessionId(); if (sessionId != null) { session = servletContext.getSession(sessionId); } } return session; }
@Test public void shouldReturnPostRequestParametersMapOnPostMethod() { requestStatus.setMethod(HttpServletRequest.METHOD_POST); assertThat(httpServletRequestImpl.getPostParameter("postKey"), is("postValue")); assertThat(((Map<String, String>) httpServletRequestImpl.getParameterMap()), hasKey("postKey")); assertThat(((Map<String, String>) httpServletRequestImpl.getParameterMap()), not(hasKey("getKey"))); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), hasSize(1)); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), hasItems("postKey")); assertThat((List<String>) (Collections.list(httpServletRequestImpl.getParameterNames())), not(hasItems("getKey"))); }
builder = HttpServletRequestImpl.createNewBuilder() .withStatus(requestStatus) .withPostParameters(postParameters)
@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)); }