### Question:
RefererService { public String buildRedirectUri(String referer, String targetPath) { if(isInvalidTargetPath(targetPath)) { throw new BadTargetPathException("Illegal target_path"); } return UriComponentsBuilder.fromUriString(referer) .replacePath(targetPath) .build() .toString(); }  String buildRedirectUri(String referer, String targetPath);  }### Answer:
@Test public void shouldBuildRedirectUri() throws Exception { String redirectUri = refererService.buildRedirectUri("http: assertThat(redirectUri, is(equalTo("http: }
@Test public void shouldBuildRedirectUriWithoutStartingSlash() throws Exception { String redirectUri = refererService.buildRedirectUri("http: assertThat(redirectUri, is(equalTo("http: }
@Test public void shouldBuildRedirectUriByIgnoringEndingSlash() throws Exception { String redirectUri = refererService.buildRedirectUri("http: assertThat(redirectUri, is(equalTo("http: }
@Test public void shouldBuildRedirectUriByIgnoringAnyRefererPath() throws Exception { String redirectUri = refererService.buildRedirectUri("http: assertThat(redirectUri, is(equalTo("http: }
@Test public void shouldThrowErrorWhenTargetPathIsContainingQueryParams() throws Exception { catchException(refererService) .buildRedirectUri( "http: "/my_custom_path?query_param=value" ); Assert.assertThat(caughtException(), allOf( instanceOf(RefererService.BadTargetPathException.class), hasMessage("Illegal target_path") ) ); }
@Test public void shouldThrowErrorWhenTargetPathIsAbsoluteUrl() throws Exception { catchException(refererService) .buildRedirectUri( "http: "http: ); Assert.assertThat(caughtException(), allOf( instanceOf(RefererService.BadTargetPathException.class), hasMessage("Illegal target_path") ) ); }
@Test public void shouldThrowErrorWhenTargetPathIsIllegalUrl() throws Exception { catchException(refererService) .buildRedirectUri( "http: "http: ); Assert.assertThat(caughtException(), allOf( instanceOf(RefererService.BadTargetPathException.class), hasMessage("Illegal target_path") ) ); }