-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from julian-michelmann/feature/java8-compatibi…
…lity Java 1.8 compatibility
- Loading branch information
Showing
9 changed files
with
97 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/test/java/org/wiremock/integrations/testcontainers/testsupport/http/HttpResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.wiremock.integrations.testcontainers.testsupport.http; | ||
|
||
public class HttpResponse { | ||
|
||
String body; | ||
int statusCode; | ||
public HttpResponse(String body, int statusCode) { | ||
this.body = body; | ||
this.statusCode = statusCode; | ||
} | ||
|
||
public String getBody() { | ||
return body; | ||
} | ||
|
||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
} |
72 changes: 41 additions & 31 deletions
72
src/test/java/org/wiremock/integrations/testcontainers/testsupport/http/TestHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,56 @@ | ||
package org.wiremock.integrations.testcontainers.testsupport.http; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.BufferedReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public final class TestHttpClient { | ||
private final HttpClient client; | ||
|
||
private TestHttpClient(HttpClient.Version version) { | ||
client = HttpClient.newBuilder().version(version).build(); | ||
public HttpResponse send(HttpURLConnection connection) throws IOException { | ||
InputStream inputStream = connection.getInputStream(); | ||
|
||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
StringBuilder response = new StringBuilder(); | ||
|
||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
response.append(line); | ||
} | ||
reader.close(); | ||
|
||
|
||
|
||
return new HttpResponse(response.toString(), connection.getResponseCode()); | ||
} | ||
|
||
public static TestHttpClient newInstance() { | ||
return new TestHttpClient(HttpClient.Version.HTTP_1_1); | ||
} | ||
public HttpResponse get(String uri) throws IOException { | ||
URL url = new URL(uri); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
|
||
public HttpResponse<String> send(HttpRequest request) throws IOException, InterruptedException { | ||
return client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
return send(connection); | ||
} | ||
|
||
public HttpResponse<String> get(String uri) throws IOException, InterruptedException { | ||
HttpRequest request = newRequestBuilder() | ||
.uri(URI.create(uri)) | ||
.GET() | ||
.build(); | ||
return send(request); | ||
} | ||
|
||
public HttpResponse<String> post(String uri, String body) throws IOException, InterruptedException { | ||
HttpRequest request = newRequestBuilder() | ||
.uri(URI.create(uri)) | ||
.header("Content-Type", "application/json") | ||
.POST(HttpRequest.BodyPublishers.ofString(body)) | ||
.build(); | ||
return send(request); | ||
} | ||
public HttpResponse post(String uri, String body) throws IOException { | ||
URL url = new URL(uri); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Content-Type", "application/json"); | ||
connection.setRequestProperty("Accept", "application/json"); | ||
connection.setDoOutput(true); | ||
connection.setConnectTimeout(10000); | ||
|
||
try (OutputStream outputStream = connection.getOutputStream()) { | ||
byte[] input = body.getBytes(StandardCharsets.UTF_8); | ||
outputStream.write(input, 0, input.length); | ||
} | ||
|
||
private static HttpRequest.Builder newRequestBuilder() { | ||
return HttpRequest.newBuilder() | ||
.timeout(Duration.ofSeconds(10)); | ||
return send(connection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters