Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
dzieciou committed May 15, 2021
2 parents dead3b7 + 02f5968 commit 01fdcd7
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 29 deletions.
45 changes: 33 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Latest release:
<dependency>
<groupId>com.github.dzieciou.testing</groupId>
<artifactId>curl-logger</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
</dependency>
```

Expand Down Expand Up @@ -222,6 +222,15 @@ curl 'http://google.pl/' -header 'Content-Type: application/x-www-form-urlencode
By default `CurlRestAssuredConfigFactory#createConfig` create configuration that prints
a curl command parameters in short form.

### Printing request methods

By default, curl-logger does not print HTTP method for GET requests and POST requests without data. To always print
request method, method configure curl-logger as follows:

```java
Options.builder().alwaysPrintMethod().build();
```

### Updating curl command before print

The library provides a way to modify curl command before
Expand Down Expand Up @@ -332,8 +341,17 @@ given()

## Releases

2.1.0:
* Support for `alwaysPrintMethod` option that enables always printing HTTP method of a request (many thanks to Luigi
Cardito for his pull request)

2.0.1:
* Bug fix: Do not reuse HTTP client instance across multiple/parallel requests (fix for [#37](https://github.com/dzieciou/curl-logger/issues/37))
* Bug fix: Make CurlHandler interface public (fix for [#56](https://github.com/dzieciou/curl-logger/issues/56))

2.0.0:
* Fix to invalid escaping of characters longer than 8 bits (#47, thanks to Srepfler Srdan for reporting it)
* Fix to invalid escaping of characters longer than 8 bits ([#47](https://github.com/dzieciou/curl-logger/issues/47),
thanks to Srepfler Srdan for reporting it)
* Tested with latest REST-assured (4.3.1)
* Support for custom log levels (thanks to Jérémie Bresson for pull request)
* Support for custom curl handlers
Expand All @@ -343,26 +361,26 @@ given()
1.0.5:
* Upgrade to REST-assured 4.0.0.
* Update test scope dependencies.
* Fix character escaping for both POSIX and Windows platforms (many thanks to Chirag008 for rausung
the issue: https://github.com/dzieciou/curl-logger/issues/25)
* Bug fix: fix character escaping for both POSIX and Windows platforms (many thanks to Chirag008 for raising
the issue: [#25](https://github.com/dzieciou/curl-logger/issues/25))

1.0.4:
* Bug fix: HTTPS protocol was not always recognized correctly
(https://github.com/dzieciou/curl-logger/issues/17). Many thanks to pafitchett-ks for troubleshooting.
([#17](https://github.com/dzieciou/curl-logger/issues/17)). Many thanks to pafitchett-ks for troubleshooting.
* Support slf4j 1.8.0-beta2.
* Support rest-assured 3.2.0.

1.0.3:
* Bug fix: Invalid basic authentication headers are failing curl generation
(https://github.com/dzieciou/curl-logger/issues/15)
([#15](https://github.com/dzieciou/curl-logger/issues/15))

1.0.2:
* Bug fix: CurlLogger was failing when multiple Cookie headers are present in HTTP Request. Now it
only prints warning (https://github.com/dzieciou/curl-logger/issues/13)
only prints warning ([#37](https://github.com/dzieciou/curl-logger/issues/13))

1.0.1:
* Bug fix: `CurlLoggingRestAssuredConfigBuilder` was not updating `RestAssuredConfig` properly
(https://github.com/dzieciou/curl-logger/issues/4):
([#4](https://github.com/dzieciou/curl-logger/issues/4)):

1.0.0:

Expand All @@ -375,16 +393,19 @@ only prints warning (https://github.com/dzieciou/curl-logger/issues/13)

* Added possibility to print shorter versions of curl parameters, e.g., -v instead of --verbose
* Added possibility to modify a curl command before printing it, inspired by the suggestion from
Alexey Dushen (blacky0x0): https://github.com/dzieciou/curl-logger/issues/2.
Alexey Dushen (blacky0x0): [#2](https://github.com/dzieciou/curl-logger/issues/2).

0.6:
* Fixed bug: For each cookie a separate `-b cookie=content` parameter was generated (https://github.com/dzieciou/curl-logger/issues/4)
* Fixed bug: For each cookie a separate `-b cookie=content` parameter was generated
([#6](https://github.com/dzieciou/curl-logger/issues/4))
* Upgraded to REST-assured 3.0.2
* Simplified curl-logger configuration with `CurlLoggingRestAssuredConfigBuilder`, based on suggestion from Tao Zhang (https://github.com/dzieciou/curl-logger/issues/4)
* Simplified curl-logger configuration with `CurlLoggingRestAssuredConfigBuilder`, based on suggestion from Tao Zhang
([#4](https://github.com/dzieciou/curl-logger/issues/4))

0.5:

* Upgraded to REST-assured 3.0.1 that contains important fix impacting curl-logger: Cookie attributes are no longer sent in request in accordance with RFC6265.
* Upgraded to REST-assured 3.0.1 that contains important fix impacting curl-logger: Cookie attributes are no longer sent
in request in accordance with RFC6265.
* Fixed bug: cookie values can have = sign inside so we need to get around them somehow
* Cookie strings are now escaped
* `CurlLoggingInterceptor`'s constructor is now protected to make extending it possible
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dzieciou.testing</groupId>
<artifactId>curl-logger</artifactId>
<version>2.0.0</version>
<version>2.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<url>https://github.com/dzieciou/curl-logger</url>
<name>com.github.dzieciou.testing:curl-logger</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private String escape(char c) {
case '\'': return "\\'";
case '\t': return "\\t";
case '\r': return "\\r";
// '@' character has a special meaning in --data-binary (loadin a file)
// '@' character has a special meaning in --data-binary (loading a file)
// So we need to escape it
case '@': return escapeAsHex(c);
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.dzieciou.testing.curl;

interface CurlHandler {
public interface CurlHandler {

/**
* Handle generated curl expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static RestAssuredConfig updateConfig(RestAssuredConfig config, Options o
handlers);
return config
.httpClient(config.getHttpClientConfig()
.reuseHttpClientInstance()
.dontReuseHttpClientInstance()
.httpClientFactory(new MyHttpClientFactory(originalFactory, interceptor)));
}

Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/github/dzieciou/testing/curl/Http2Curl.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ public Headers(List<Header> toProcess) {
}
}

@SuppressWarnings("deprecation")
private CurlCommand http2curl(HttpRequest request)
throws NoSuchFieldException, IllegalAccessException, IOException {
private CurlCommand http2curl(HttpRequest request) throws IOException {

Headers headers = new Headers(Arrays.asList(request.getAllHeaders()));
CurlCommand curl = new CurlCommand();
Expand All @@ -203,12 +201,16 @@ private CurlCommand http2curl(HttpRequest request)
}

String requestMethod = request.getRequestLine().getMethod();
if ("GET".equals(requestMethod)) {
// skip
} else if ("POST".equals(requestMethod) && curl.hasData()) {
// skip
} else {
if (options.alwaysPrintMethod()) {
curl.setMethod(requestMethod);
} else {
if ("GET".equals(requestMethod)) {
// skip
} else if ("POST".equals(requestMethod) && curl.hasData()) {
// skip
} else {
curl.setMethod(requestMethod);
}
}

headers.toProcess = handleAuthenticationHeader(headers.toProcess, curl);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/github/dzieciou/testing/curl/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Options {
private Consumer<CurlCommand> curlUpdater;
private Platform targetPlatform = Platform.RECOGNIZE_AUTOMATICALLY;
private boolean escapeNonAscii;
private boolean alwaysPrintMethod;

private Options() {
}
Expand Down Expand Up @@ -49,6 +50,8 @@ public Platform getTargetPlatform() {
return targetPlatform;
}

public boolean alwaysPrintMethod() { return alwaysPrintMethod; }

public static class Builder {

private final Options options = new Options();
Expand Down Expand Up @@ -143,6 +146,14 @@ public Builder useLogLevel(Level level) {
return this;
}

/**
* Always print HTTP method, including GET method.
*/
public Builder alwaysPrintMethod() {
options.alwaysPrintMethod = true;
return this;
}

public Options build() {
return options;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dzieciou.testing.curl;


import io.restassured.specification.RequestSpecification;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
Expand All @@ -21,6 +22,7 @@
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RestAssuredConfig;

import static io.restassured.RestAssured.config;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -101,6 +103,24 @@ public void shouldSentRequestWhenUsingConfigurationFactory() {
.statusCode(200);
}

@Test
public void shouldSentSameRequestTwice() {
// Verifying fix for https://github.com/dzieciou/curl-logger/issues/37

//@formatter:off
RequestSpecification request = RestAssured.given()
.baseUri(MOCK_BASE_URI)
.port(MOCK_PORT)
.config(CurlRestAssuredConfigFactory.createConfig())
.body("anything")
.when();

request.post("/");

request.post("/");
//@formatter:on
}

@AfterClass
public void closeMock() {
mockServer.stop();
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/github/dzieciou/testing/curl/Http2CurlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ public void shouldWriteParametersInLongForm() throws Exception {
"curl 'http://test.com:8080/items/query?x=y#z' --header 'Host: H' --compressed --insecure --verbose"));
}

@Test
public void shouldPrintGetRequestWithMethodProperly() throws Exception {
HttpGet getRequest = new HttpGet("http://test.com:8080/items/query?x=y#z");

Options options = Options.builder().targetPlatform(Platform.UNIX).useShortForm()
.printSingleliner().alwaysPrintMethod().build();

assertThat(new Http2Curl(options).generateCurl(getRequest),
equalTo("curl 'http://test.com:8080/items/query?x=y#z' -X GET --compressed -k -v"));
}

@Test
public void shouldPrintPostRequestWithMethodProperly() throws Exception {
HttpPost postRequest = new HttpPost("http://google.pl/");
List<NameValuePair> postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("param1", "param1_value"));

postRequest.setEntity(new UrlEncodedFormEntity(postParameters));
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");

Options options = Options.builder().targetPlatform(Platform.UNIX).useShortForm()
.printSingleliner().alwaysPrintMethod().build();

assertThat(new Http2Curl(options).generateCurl(postRequest),
equalTo(
"curl 'http://google.pl/' -X POST -H 'Content-Type: application/x-www-form-urlencoded' --data-binary 'param1=param1_value' --compressed -k -v"));
}


public Http2Curl getNonWindowsHttp2Curl() {
return new Http2Curl(
Options.builder().targetPlatform(Platform.UNIX).useShortForm().printSingleliner().build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import io.restassured.specification.RequestSpecification;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.mockserver.client.MockServerClient;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -171,6 +172,7 @@ public void basicIntegrationTest() {
+ "/' -H 'Accept: */*' --compressed -k -v");
}


@Test(groups = "end-to-end-samples")
public void shouldPrintPostRequestWithMultipartDataProperly() {

Expand Down Expand Up @@ -213,7 +215,6 @@ public void shouldPrintBody() {
}



@Test(groups = "end-to-end-samples")
public void shouldPrintBodyWithEncoding() {

Expand Down Expand Up @@ -359,6 +360,12 @@ private MyHttpClientFactory(Consumer<String> curlConsumer) {
public HttpClient createHttpClient() {
AbstractHttpClient client = new DefaultHttpClient();
client.addRequestInterceptor(new CurlTestingInterceptor(curlConsumer));
client.addResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
EntityUtils.consume(response.getEntity());
}
});
return client;
}
}
Expand All @@ -375,7 +382,6 @@ public CurlTestingInterceptor(Consumer<String> curlConsumer) {
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {


Options options = Options.builder()
.printSingleliner()
.targetPlatform(Platform.UNIX)
Expand Down

0 comments on commit 01fdcd7

Please sign in to comment.