Skip to content

Commit

Permalink
[#18] Factor out conversation form REST-assured requests to curl-logg…
Browse files Browse the repository at this point in the history
…er-restassured module
  • Loading branch information
dzieciou committed Oct 16, 2017
1 parent 1d73119 commit d08d2ae
Show file tree
Hide file tree
Showing 19 changed files with 531 additions and 364 deletions.
4 changes: 2 additions & 2 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>1.0.2</version>
<version>2.0.0</version>
</dependency>
```

Expand Down Expand Up @@ -258,7 +258,7 @@ only prints warning (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):
(https://github.com/dzieciou/curl-logger/issues/4)

1.0.0:

Expand Down
46 changes: 46 additions & 0 deletions curl-logger-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>curl-logger</artifactId>
<groupId>com.github.dzieciou.testing</groupId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>curl-logger-core</artifactId>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>

<!-- Test scope -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.github.dzieciou.testing.curl;
package com.github.dzieciou.testing.curl.core;


import java.util.ArrayList;
Expand Down Expand Up @@ -187,9 +187,6 @@ public String getUser() {
private static class Serializer {

private static final Map<String, String> SHORT_PARAMETER_NAMES = new HashMap<>();
private final Platform targetPlatform;
private final boolean useShortForm;
private final boolean printMultiliner;

static {
SHORT_PARAMETER_NAMES.put("--user", "-u");
Expand All @@ -202,6 +199,10 @@ private static class Serializer {
SHORT_PARAMETER_NAMES.put("--verbose", "-v");
}

private final Platform targetPlatform;
private final boolean useShortForm;
private final boolean printMultiliner;

public Serializer(Platform targetPlatform, boolean useShortForm, boolean printMultiliner) {
this.targetPlatform = targetPlatform;
this.useShortForm = useShortForm;
Expand Down Expand Up @@ -282,7 +283,8 @@ public String serialize(CurlCommand curl) {
List<List<String>> command = new ArrayList<>();

command
.add(line(useShortForm, "curl", escapeString(curl.url).replaceAll("[[{}\\\\]]", "\\$&")));
.add(line(useShortForm, "curl",
escapeString(curl.url).replaceAll("[[{}\\\\]]", "\\$&")));

curl.method.ifPresent(method -> command.add(line(useShortForm, "--request", method)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.dzieciou.testing.curl.core;

import java.io.IOException;
import org.apache.http.HttpRequest;


public abstract class Http2Curl<RequestT> {

protected final Options options;

public Http2Curl(Options options) {
this.options = options;
}

/**
* Generates single-line CURL command for a given HTTP request.
*
* @param request HTTP request
* @return CURL command
* @throws Exception if failed to generate CURL command
*/
public String generateCurl(RequestT request) throws Exception {

CurlCommand curl = http2curl(request);
options.getCurlUpdater().ifPresent(updater -> updater.accept(curl));
return curl
.asString(options.getTargetPlatform(), options.useShortForm(), options.printMultiliner());
}

@SuppressWarnings("deprecation")
protected abstract CurlCommand http2curl(RequestT request)
throws NoSuchFieldException, IllegalAccessException, IOException;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.dzieciou.testing.curl;
package com.github.dzieciou.testing.curl.core;


import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.dzieciou.testing.curl;
package com.github.dzieciou.testing.curl.core;


public enum Platform {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.dzieciou.testing.curl;
package com.github.dzieciou.testing.curl.core;


import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
File renamed without changes.
64 changes: 64 additions & 0 deletions curl-logger-restassured/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>curl-logger</artifactId>
<groupId>com.github.dzieciou.testing</groupId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>curl-logger-restassured</artifactId>

<dependencies>
<dependency>
<groupId>com.github.dzieciou.testing</groupId>
<artifactId>curl-logger-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>

<!-- Test scope -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>slf4j-test</artifactId>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.dzieciou.testing.curl;
package com.github.dzieciou.testing.curl.restassured;

import com.github.dzieciou.testing.curl.core.Options;
import java.io.IOException;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
Expand All @@ -18,11 +19,11 @@ public class CurlLoggingInterceptor implements HttpRequestInterceptor {

private final Options options;

private final Http2Curl http2Curl;
private final RestAssuredHttp2Curl http2Curl;

public CurlLoggingInterceptor(Options options) {
this.options = options;
http2Curl = new Http2Curl(options);
http2Curl = new RestAssuredHttp2Curl(options);
}

private static void printStacktrace(StringBuffer sb) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.github.dzieciou.testing.curl;
package com.github.dzieciou.testing.curl.restassured;


import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.AbstractHttpClient;

import java.lang.reflect.Field;

import com.github.dzieciou.testing.curl.core.Options;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RestAssuredConfig;
import java.lang.reflect.Field;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.AbstractHttpClient;

/**
* Creates `RestAssuredConfig` objects that configure REST-assured to log each HTTP request as CURL
Expand Down Expand Up @@ -47,7 +46,7 @@ public static RestAssuredConfig updateConfig(RestAssuredConfig config) {
/**
* Updates a given REST-assured configuration to generate curl command using custom options.
*
* @param config an original configuration to update
* @param config an original configuration to update
* @param options options defining curl generation
* @return updated configuration; note original configuration remain unchanged.
*/
Expand All @@ -56,7 +55,8 @@ public static RestAssuredConfig updateConfig(RestAssuredConfig config, Options o
return config
.httpClient(config.getHttpClientConfig()
.reuseHttpClientInstance()
.httpClientFactory(new MyHttpClientFactory(originalFactory, new CurlLoggingInterceptor(options))));
.httpClientFactory(
new MyHttpClientFactory(originalFactory, new CurlLoggingInterceptor(options))));
}

private static Options getDefaultOptions() {
Expand All @@ -72,7 +72,8 @@ private static HttpClientConfig.HttpClientFactory getHttpClientFactory(RestAssur
Field f = HttpClientConfig.class.getDeclaredField("httpClientFactory");
f.setAccessible(true);
HttpClientConfig httpClientConfig = config.getHttpClientConfig();
HttpClientConfig.HttpClientFactory httpClientFactory = (HttpClientConfig.HttpClientFactory) f.get(httpClientConfig);
HttpClientConfig.HttpClientFactory httpClientFactory = (HttpClientConfig.HttpClientFactory) f
.get(httpClientConfig);
f.setAccessible(false);
return httpClientFactory;
} catch (NoSuchFieldException | IllegalAccessException e) {
Expand All @@ -86,7 +87,7 @@ private static class MyHttpClientFactory implements HttpClientConfig.HttpClientF
private final CurlLoggingInterceptor curlLoggingInterceptor;

public MyHttpClientFactory(HttpClientConfig.HttpClientFactory wrappedFactory,
CurlLoggingInterceptor curlLoggingInterceptor) {
CurlLoggingInterceptor curlLoggingInterceptor) {
this.wrappedFactory = wrappedFactory;
this.curlLoggingInterceptor = curlLoggingInterceptor;
}
Expand Down
Loading

0 comments on commit d08d2ae

Please sign in to comment.