diff --git a/core/src/main/java/io/cucumber/core/plugin/UrlOutputStream.java b/core/src/main/java/io/cucumber/core/plugin/UrlOutputStream.java index 04e2004ee8..db5f1f6120 100644 --- a/core/src/main/java/io/cucumber/core/plugin/UrlOutputStream.java +++ b/core/src/main/java/io/cucumber/core/plugin/UrlOutputStream.java @@ -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; @@ -69,7 +70,7 @@ public void close() throws IOException { } private Optional sendRequest(URL url, HttpMethod method, boolean setHeaders) throws IOException { - HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + HttpURLConnection urlConnection = openConnection(url, method); if (setHeaders) { for (Entry header : option.getHeaders()) { urlConnection.setRequestProperty(header.getKey(), header.getValue()); @@ -87,21 +88,39 @@ private Optional 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> 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> requestHeaders @@ -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; } @@ -130,16 +149,17 @@ static IOException createCurlLikeException( URL url, Map> requestHeaders, Map> 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> headers) { diff --git a/core/src/test/java/io/cucumber/core/plugin/UrlOutputStreamTest.java b/core/src/test/java/io/cucumber/core/plugin/UrlOutputStreamTest.java index ec432e2709..d9ead04832 100644 --- a/core/src/test/java/io/cucumber/core/plugin/UrlOutputStreamTest.java +++ b/core/src/test/java/io/cucumber/core/plugin/UrlOutputStreamTest.java @@ -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;