Skip to content

Commit

Permalink
#35 reproduced and fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 8, 2017
1 parent 3fdbe60 commit 6b17a2d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 59 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/jare/tk/Destination.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String path() throws HttpException {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"protocol must be either HTTP or HTTPS at \"%s\"",
"Protocol must be either HTTP or HTTPS at \"%s\"",
this.uri
)
);
Expand All @@ -81,13 +81,13 @@ public String path() throws HttpException {
if (this.uri.getPath().isEmpty()) {
path.append('/');
} else {
path.append(this.uri.getPath());
path.append(this.uri.getRawPath());
}
if (this.uri.getQuery() != null) {
path.append('?').append(this.uri.getQuery());
path.append('?').append(this.uri.getRawQuery());
}
if (this.uri.getFragment() != null) {
path.append('#').append(this.uri.getFragment());
path.append('#').append(this.uri.getRawFragment());
}
return path.toString();
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/jare/tk/TkRelay.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class TkRelay implements Take {
* @link https://tools.ietf.org/html/rfc3986
*/
private static final Pattern PTN = Pattern.compile(
"[A-Za-z0-9-._~:/\\?#@!\\$&'\\(\\)\\*\\+,;=`\\[\\]]+"
"[A-Za-z0-9-%._~:/\\?#@!\\$&'\\(\\)\\*\\+,;=`\\[\\]]+"
);

/**
Expand Down Expand Up @@ -109,7 +109,10 @@ public Response act(final Request req) throws IOException {
return TkRelay.cached(
new RsWithHeaders(
new TkProxy(uri.toString()).act(
TkRelay.request(req, new Destination(uri).path())
TkRelay.request(
req,
new Destination(uri).path()
)
),
String.format("X-Jare-Target: %s", uri),
String.format("X-Jare-Usage: %d", domain.usage().total())
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/io/jare/tk/DestinationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public final class DestinationTest {
@Test
public void buildsPath() throws Exception {
MatcherAssert.assertThat(
new Destination(new URI("http://www.google.com/a/b/c")).path(),
Matchers.equalTo("/a/b/c")
new Destination(
new URI(
"http://www.google.com/a/b/%D0%B4%D0%B0?z=%D0%B0#%D0%B0"
)
).path(),
Matchers.equalTo("/a/b/%D0%B4%D0%B0?z=%D0%B0#%D0%B0")
);
}

Expand Down
93 changes: 42 additions & 51 deletions src/test/java/io/jare/tk/TkRelayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@
package io.jare.tk;

import io.jare.fake.FkBase;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Arrays;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.takes.HttpException;
import org.takes.Request;
import org.takes.Take;
import org.takes.facets.fork.FkRegex;
import org.takes.facets.fork.TkFork;
import org.takes.facets.hamcrest.HmRsHeader;
import org.takes.http.FtRemote;
import org.takes.rq.RqFake;
import org.takes.rq.RqHref;
import org.takes.rs.RsPrint;
import org.takes.rs.RsText;
import org.takes.tk.TkText;
import org.takes.tk.TkWithHeaders;

Expand All @@ -48,76 +53,44 @@
*/
public final class TkRelayTest {

/**
* TkRelay can send the request through.
* @throws Exception If some problem inside
*/
@Test
public void sendsRequestThrough() throws Exception {
final Take target = new TkFork(
new FkRegex("/alpha/beta", new TkText("it's success"))
);
new FtRemote(target).exec(
home -> MatcherAssert.assertThat(
new RsPrint(
new TkRelay(new FkBase()).act(
new RqFake(
Arrays.asList(
String.format("GET /?u=%s/alpha/beta", home),
"Host: localhost"
),
""
)
)
).printBody(),
Matchers.containsString("success")
)
);
}

/**
* TkRelay can send the request through.
* @throws Exception If some problem inside
*/
@Test
public void sendsRequestThroughToHome() throws Exception {
final Take target = new TkFork(
new FkRegex("/.*", new TkText("it's home"))
new FkRegex(
"/alpha/.*",
(Take) req -> new RsText(
new RqHref.Base(req).href().toString()
)
)
);
new FtRemote(target).exec(
home -> MatcherAssert.assertThat(
new RsPrint(
new TkRelay(new FkBase()).act(
new RqFake(
Arrays.asList(
String.format(
"GET /?u=%s",
URLEncoder.encode(
home.resolve("/альфа").toASCIIString(),
"UTF-8"
)
),
"Host: localhost "
),
""
)
TkRelayTest.fake(home, "/alpha/%D0%B4%D0%B0?a=%D0%B0")
)
).printBody(),
Matchers.containsString("home")
Matchers.equalTo(
String.format("%s/alpha/%%D0%%B4%%D0%%B0?a=%%D0%%B0", home)
)
)
);
}

/**
* TkRelay can tolerate complex URLs.
* TkRelay can fail if URL is not valid (space is not allowed).
* @throws Exception If some problem inside
*/
@Test(expected = HttpException.class)
public void catchesInvalidUrls() throws Exception {
new TkRelay(new FkBase()).act(
new RqFake(
Arrays.asList(
"GET /?u=http://www.yegor256.com/i+%D1%85%D0%BC",
"GET /?u=http://www.yegor256.com/a+b",
"Host: 127.0.0.1"
),
""
Expand All @@ -141,13 +114,7 @@ public void setsCachingHeaders() throws Exception {
home -> MatcherAssert.assertThat(
new RsPrint(
new TkRelay(new FkBase()).act(
new RqFake(
Arrays.asList(
String.format("GET /?u=%s&whatever", home),
"Host: test.jare.io"
),
""
)
TkRelayTest.fake(home, "/&whatever")
)
),
Matchers.allOf(
Expand All @@ -162,4 +129,28 @@ public void setsCachingHeaders() throws Exception {
);
}

/**
* Fake request.
* @param home Base URI
* @param path Path
* @return Request
* @throws UnsupportedEncodingException If fails
*/
private static Request fake(final URI home, final String path)
throws UnsupportedEncodingException {
return new RqFake(
Arrays.asList(
String.format(
"GET /?u=%s",
URLEncoder.encode(
home.resolve(path).toString(),
"UTF-8"
)
),
"Host: localhost"
),
""
);
}

}

0 comments on commit 6b17a2d

Please sign in to comment.