Skip to content

Commit

Permalink
#834 RqHref.Smart fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 2, 2018
1 parent 74f4600 commit da47b15
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/main/java/org/takes/rq/RqHref.java
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
import org.takes.HttpException;
Expand Down Expand Up @@ -138,12 +139,12 @@ public InputStream body() throws IOException {
* @since 0.14
*/
public Href home() throws IOException {
final URI full = URI.create(this.href().toString());
return new Href(
String.format(
"http://%s/",
new RqHeaders.Smart(
new RqHeaders.Base(this)
).single("Host")
"%s://%s/",
full.getScheme(),
full.getHost()
)
);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/takes/rq/RqHrefTest.java
Expand Up @@ -37,6 +37,7 @@
* @version $Id$
* @since 0.1
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class RqHrefTest {

/**
Expand Down Expand Up @@ -194,6 +195,29 @@ public void extractsHome() throws IOException {
);
}

/**
* RqHref.Smart can extract home URI.
* @throws IOException If some problem inside
*/
@Test
public void extractsHomeWithProtocol() throws IOException {
MatcherAssert.assertThat(
new RqHref.Smart(
new RqHref.Base(
new RqFake(
Arrays.asList(
"GET /bye-dude?extra=343",
"Host: ff9.example.com",
"X-Forwarded-Proto: https"
),
""
)
)
).home(),
Matchers.hasToString("https://ff9.example.com/")
);
}

/**
* RqHref.Smart can extract param with default value.
* @throws IOException If some problem inside
Expand Down

0 comments on commit da47b15

Please sign in to comment.