Skip to content

Commit

Permalink
Merge b6e694e into 350be42
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashinsch committed Aug 4, 2015
2 parents 350be42 + b6e694e commit a9b64d4
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public String getHeader(String name) {
@Override
public Enumeration<String> getHeaders(String name) {
HashSet<String> values = new HashSet<>(Collections.list(super.getHeaders(name)));
values.add(headers.get(name));
if (headers.containsKey(name)) {
values.add(headers.get(name));
}
return Collections.enumeration(values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import javax.servlet.http.HttpServletRequest;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.emptyMap;
import static java.util.Collections.enumeration;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
Expand All @@ -29,7 +35,7 @@ public void setup() {
@Test
public void shouldReturnServletPathUnchangedIfStartingWithSlash() {
// given
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", Collections.emptyMap());
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", emptyMap());

// when
String servletPath = wrapper.getServletPath();
Expand All @@ -41,7 +47,7 @@ public void shouldReturnServletPathUnchangedIfStartingWithSlash() {
@Test
public void shouldAddMissingSlashToServletPath() {
// given
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "test", Collections.emptyMap());
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "test", emptyMap());

// when
String servletPath = wrapper.getServletPath();
Expand All @@ -55,7 +61,7 @@ public void shouldRequestURIReplaceLocalUrl() {
// given
when(httpServletRequest.getServletPath()).thenReturn("/original");
when(httpServletRequest.getContextPath()).thenReturn("/context");
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", Collections.emptyMap());
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", emptyMap());

// when
String requestURI = wrapper.getRequestURI();
Expand All @@ -69,7 +75,7 @@ public void shouldRequestURLSubstituteServletPath() {
// given
when(httpServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/context/original"));
when(httpServletRequest.getServletPath()).thenReturn("/original");
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", Collections.emptyMap());
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", emptyMap());

// when
StringBuffer requestURL = wrapper.getRequestURL();
Expand All @@ -78,4 +84,118 @@ public void shouldRequestURLSubstituteServletPath() {
assertThat(requestURL.toString(), is("http://localhost:8080/context/test"));
}

@Test
public void shouldReturnOverwrittenContentType() {
// given
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type", "overwritten content type");
when(httpServletRequest.getContentType()).thenReturn("wrapped request content type");
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", headers);

// when
String contentType = wrapper.getContentType();

// then
assertThat(contentType, is("overwritten content type"));
}

@Test
public void shouldReturnContentTypeFromWrappedRequest() {
// given
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", emptyMap());
when(httpServletRequest.getContentType()).thenReturn("wrapped request content type");

// when
String contentType = wrapper.getContentType();

// then
assertThat(contentType, is("wrapped request content type"));
}

@Test
public void shouldReturnOverwrittenIntHeader() {
// given
Map<String,String> headers = new HashMap<>();
headers.put("X-my-header", "1");
when(httpServletRequest.getIntHeader("X-my-header")).thenReturn(0);
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", headers);

// when
int value = wrapper.getIntHeader("X-my-header");

// then
assertThat(value, is(1));
}

@Test
public void shouldReturnIntHeaderFromWrappedRequest() {
// given
when(httpServletRequest.getIntHeader("X-my-header")).thenReturn(1);
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", emptyMap());

// when
int value = wrapper.getIntHeader("X-my-header");

// then
assertThat(value, is(1));
}

@Test
public void shouldReturnOverwrittenHeader() {
// given
Map<String,String> headers = new HashMap<>();
headers.put("X-my-header", "overwritten value");
when(httpServletRequest.getHeader("X-my-header")).thenReturn("wrapped request value");
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", headers);

// when
String value = wrapper.getHeader("X-my-header");

// then
assertThat(value, is("overwritten value"));
}

@Test
public void shouldReturnHeaderFromWrappedRequest() {
// given
when(httpServletRequest.getHeader("X-my-header")).thenReturn("wrapped request value");
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", emptyMap());

// when
String value = wrapper.getHeader("X-my-header");

// then
assertThat(value, is("wrapped request value"));
}

@Test
public void shouldReturnOverwrittenHeaders() {
// given
Map<String,String> headers = new HashMap<>();
headers.put("X-my-header", "added value");
when(httpServletRequest.getHeaders("X-my-header")).thenReturn(enumeration(singletonList("wrapped request value")));
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", headers);

// when
Enumeration<String> value = wrapper.getHeaders("X-my-header");

// then
assertThat(value.nextElement(), is("added value"));
assertThat(value.nextElement(), is("wrapped request value"));
}

@Test
public void shouldReturnHeaderFromWrappedRequests() {
// given
when(httpServletRequest.getHeaders("X-my-header")).thenReturn(enumeration(singletonList("wrapped request value")));
UrlRewritingRequestWrapper wrapper = new UrlRewritingRequestWrapper(httpServletRequest, "/test", emptyMap());

// when
Enumeration<String> value = wrapper.getHeaders("X-my-header");

// then
assertThat(value.nextElement(), is("wrapped request value"));
}


}

0 comments on commit a9b64d4

Please sign in to comment.