Skip to content

Commit

Permalink
[Core] Display curl-like error message for more url output stream pro…
Browse files Browse the repository at this point in the history
…blems
  • Loading branch information
mpkorstanje committed Jan 1, 2022
1 parent f0b5641 commit 422bb0c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
56 changes: 38 additions & 18 deletions core/src/main/java/io/cucumber/core/plugin/UrlOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void close() throws IOException {
}

private Optional<String> sendRequest(URL url, HttpMethod method, boolean setHeaders) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
HttpURLConnection urlConnection = openConnection(url, method);
if (setHeaders) {
for (Entry<String, String> header : option.getHeaders()) {
urlConnection.setRequestProperty(header.getKey(), header.getValue());
Expand All @@ -87,21 +88,39 @@ private Optional<String> sendRequest(URL url, HttpMethod method, boolean setHead
}
} else {
urlConnection.setDoOutput(true);
try (OutputStream outputStream = urlConnection.getOutputStream()) {
Files.copy(temp, outputStream);
getResponseBody(urlConnection, requestHeaders);
}
sendRequestBody(urlConnection, requestHeaders, temp);
getResponseBody(urlConnection, requestHeaders);
}
return Optional.ofNullable(redirectMessage);
}

private static void sendRequestBody(HttpURLConnection urlConnection, Map<String, List<String>> requestHeaders, Path requestBody) throws IOException {
try (OutputStream outputStream = urlConnection.getOutputStream()) {
Files.copy(requestBody, outputStream);
} catch (IOException e){
String method = urlConnection.getRequestMethod();
URL url = urlConnection.getURL();
throw createCurlLikeException(method, url, requestHeaders, Collections.emptyMap(), "", e);
}
}

private HttpURLConnection openConnection(URL url, HttpMethod method) throws IOException {
HttpURLConnection urlConnection;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
throw createCurlLikeException(method.name(), url, Collections.emptyMap(), Collections.emptyMap(), "", e);
}
return urlConnection;
}

/**
* return the request body
*
* @param urlConnection the http connection
* @param requestHeaders the headers sent
* @return the response body
* @throws IOException if an exception occurs
* @param urlConnection the http connection
* @param requestHeaders the headers sent
* @return the response body
* @throws IOException if an exception occurs
*/
private static String getResponseBody(
HttpURLConnection urlConnection, Map<String, List<String>> requestHeaders
Expand All @@ -118,7 +137,7 @@ private static String getResponseBody(
if (unsuccessful) {
String method = urlConnection.getRequestMethod();
URL url = urlConnection.getURL();
throw createCurlLikeException(method, url, requestHeaders, responseHeaders, responseBody);
throw createCurlLikeException(method, url, requestHeaders, responseHeaders, responseBody, null);
} else {
return responseBody;
}
Expand All @@ -130,16 +149,17 @@ static IOException createCurlLikeException(
URL url,
Map<String, List<String>> requestHeaders,
Map<String, List<String>> responseHeaders,
String responseBody
String responseBody,
Exception e
) {
return new IOException(String.format(
"%s:\n> %s %s%s%s%s",
"HTTP request failed",
method,
url,
headersToString("> ", requestHeaders),
headersToString("< ", responseHeaders),
responseBody));
"%s:\n> %s %s%s%s%s",
"HTTP request failed",
method,
url,
headersToString("> ", requestHeaders),
headersToString("< ", responseHeaders),
responseBody), e);
}

private static String headersToString(String prefix, Map<String, List<String>> headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

@ExtendWith({ VertxExtension.class })
@ExtendWith(VertxExtension.class)
public class UrlOutputStreamTest {

private static final int TIMEOUT_SECONDS = 15;
Expand Down

0 comments on commit 422bb0c

Please sign in to comment.