Skip to content

Commit

Permalink
Merge pull request #91 from dimadl/tokenized_location_header
Browse files Browse the repository at this point in the history
Fixes issue #90 - Added ability to use tokenized "Location" header in 3xx responses
  • Loading branch information
azagniotov authored Mar 19, 2018
2 parents a9933fa + 94cbed3 commit c6c8936
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import org.eclipse.jetty.http.HttpHeader;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static io.github.azagniotov.stubby4j.utils.StringUtils.isTokenized;
import static io.github.azagniotov.stubby4j.utils.StringUtils.replaceTokensInString;

public class RedirectResponseHandlingStrategy implements StubResponseHandlingStrategy {

private final StubResponse foundStubResponse;
Expand All @@ -39,14 +43,21 @@ public class RedirectResponseHandlingStrategy implements StubResponseHandlingStr
@Override
public void handle(final HttpServletResponse response, final StubRequest assertionStubRequest) throws Exception {
HandlerUtils.setResponseMainHeaders(response);
final Map<String, String> regexGroups = assertionStubRequest.getRegexGroups();

if (StringUtils.isSet(foundStubResponse.getLatency())) {
final long latency = Long.parseLong(foundStubResponse.getLatency());
TimeUnit.MILLISECONDS.sleep(latency);
}

String headerLocation = foundStubResponse.getHeaders().get("location");

if (isTokenized(headerLocation)) {
headerLocation = replaceTokensInString(headerLocation, regexGroups);
}

response.setHeader(HttpHeader.LOCATION.asString(), headerLocation);
response.setStatus(foundStubResponse.getHttpStatusCode().getCode());
response.setHeader(HttpHeader.LOCATION.asString(), foundStubResponse.getHeaders().get("location"));
response.setHeader(HttpHeader.CONNECTION.asString(), "close");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpStatus.Code;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -15,6 +16,8 @@

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.TreeMap;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -39,6 +42,13 @@ public class RedirectResponseHandlingStrategyTest {
@InjectMocks
private RedirectResponseHandlingStrategy redirectResponseHandlingStrategy;

@Before
public void setUp(){
when(mockStubResponse.getHeaders()).thenReturn(new HashMap<String, String>() {{
put("location", "http://location.com");
}});
}

@Test
public void shouldVerifyBehaviourWhenHandlingTemporaryRedirectResponseWithoutLatency() throws Exception {
when(mockStubResponse.getHttpStatusCode()).thenReturn(Code.MOVED_TEMPORARILY);
Expand Down Expand Up @@ -76,6 +86,29 @@ public void shouldVerifyBehaviourWhenHandlingRedirectResponseWithLatency() throw
verifyMainHeaders(mockHttpServletResponse);
}

@Test
public void shouldReturnReplacedValueInLocationHeaderWhenRequestBodyHasDynamicToken() throws Exception {
String redirectUrlDomain = "test.com";
String tokenizedLocationHeaderValue = "https://<% query.redirect_uri.1 %>/auth";
String expectedLocationValue = "https://test.com/auth";

when(mockAssertionRequest.getRegexGroups()).thenReturn(new TreeMap<String, String>() {{
put("query.redirect_uri.1", redirectUrlDomain);
}});
when(mockStubResponse.getHttpStatusCode()).thenReturn(Code.MOVED_TEMPORARILY);
when(mockStubResponse.getHeaders()).thenReturn(new HashMap<String, String>() {{
put("location", tokenizedLocationHeaderValue);
}});

redirectResponseHandlingStrategy.handle(mockHttpServletResponse, mockAssertionRequest);

verify(mockHttpServletResponse, times(1)).setStatus(HttpStatus.MOVED_TEMPORARILY_302);
verify(mockHttpServletResponse, times(1)).setHeader(HttpHeader.LOCATION.asString(), expectedLocationValue);
verify(mockHttpServletResponse, times(1)).setHeader(HttpHeader.CONNECTION.asString(), "close");
verifyMainHeaders(mockHttpServletResponse);

}

private void verifyMainHeaders(final HttpServletResponse mockHttpServletResponse) throws Exception {
verify(mockHttpServletResponse, times(1)).setHeader(HttpHeader.SERVER.asString(), HandlerUtils.constructHeaderServerName());
verify(mockHttpServletResponse, times(1)).setHeader(HttpHeader.CONTENT_TYPE.asString(), "text/html;charset=UTF-8");
Expand Down

0 comments on commit c6c8936

Please sign in to comment.