Skip to content

Commit

Permalink
#1193 follow redirects in JsonResources
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 3, 2021
1 parent be8afd8 commit 0acdf71
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,13 @@ private HttpClient newHttpClient() {
client = HttpClient
.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
} else {
client = HttpClient.newHttpClient();
client = HttpClient
.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
}
return client;
}
Expand Down
111 changes: 111 additions & 0 deletions self-core-impl/src/test/java/com/selfxdsd/core/JdkHttpITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,117 @@ public void getJsonObjectOkNoAuth() throws IOException {
}
}

/**
* We can GET a JsonObject from the server if the response is 301 (redirect)
* with a new Location header.
* @throws IOException If something goes wrong.
*/
@Test
public void getJsonObjectMovedPermanentlyRedirect() throws IOException {
final int port = this.resource.port();
final JsonObject json = Json.createObjectBuilder()
.add("from", "server")
.build();
try(
final MkContainer container = new MkGrizzlyContainer().next(
new MkAnswer.Simple(HttpURLConnection.HTTP_MOVED_PERM)
.withHeader(
"Location", "http://localhost:" + port
)
).next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
json.toString()
)
).start(port)
) {
final JsonResources resources = new JsonResources.JdkHttp(true);
final Resource response = resources.get(container.home());
MatcherAssert.assertThat(
response.asJsonObject(),
Matchers.equalTo(json)
);
MatcherAssert.assertThat(
response.statusCode(),
Matchers.equalTo(HttpURLConnection.HTTP_OK)
);
}
}

/**
* We can GET a JsonObject from the server if the response is 302 (redirect)
* with a new Location header.
* @throws IOException If something goes wrong.
*/
@Test
public void getJsonObjectMovedTemporarilyRedirect() throws IOException {
final int port = this.resource.port();
final JsonObject json = Json.createObjectBuilder()
.add("from", "server")
.build();
try(
final MkContainer container = new MkGrizzlyContainer().next(
new MkAnswer.Simple(HttpURLConnection.HTTP_MOVED_TEMP)
.withHeader(
"Location", "http://localhost:" + port
)
).next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
json.toString()
)
).start(port)
) {
final JsonResources resources = new JsonResources.JdkHttp(true);
final Resource response = resources.get(container.home());
MatcherAssert.assertThat(
response.asJsonObject(),
Matchers.equalTo(json)
);
MatcherAssert.assertThat(
response.statusCode(),
Matchers.equalTo(HttpURLConnection.HTTP_OK)
);
}
}

/**
* We can GET a JsonObject from the server if the response is 303 (redirect)
* with a new Location header.
* @throws IOException If something goes wrong.
*/
@Test
public void getJsonObjectSeeOtherRedirect() throws IOException {
final int port = this.resource.port();
final JsonObject json = Json.createObjectBuilder()
.add("from", "server")
.build();
try(
final MkContainer container = new MkGrizzlyContainer().next(
new MkAnswer.Simple(HttpURLConnection.HTTP_SEE_OTHER)
.withHeader(
"Location", "http://localhost:" + port
)
).next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
json.toString()
)
).start(port)
) {
final JsonResources resources = new JsonResources.JdkHttp(true);
final Resource response = resources.get(container.home());
MatcherAssert.assertThat(
response.asJsonObject(),
Matchers.equalTo(json)
);
MatcherAssert.assertThat(
response.statusCode(),
Matchers.equalTo(HttpURLConnection.HTTP_OK)
);
}
}

/**
* We can GET a JsonObject from the server with no access token and
* request having other headers.
Expand Down

0 comments on commit 0acdf71

Please sign in to comment.