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 (#2451)

Fixes: #2307
  • Loading branch information
mpkorstanje authored Jan 1, 2022
1 parent 3d0842e commit 49a336d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
* [Core] Support multiple doc strings types with same content type ([#2421](https://github.com/cucumber/cucumber-jvm/pull/2421) Postelnicu George)
- When transforming doc strings in addition to the content type Cucumber will
also look at the type used in the step definition to disambiguate between
doc string types.
- When transforming doc strings in addition to the content type Cucumber will
also look at the type used in the step definition to disambiguate between
doc string types.
* [Guice] Automatically detect `InjectorSource` ([#2432 ](https://github.com/cucumber/cucumber-jvm/pull/2432) Postelnicu George)

### Changed
Expand All @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed
* [JUnit Platform] Delay plugin creation until test execution ([#2442](https://github.com/cucumber/cucumber-jvm/pull/2442) M.P. Korstanje)
* [Core] Display curl-like error message for more url output stream problems ([#2451](https://github.com/cucumber/cucumber-jvm/pull/2451) M.P. Korstanje)

## [7.1.0] (2021-11-28)

Expand Down
47 changes: 30 additions & 17 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,12 +70,13 @@ 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());
}
}

Map<String, List<String>> requestHeaders = urlConnection.getRequestProperties();
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestMethod(method.name());
Expand All @@ -87,22 +89,32 @@ 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);
}

/**
* return the request body
*
* @param urlConnection the http connection
* @param requestHeaders the headers sent
* @return the response body
* @throws IOException if an exception occurs
*/
private static HttpURLConnection openConnection(URL url, HttpMethod method) throws IOException {
try {
return (HttpURLConnection) url.openConnection();
} catch (IOException e) {
throw createCurlLikeException(method.name(), url, Collections.emptyMap(), Collections.emptyMap(), "", e);
}
}

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 static String getResponseBody(
HttpURLConnection urlConnection, Map<String, List<String>> requestHeaders
)
Expand All @@ -118,19 +130,20 @@ 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;
}
}
}

static IOException createCurlLikeException(
private static IOException createCurlLikeException(
String method,
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",
Expand All @@ -139,7 +152,7 @@ static IOException createCurlLikeException(
url,
headersToString("> ", requestHeaders),
headersToString("< ", responseHeaders),
responseBody));
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 49a336d

Please sign in to comment.