diff --git a/README.md b/README.md index 7d18bbe..541e08e 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Latest release: com.github.dzieciou.testing curl-logger - 1.0.2 + 2.0.0 ``` @@ -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: diff --git a/curl-logger-core/pom.xml b/curl-logger-core/pom.xml new file mode 100644 index 0000000..6a086b9 --- /dev/null +++ b/curl-logger-core/pom.xml @@ -0,0 +1,46 @@ + + + + curl-logger + com.github.dzieciou.testing + 2.0.0-SNAPSHOT + + 4.0.0 + + curl-logger-core + + + + org.slf4j + slf4j-api + + + org.apache.httpcomponents + httpclient + 4.5.3 + + + io.rest-assured + rest-assured + + + + + org.testng + testng + + + org.hamcrest + hamcrest-all + + + com.google.guava + guava + 23.0 + test + + + + \ No newline at end of file diff --git a/src/main/java/com/github/dzieciou/testing/curl/CurlCommand.java b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/CurlCommand.java similarity index 98% rename from src/main/java/com/github/dzieciou/testing/curl/CurlCommand.java rename to curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/CurlCommand.java index 6fa716f..06190c2 100644 --- a/src/main/java/com/github/dzieciou/testing/curl/CurlCommand.java +++ b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/CurlCommand.java @@ -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; @@ -187,9 +187,6 @@ public String getUser() { private static class Serializer { private static final Map SHORT_PARAMETER_NAMES = new HashMap<>(); - private final Platform targetPlatform; - private final boolean useShortForm; - private final boolean printMultiliner; static { SHORT_PARAMETER_NAMES.put("--user", "-u"); @@ -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; @@ -282,7 +283,8 @@ public String serialize(CurlCommand curl) { List> 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))); diff --git a/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Http2Curl.java b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Http2Curl.java new file mode 100644 index 0000000..d570423 --- /dev/null +++ b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Http2Curl.java @@ -0,0 +1,33 @@ +package com.github.dzieciou.testing.curl.core; + +import java.io.IOException; +import org.apache.http.HttpRequest; + + +public abstract class Http2Curl { + + 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; +} diff --git a/src/main/java/com/github/dzieciou/testing/curl/Options.java b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Options.java similarity index 98% rename from src/main/java/com/github/dzieciou/testing/curl/Options.java rename to curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Options.java index 7370fe5..e415070 100644 --- a/src/main/java/com/github/dzieciou/testing/curl/Options.java +++ b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Options.java @@ -1,4 +1,4 @@ -package com.github.dzieciou.testing.curl; +package com.github.dzieciou.testing.curl.core; import java.util.Optional; diff --git a/src/main/java/com/github/dzieciou/testing/curl/Platform.java b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Platform.java similarity index 92% rename from src/main/java/com/github/dzieciou/testing/curl/Platform.java rename to curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Platform.java index fe198c1..196d73d 100644 --- a/src/main/java/com/github/dzieciou/testing/curl/Platform.java +++ b/curl-logger-core/src/main/java/com/github/dzieciou/testing/curl/core/Platform.java @@ -1,4 +1,4 @@ -package com.github.dzieciou.testing.curl; +package com.github.dzieciou.testing.curl.core; public enum Platform { diff --git a/src/test/java/com/github/dzieciou/testing/curl/CurlCommandTest.java b/curl-logger-core/src/test/java/com/github/dzieciou/testing/curl/core/CurlCommandTest.java similarity index 93% rename from src/test/java/com/github/dzieciou/testing/curl/CurlCommandTest.java rename to curl-logger-core/src/test/java/com/github/dzieciou/testing/curl/core/CurlCommandTest.java index bc16bec..ed610c1 100644 --- a/src/test/java/com/github/dzieciou/testing/curl/CurlCommandTest.java +++ b/curl-logger-core/src/test/java/com/github/dzieciou/testing/curl/core/CurlCommandTest.java @@ -1,4 +1,4 @@ -package com.github.dzieciou.testing.curl; +package com.github.dzieciou.testing.curl.core; import static org.hamcrest.MatcherAssert.assertThat; diff --git a/src/test/resources/logback-test.xml b/curl-logger-core/src/test/resources/logback-test.xml similarity index 100% rename from src/test/resources/logback-test.xml rename to curl-logger-core/src/test/resources/logback-test.xml diff --git a/curl-logger-restassured/pom.xml b/curl-logger-restassured/pom.xml new file mode 100644 index 0000000..b1f4392 --- /dev/null +++ b/curl-logger-restassured/pom.xml @@ -0,0 +1,64 @@ + + + + curl-logger + com.github.dzieciou.testing + 2.0.0-SNAPSHOT + + 4.0.0 + + curl-logger-restassured + + + + com.github.dzieciou.testing + curl-logger-core + ${project.version} + + + org.slf4j + slf4j-api + + + org.apache.httpcomponents + httpclient + + + io.rest-assured + rest-assured + + + + + org.testng + testng + + + org.hamcrest + hamcrest-all + + + org.mockito + mockito-all + + + uk.org.lidalia + slf4j-test + + + org.mock-server + mockserver-netty + + + com.google.guava + guava + + + jackson-databind + com.fasterxml.jackson.core + + + + \ No newline at end of file diff --git a/src/main/java/com/github/dzieciou/testing/curl/CurlLoggingInterceptor.java b/curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingInterceptor.java similarity index 86% rename from src/main/java/com/github/dzieciou/testing/curl/CurlLoggingInterceptor.java rename to curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingInterceptor.java index 9615abb..b7cc198 100644 --- a/src/main/java/com/github/dzieciou/testing/curl/CurlLoggingInterceptor.java +++ b/curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingInterceptor.java @@ -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; @@ -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) { diff --git a/src/main/java/com/github/dzieciou/testing/curl/CurlLoggingRestAssuredConfigFactory.java b/curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingRestAssuredConfigFactory.java similarity index 88% rename from src/main/java/com/github/dzieciou/testing/curl/CurlLoggingRestAssuredConfigFactory.java rename to curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingRestAssuredConfigFactory.java index 94661ae..d313b64 100644 --- a/src/main/java/com/github/dzieciou/testing/curl/CurlLoggingRestAssuredConfigFactory.java +++ b/curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingRestAssuredConfigFactory.java @@ -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 @@ -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. */ @@ -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() { @@ -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) { @@ -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; } diff --git a/src/main/java/com/github/dzieciou/testing/curl/Http2Curl.java b/curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/RestAssuredHttp2Curl.java similarity index 93% rename from src/main/java/com/github/dzieciou/testing/curl/Http2Curl.java rename to curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/RestAssuredHttp2Curl.java index d91d5b0..f07a728 100644 --- a/src/main/java/com/github/dzieciou/testing/curl/Http2Curl.java +++ b/curl-logger-restassured/src/main/java/com/github/dzieciou/testing/curl/restassured/RestAssuredHttp2Curl.java @@ -29,22 +29,12 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package com.github.dzieciou.testing.curl; - -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpEntityEnclosingRequest; -import org.apache.http.HttpRequest; -import org.apache.http.client.methods.HttpRequestWrapper; -import org.apache.http.entity.mime.FormBodyPart; -import org.apache.http.entity.mime.MultipartEntityBuilder; -import org.apache.http.entity.mime.content.ContentBody; -import org.apache.http.impl.client.RequestWrapper; -import org.apache.http.message.BasicHeader; -import org.apache.http.util.EntityUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +package com.github.dzieciou.testing.curl.restassured; +import com.github.dzieciou.testing.curl.core.CurlCommand; +import com.github.dzieciou.testing.curl.core.Http2Curl; +import com.github.dzieciou.testing.curl.core.Options; +import io.restassured.internal.multipart.RestAssuredMultiPartEntity; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.reflect.Field; @@ -61,25 +51,34 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; - -import io.restassured.internal.multipart.RestAssuredMultiPartEntity; +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpEntityEnclosingRequest; +import org.apache.http.HttpRequest; +import org.apache.http.client.methods.HttpRequestWrapper; +import org.apache.http.entity.mime.FormBodyPart; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.ContentBody; +import org.apache.http.impl.client.RequestWrapper; +import org.apache.http.message.BasicHeader; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Generates CURL command for a given HTTP request. */ @SuppressWarnings("deprecation") -public class Http2Curl { +public class RestAssuredHttp2Curl extends Http2Curl { - private static final Logger log = LoggerFactory.getLogger(Http2Curl.class); + private static final Logger log = LoggerFactory.getLogger(RestAssuredHttp2Curl.class); private static final List NON_BINARY_CONTENT_TYPES = Arrays.asList( "application/x-www-form-urlencoded", "application/json"); - private final Options options; - - public Http2Curl(Options options) { - this.options = options; + public RestAssuredHttp2Curl(Options options) { + super(options); } private static String getContent(FormBodyPart bodyPart) throws IOException { @@ -151,23 +150,9 @@ private static Field getField(Class clazz, String fieldName) } } - /** - * 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(HttpRequest request) throws Exception { - - CurlCommand curl = http2curl(request); - options.getCurlUpdater().ifPresent(updater -> updater.accept(curl)); - return curl - .asString(options.getTargetPlatform(), options.useShortForm(), options.printMultiliner()); - } - + @Override @SuppressWarnings("deprecation") - private CurlCommand http2curl(HttpRequest request) + protected CurlCommand http2curl(HttpRequest request) throws NoSuchFieldException, IllegalAccessException, IOException { Set ignoredHeaders = new HashSet<>(); List
headers = Arrays.asList(request.getAllHeaders()); @@ -313,7 +298,7 @@ private void handlePart(FormBodyPart bodyPart, CurlCommand curl) { } private void handleNotIgnoredHeaders(List
headers, Set ignoredHeaders, - CurlCommand curl) { + CurlCommand curl) { headers .stream() .filter(h -> !ignoredHeaders.contains(h.getName())) diff --git a/src/test/java/com/github/dzieciou/testing/curl/CurlLoggingInterceptorTest.java b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingInterceptorTest.java similarity index 97% rename from src/test/java/com/github/dzieciou/testing/curl/CurlLoggingInterceptorTest.java rename to curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingInterceptorTest.java index b93065c..b31a32a 100644 --- a/src/test/java/com/github/dzieciou/testing/curl/CurlLoggingInterceptorTest.java +++ b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingInterceptorTest.java @@ -1,4 +1,4 @@ -package com.github.dzieciou.testing.curl; +package com.github.dzieciou.testing.curl.restassured; import static io.restassured.RestAssured.config; import static io.restassured.RestAssured.given; @@ -12,6 +12,7 @@ import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; +import com.github.dzieciou.testing.curl.core.Options; import io.restassured.config.HttpClientConfig; import io.restassured.config.RestAssuredConfig; import org.apache.http.client.HttpClient; diff --git a/src/test/java/com/github/dzieciou/testing/curl/CurlLoggingRestAssuredConfigFactoryTest.java b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingRestAssuredConfigFactoryTest.java similarity index 85% rename from src/test/java/com/github/dzieciou/testing/curl/CurlLoggingRestAssuredConfigFactoryTest.java rename to curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingRestAssuredConfigFactoryTest.java index 6c4334d..048de58 100644 --- a/src/test/java/com/github/dzieciou/testing/curl/CurlLoggingRestAssuredConfigFactoryTest.java +++ b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/CurlLoggingRestAssuredConfigFactoryTest.java @@ -1,6 +1,18 @@ -package com.github.dzieciou.testing.curl; +package com.github.dzieciou.testing.curl.restassured; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.not; +import static org.mockserver.integration.ClientAndServer.startClientAndServer; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +import com.github.dzieciou.testing.curl.core.Options; +import io.restassured.RestAssured; +import io.restassured.config.HttpClientConfig; +import io.restassured.config.RestAssuredConfig; +import java.io.IOException; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; @@ -15,19 +27,6 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.io.IOException; - -import io.restassured.RestAssured; -import io.restassured.config.HttpClientConfig; -import io.restassured.config.RestAssuredConfig; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.not; -import static org.mockserver.integration.ClientAndServer.startClientAndServer; -import static org.mockserver.model.HttpRequest.request; -import static org.mockserver.model.HttpResponse.response; - public class CurlLoggingRestAssuredConfigFactoryTest { private static final int MOCK_PORT = 9999; @@ -45,7 +44,8 @@ public void setupMock() { @Test public void shouldIncludeCurlInterceptorWhenCreatingConfig() { RestAssuredConfig updatedConfig = CurlLoggingRestAssuredConfigFactory.createConfig(); - AbstractHttpClient updateClientConfig = (AbstractHttpClient) updatedConfig.getHttpClientConfig().httpClientInstance(); + AbstractHttpClient updateClientConfig = (AbstractHttpClient) updatedConfig.getHttpClientConfig() + .httpClientInstance(); assertThat(updateClientConfig, new ContainsRequestInterceptor(CurlLoggingInterceptor.class)); } @@ -67,17 +67,20 @@ public HttpClient createHttpClient() { final RestAssuredConfig config = RestAssuredConfig.config() .httpClient(httpClientConfig); - RestAssuredConfig updatedConfig = CurlLoggingRestAssuredConfigFactory.updateConfig(config, Options.builder().build()); + RestAssuredConfig updatedConfig = CurlLoggingRestAssuredConfigFactory + .updateConfig(config, Options.builder().build()); // original configuration has not been modified assertThat(updatedConfig, not(equalTo(config))); - AbstractHttpClient clientConfig = (AbstractHttpClient) config.getHttpClientConfig().httpClientInstance(); + AbstractHttpClient clientConfig = (AbstractHttpClient) config.getHttpClientConfig() + .httpClientInstance(); assertThat(clientConfig, not(new ContainsRequestInterceptor(CurlLoggingInterceptor.class))); assertThat(clientConfig, new ContainsRequestInterceptor(MyRequestInerceptor.class)); assertThat(updatedConfig.getHttpClientConfig().params().get("TestParam"), equalTo("TestValue")); // curl logging interceptor is included - AbstractHttpClient updateClientConfig = (AbstractHttpClient) updatedConfig.getHttpClientConfig().httpClientInstance(); + AbstractHttpClient updateClientConfig = (AbstractHttpClient) updatedConfig.getHttpClientConfig() + .httpClientInstance(); assertThat(updateClientConfig, new ContainsRequestInterceptor(CurlLoggingInterceptor.class)); // original interceptors are preserved in new configuration @@ -91,7 +94,8 @@ public HttpClient createHttpClient() { @Test public void shouldSentRequestWhenUsingConfigurationFactory() { RestAssured.given() - .config(CurlLoggingRestAssuredConfigFactory.createConfig(Options.builder().useShortForm().build())) + .config(CurlLoggingRestAssuredConfigFactory + .createConfig(Options.builder().useShortForm().build())) .baseUri(MOCK_BASE_URI) .port(MOCK_PORT) .when() @@ -106,11 +110,13 @@ public void closeMock() { } - private static class ContainsRequestInterceptor extends TypeSafeDiagnosingMatcher { + private static class ContainsRequestInterceptor extends + TypeSafeDiagnosingMatcher { private Class expectedRequestedInterceptor; - public ContainsRequestInterceptor(Class expectedRequestedInterceptor) { + public ContainsRequestInterceptor( + Class expectedRequestedInterceptor) { this.expectedRequestedInterceptor = expectedRequestedInterceptor; } @@ -133,7 +139,8 @@ public void describeTo(Description description) { private static class MyRequestInerceptor implements HttpRequestInterceptor { @Override - public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { + public void process(HttpRequest httpRequest, HttpContext httpContext) + throws HttpException, IOException { } } diff --git a/src/test/java/com/github/dzieciou/testing/curl/Http2CurlTest.java b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/RestAssuredHttp2CurlTest.java similarity index 90% rename from src/test/java/com/github/dzieciou/testing/curl/Http2CurlTest.java rename to curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/RestAssuredHttp2CurlTest.java index 325e37b..dfb49df 100644 --- a/src/test/java/com/github/dzieciou/testing/curl/Http2CurlTest.java +++ b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/RestAssuredHttp2CurlTest.java @@ -1,6 +1,14 @@ -package com.github.dzieciou.testing.curl; +package com.github.dzieciou.testing.curl.restassured; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import com.github.dzieciou.testing.curl.core.Options; +import com.github.dzieciou.testing.curl.core.Platform; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; @@ -12,15 +20,8 @@ import org.apache.http.message.BasicNameValuePair; import org.testng.annotations.Test; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - @Test(groups = "unit") -public class Http2CurlTest { +public class RestAssuredHttp2CurlTest { @Test public void shouldPrintGetRequestProperly() throws Exception { @@ -54,7 +55,8 @@ public void shouldNotPrintInvalidBasicAuthnUserCredentials() throws Exception { String invalidEncodedCredentials = "xxx"; getRequest.addHeader("Authorization", "Basic " + invalidEncodedCredentials); assertThat(getNonWindowsHttp2Curl().generateCurl(getRequest), - equalTo("curl 'http://test.com:8080/items/query?x=y#z' -H 'Authorization: Basic xxx' --compressed -k -v")); + equalTo( + "curl 'http://test.com:8080/items/query?x=y#z' -H 'Authorization: Basic xxx' --compressed -k -v")); } @Test @@ -99,7 +101,8 @@ public void shouldPrintMultipleCookieHeadersInMultipleParameters() throws Except headRequest.addHeader("Cookie", "X=Y; A=B"); headRequest.addHeader("Cookie", "D=E"); assertThat(getNonWindowsHttp2Curl().generateCurl(headRequest), - equalTo("curl 'http://test.com/items/12345' -X HEAD -H 'Cookie: X=Y; A=B' -H 'Cookie: D=E' --compressed -k -v")); + equalTo( + "curl 'http://test.com/items/12345' -X HEAD -H 'Cookie: X=Y; A=B' -H 'Cookie: D=E' --compressed -k -v")); } @@ -129,7 +132,7 @@ public void shouldPrintMultilineRequestProperly() throws Exception { .printMultiliner().build(); // then - assertThat(new Http2Curl(options).generateCurl(postRequest), + assertThat(new RestAssuredHttp2Curl(options).generateCurl(postRequest), equalTo( "curl 'http://google.pl/' \\\n -H 'Content-Type: application/x-www-form-urlencoded' \\\n -d 'param1=param1_value¶m2=param2_value' \\\n --compressed \\\n -k \\\n -v")); } @@ -146,13 +149,13 @@ public void shouldWriteParametersInLongForm() throws Exception { .printSingleliner().build(); // then - assertThat(new Http2Curl(options).generateCurl(getRequest), + assertThat(new RestAssuredHttp2Curl(options).generateCurl(getRequest), equalTo( "curl 'http://test.com:8080/items/query?x=y#z' --header 'Host: H' --compressed --insecure --verbose")); } - public Http2Curl getNonWindowsHttp2Curl() { - return new Http2Curl( + public RestAssuredHttp2Curl getNonWindowsHttp2Curl() { + return new RestAssuredHttp2Curl( Options.builder().targetPlatform(Platform.UNIX).useShortForm().printSingleliner().build()); } diff --git a/src/test/java/com/github/dzieciou/testing/curl/UsingWithHttpClientTest.java b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/UsingWithHttpClientTest.java similarity index 85% rename from src/test/java/com/github/dzieciou/testing/curl/UsingWithHttpClientTest.java rename to curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/UsingWithHttpClientTest.java index b7ef564..2525cf8 100644 --- a/src/test/java/com/github/dzieciou/testing/curl/UsingWithHttpClientTest.java +++ b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/UsingWithHttpClientTest.java @@ -1,6 +1,7 @@ -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.client.HttpClient; import org.apache.http.client.methods.HttpGet; diff --git a/src/test/java/com/github/dzieciou/testing/curl/UsingWithRestAssuredTest.java b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/UsingWithRestAssuredTest.java similarity index 87% rename from src/test/java/com/github/dzieciou/testing/curl/UsingWithRestAssuredTest.java rename to curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/UsingWithRestAssuredTest.java index 84b9ee6..651fb65 100644 --- a/src/test/java/com/github/dzieciou/testing/curl/UsingWithRestAssuredTest.java +++ b/curl-logger-restassured/src/test/java/com/github/dzieciou/testing/curl/restassured/UsingWithRestAssuredTest.java @@ -1,4 +1,4 @@ -package com.github.dzieciou.testing.curl; +package com.github.dzieciou.testing.curl.restassured; import static io.restassured.RestAssured.config; @@ -11,12 +11,15 @@ import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; +import com.github.dzieciou.testing.curl.core.Options; +import com.github.dzieciou.testing.curl.core.Platform; import io.restassured.config.HttpClientConfig; import io.restassured.config.RestAssuredConfig; import io.restassured.http.Cookie; import io.restassured.http.Cookies; import java.io.File; import java.io.IOException; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; @@ -38,9 +41,18 @@ public class UsingWithRestAssuredTest { private static final int MOCK_PORT = 9999; private static final String MOCK_HOST = "localhost"; private static final String MOCK_BASE_URI = "http://" + MOCK_HOST; + private static final String FILE_TO_ATTACH_URI = "/fileToAttach.json"; private MockServerClient mockServer; + private static File getFileToAttach() { + try { + return new File(UsingWithRestAssuredTest.class.getResource(FILE_TO_ATTACH_URI).toURI()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + private RestAssuredConfig getRestAssuredConfig(Consumer curlConsumer) { return config() .httpClient(httpClientConfig() @@ -165,7 +177,7 @@ public void basicIntegrationTest() { } @Test(groups = "end-to-end-samples") - public void shouldPrintPostRequestWithMultipartDataProperly() { + public void shouldPrintPostRequestWithMultipartDataProperly() throws URISyntaxException { Consumer curlConsumer = mock(Consumer.class); @@ -174,14 +186,14 @@ public void shouldPrintPostRequestWithMultipartDataProperly() { .baseUri(MOCK_BASE_URI) .port(MOCK_PORT) .config(getRestAssuredConfig(curlConsumer)) - .multiPart(new File("README.md")) + .multiPart(getFileToAttach()) .formParam("parameterX", "parameterXValue") .when().post("/"); //@formatter:on verify(curlConsumer).accept( "curl 'http://localhost:" + MOCK_PORT - + "/' -X POST -H 'Accept: */*' -F 'file=@README.md;type=application/octet-stream' -F 'parameterX=parameterXValue;type=text/plain' --compressed -k -v"); + + "/' -X POST -H 'Accept: */*' -F 'file=@fileToAttach.json;type=application/octet-stream' -F 'parameterX=parameterXValue;type=text/plain' --compressed -k -v"); } @@ -216,13 +228,13 @@ public void shouldPrintMultipartWithMixedType() { .port(MOCK_PORT) .config(getRestAssuredConfig(curlConsumer) .multiPartConfig(multiPartConfig().defaultSubtype("mixed"))) - .multiPart("myfile", new File("README.md"), "application/json") + .multiPart("myfile", getFileToAttach(), "application/json") .when().post("/"); //@formatter:on verify(curlConsumer).accept( "curl 'http://localhost:" + MOCK_PORT - + "/' -X POST -H 'Accept: */*' -H 'Content-Type: multipart/mixed' -F 'myfile=@README.md;type=application/json' --compressed -k -v"); + + "/' -X POST -H 'Accept: */*' -H 'Content-Type: multipart/mixed' -F 'myfile=@fileToAttach.json;type=application/json' --compressed -k -v"); } @AfterClass @@ -230,23 +242,6 @@ public void closeMock() { mockServer.stop(); } - private class MyHttpClientFactory implements HttpClientConfig.HttpClientFactory { - - public final Consumer curlConsumer; - - private MyHttpClientFactory(Consumer curlConsumer) { - this.curlConsumer = curlConsumer; - } - - @SuppressWarnings("deprecation") - @Override - public HttpClient createHttpClient() { - AbstractHttpClient client = new DefaultHttpClient(); - client.addRequestInterceptor(new CurlTestingInterceptor(curlConsumer)); - return client; - } - } - private static class CurlTestingInterceptor implements HttpRequestInterceptor { public final Consumer curlConsumer; @@ -259,19 +254,18 @@ public CurlTestingInterceptor(Consumer curlConsumer) { public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { - - Options options = Options.builder() - .printSingleliner() - .targetPlatform(Platform.UNIX) - .useShortForm() - .updateCurl(curl -> curl - .removeHeader("Host") - .removeHeader("User-Agent") - .removeHeader("Connection")) - .build(); + Options options = Options.builder() + .printSingleliner() + .targetPlatform(Platform.UNIX) + .useShortForm() + .updateCurl(curl -> curl + .removeHeader("Host") + .removeHeader("User-Agent") + .removeHeader("Connection")) + .build(); try { - curlConsumer.accept(new Http2Curl(options).generateCurl(request)); + curlConsumer.accept(new RestAssuredHttp2Curl(options).generateCurl(request)); } catch (Exception e) { throw new RuntimeException(e); } @@ -279,4 +273,21 @@ public void process(HttpRequest request, HttpContext context) } } + private class MyHttpClientFactory implements HttpClientConfig.HttpClientFactory { + + public final Consumer curlConsumer; + + private MyHttpClientFactory(Consumer curlConsumer) { + this.curlConsumer = curlConsumer; + } + + @SuppressWarnings("deprecation") + @Override + public HttpClient createHttpClient() { + AbstractHttpClient client = new DefaultHttpClient(); + client.addRequestInterceptor(new CurlTestingInterceptor(curlConsumer)); + return client; + } + } + } diff --git a/curl-logger-restassured/src/test/resources/fileToAttach.json b/curl-logger-restassured/src/test/resources/fileToAttach.json new file mode 100644 index 0000000..e69de29 diff --git a/pom.xml b/pom.xml index 69ac27b..bc63d75 100644 --- a/pom.xml +++ b/pom.xml @@ -1,239 +1,251 @@ - 4.0.0 - com.github.dzieciou.testing - curl-logger - 1.0.3 - jar - https://github.com/dzieciou/curl-logger - com.github.dzieciou.testing:curl-logger - Logs REST-assured requests as curl commands - 2016 - - GitHub Issue Tracking - https://github.com/dzieciou/curl-logger/issues - - - - BSD License, Revised - https://opensource.org/licenses/BSD-3-Clause - - - - - Maciej Gawinecki - dzieciou - mgawinecki at gmail.com - - Developer - - - - - UTF-8 - 1.8 - 1.8 - + 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"> + 4.0.0 + com.github.dzieciou.testing + curl-logger + 2.0.0-SNAPSHOT + pom + + + curl-logger-restassured + curl-logger-core + + + https://github.com/dzieciou/curl-logger + com.github.dzieciou.testing:curl-logger + Logs REST-assured requests as curl commands + 2016 + + GitHub Issue Tracking + https://github.com/dzieciou/curl-logger/issues + + + + BSD License, Revised + https://opensource.org/licenses/BSD-3-Clause + + + + + Maciej Gawinecki + dzieciou + mgawinecki at gmail.com + + Developer + + + + + UTF-8 + 1.8 + 1.8 + + + - - org.slf4j - slf4j-api - 1.7.25 - - - org.apache.httpcomponents + + org.slf4j + slf4j-api + 1.7.25 + + + org.apache.httpcomponents + httpclient + 4.5.3 + provided + + + io.rest-assured + rest-assured + 3.0.3 + provided + + httpclient - 4.5.3 - provided - - - io.rest-assured - rest-assured - 3.0.3 - provided - - - httpclient - org.apache.httpcomponents - - - commons-lang3 - org.apache.commons - - - - - org.testng - testng - 6.11 - test - - - org.hamcrest - hamcrest-all - 1.3 - test - - - org.mockito - mockito-all - 2.0.2-beta - test - - - uk.org.lidalia - slf4j-test - 1.2.0 - test - - - guava - com.google.guava - - - slf4j-api - org.slf4j - - - commons-lang3 - org.apache.commons - - - joda-time - joda-time - - - - - org.mock-server - mockserver-netty - 3.10.8 - test - - - jsr305 - com.google.code.findbugs - - - jackson-databind - com.fasterxml.jackson.core - - - scala-library - org.scala-lang - - - slf4j-api - org.slf4j - - - jackson-core - com.fasterxml.jackson.core - - - guava - com.google.guava - - - jackson-annotations - com.fasterxml.jackson.core - - - ch.qos.logback - logback-classic - - - - - com.google.guava + org.apache.httpcomponents + + + commons-lang3 + org.apache.commons + + + + + + + org.testng + testng + 6.11 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + org.mockito + mockito-all + 2.0.2-beta + test + + + uk.org.lidalia + slf4j-test + 1.2.0 + test + + guava - 23.0 - test - - + com.google.guava + + + slf4j-api + org.slf4j + + + commons-lang3 + org.apache.commons + + + joda-time + joda-time + + + + + org.mock-server + mockserver-netty + 3.10.8 + test + + + jsr305 + com.google.code.findbugs + + jackson-databind com.fasterxml.jackson.core - 2.9.0 - test - + + + scala-library + org.scala-lang + + + slf4j-api + org.slf4j + + + jackson-core + com.fasterxml.jackson.core + + + guava + com.google.guava + + + jackson-annotations + com.fasterxml.jackson.core + + + ch.qos.logback + logback-classic + + + + + com.google.guava + guava + 23.0 + test + + + jackson-databind + com.fasterxml.jackson.core + 2.9.0 + test + + + - - - release - - - - org.apache.maven.plugins - maven-gpg-plugin - 1.6 - - - sign-artifacts - verify - - sign - - - - - - org.apache.maven.plugins - maven-source-plugin - 2.4 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.3 - - - attach-javadocs - - jar - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.7 - true - - ossrh - https://oss.sonatype.org/ - true - - - - - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - scm:git:git@github.com:dzieciou/curl-logger.git - scm:git:git@github.com:dzieciou/curl-logger.git - git@github.com:dzieciou/curl-logger.git - + + + release + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.4 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + attach-javadocs + + jar + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true + + ossrh + https://oss.sonatype.org/ + true + + + + + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + scm:git:git@github.com:dzieciou/curl-logger.git + scm:git:git@github.com:dzieciou/curl-logger.git + git@github.com:dzieciou/curl-logger.git + \ No newline at end of file