Skip to content

Commit

Permalink
#816 TkRedirect fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 1, 2018
1 parent 727bd3a commit b781578
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/main/java/org/takes/tk/TkRedirect.java
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.takes.Request;
Expand Down Expand Up @@ -72,7 +73,7 @@ public TkRedirect(final String location, final int code) {
@Override
public Response act(final Request req) throws IOException {
return new RsRedirect(
new RedirectParams(req, location).location(),
new TkRedirect.RedirectParams(req, location).location(),
code
);
}
Expand All @@ -82,22 +83,16 @@ public Response act(final Request req) throws IOException {

/**
* Extract params from original query.
* @todo #793:15min Replace this class usages with
* RqHref(req).href() or simply Href when the issue with parsing anchors
* is fixed and delete this implementation for good
*/
private static final class RedirectParams {

/**
* Original request.
*/
private final Request req;

/**
* Original location.
*/
private final String origin;

/**
* Ctor.
* @param req Original request.
Expand All @@ -107,24 +102,28 @@ private static final class RedirectParams {
this.req = req;
this.origin = origin;
}

/**
* Get location with composed params.
* @return New location.
* @throws IOException in case of error.
*/
public String location() throws IOException {
String loc = this.origin;
final String uri = new RqRequestLine.Base(this.req).uri();
final int idx = uri.indexOf('?');
if (idx != -1) {
loc = String.format(
"%s&%s",
loc,
uri.substring(idx + 1)
);
final StringBuilder loc = new StringBuilder(this.origin);
final URI target = URI.create(this.origin);
final URI uri = URI.create(new RqRequestLine.Base(this.req).uri());
if (uri.getQuery() != null) {
if (target.getQuery() == null) {
loc.append('?');
} else {
loc.append('&');
}
loc.append(uri.getQuery());
}
if (uri.getFragment() != null) {
loc.append('#');
loc.append(uri.getFragment());
}
return loc;
return loc.toString();
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/takes/tk/TkRedirectTest.java
Expand Up @@ -113,4 +113,21 @@ public void redirectCarriesQueryAndFragment() throws IOException {
);
}

/**
* TkRedirect should carry on the query and the fragment.
* @throws IOException If some problem inside
*/
@Test
public void redirectCarriesQueryAndFragmentOnEmptyUrl() throws IOException {
MatcherAssert.assertThat(
new TkRedirect().act(
new RqFake("GET", "/hey-you?f=1#xxx")
),
new HmRsHeader(
"Location",
"/?f=1#xxx"
)
);
}

}

1 comment on commit b781578

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b781578 Mar 1, 2018

Choose a reason for hiding this comment

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

Puzzle 793-49ae4191 disappeared from src/main/java/org/takes/tk/TkRedirect.java, that's why I closed #796. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.