Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaultrefererfix #976

Merged
merged 9 commits into from
Apr 23, 2015
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static br.com.caelum.vraptor.view.Results.page;
import static com.google.common.base.Preconditions.checkState;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.enterprise.context.RequestScoped;
Expand Down Expand Up @@ -91,12 +93,22 @@ public void redirect() throws IllegalStateException {
}
}

private String getReferer() {
protected String getReferer() {
String referer = request.getHeader("Referer");
checkState(referer != null, "The Referer header was not specified");

String path = request.getContextPath();
return referer.substring(referer.indexOf(path) + path.length());
String refererPath = null;
try {
refererPath = new URL(referer).getPath();
} catch(MalformedURLException e) {
//Maybe a relative path?
refererPath = referer;
}
String ctxPath = request.getContextPath();

//if the context path is not in the beggining we should return the entire path
//this is useful for proxied app servers which hide the ctx path from url
return refererPath.startsWith(ctxPath) ? refererPath.substring(ctxPath.length()) : refererPath;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static br.com.caelum.vraptor.view.Results.logic;
import static br.com.caelum.vraptor.view.Results.page;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -150,4 +151,24 @@ public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception {
verify(logic).forwardTo(RefererController.class);
verify(controller).index();
}

@Test
public void getRefererShouldReturnCorrectValue() throws Exception {
when(request.getHeader("Referer")).thenReturn("http://vraptor.caelum.com.br/test/anything/ok"); //Regular case
when(request.getContextPath()).thenReturn("/test");
assertEquals("/anything/ok", refererResult.getReferer());

when(request.getHeader("Referer")).thenReturn("http://vraptor.caelum.com.br/anything/ok/test"); //URL without context path (it appears in the path but not in in the right place)
when(request.getContextPath()).thenReturn("/test");
assertEquals("/anything/ok/test", refererResult.getReferer());

when(request.getHeader("Referer")).thenReturn("http://vraptor.caelum.com.br/vrap/anything/ok"); //URL host starts with ctx path string (/vrap)
when(request.getContextPath()).thenReturn("/vrap");
assertEquals("/anything/ok", refererResult.getReferer());

when(request.getHeader("Referer")).thenReturn("/vrap/anything/ok/vrap"); //relative path in the referer (not common but predicted by http spec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can drop the comments on all tests,

then 🚢

when(request.getContextPath()).thenReturn("/vrap");
assertEquals("/anything/ok/vrap", refererResult.getReferer());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split this test into 4 @Test's, with the appropriate descriptions as the method name instead of the comments.

}

}