Skip to content

Commit dde334d

Browse files
authored
add integration with old okhttp (via allure-framework#289)
1 parent 597a75d commit dde334d

File tree

8 files changed

+268
-3
lines changed

8 files changed

+268
-3
lines changed

allure-okhttp/build.gradle.kts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
description = "Allure OkHttp Integration"
2+
3+
val agent by configurations.creating
4+
5+
val okhttpVersion = "2.7.5"
6+
7+
dependencies {
8+
agent("org.aspectj:aspectjweaver")
9+
10+
compile(project(":allure-attachments"))
11+
compile("com.squareup.okhttp:okhttp:$okhttpVersion")
12+
13+
testCompile("com.github.tomakehurst:wiremock")
14+
testCompile("org.jboss.resteasy:resteasy-client")
15+
testCompile("org.assertj:assertj-core")
16+
testCompile("org.junit.jupiter:junit-jupiter-api")
17+
testCompile("org.mockito:mockito-core")
18+
testCompile("org.slf4j:slf4j-simple")
19+
testCompile(project(":allure-java-commons-test"))
20+
testCompile(project(":allure-junit-platform"))
21+
testRuntime("org.junit.jupiter:junit-jupiter-engine")
22+
}
23+
24+
tasks.named<Jar>("jar") {
25+
manifest {
26+
attributes(mapOf(
27+
"Automatic-Module-Name" to "io.qameta.allure.okhttp"
28+
))
29+
}
30+
}
31+
32+
tasks.named<Test>("test") {
33+
useJUnitPlatform()
34+
doFirst {
35+
jvmArgs("-javaagent:${agent.singleFile}")
36+
}
37+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package io.qameta.allure.okhttp;
2+
3+
import io.qameta.allure.attachment.AttachmentData;
4+
import io.qameta.allure.attachment.AttachmentProcessor;
5+
import io.qameta.allure.attachment.DefaultAttachmentProcessor;
6+
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
7+
import io.qameta.allure.attachment.http.HttpRequestAttachment;
8+
import io.qameta.allure.attachment.http.HttpResponseAttachment;
9+
import com.squareup.okhttp.Interceptor;
10+
import com.squareup.okhttp.Request;
11+
import com.squareup.okhttp.RequestBody;
12+
import com.squareup.okhttp.Response;
13+
import com.squareup.okhttp.ResponseBody;
14+
15+
import java.io.IOException;
16+
import java.nio.charset.StandardCharsets;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
import okio.Buffer;
22+
23+
/**
24+
* Allure interceptor logger for OkHttp.
25+
*/
26+
public class AllureOkHttp implements Interceptor {
27+
28+
private String requestTemplatePath = "http-request.ftl";
29+
private String responseTemplatePath = "http-response.ftl";
30+
31+
public AllureOkHttp setRequestTemplate(final String templatePath) {
32+
this.requestTemplatePath = templatePath;
33+
return this;
34+
}
35+
36+
public AllureOkHttp setResponseTemplate(final String templatePath) {
37+
this.responseTemplatePath = templatePath;
38+
return this;
39+
}
40+
41+
/**
42+
* @deprecated use {@link #setRequestTemplate(String)} instead.
43+
* Scheduled for removal in 3.0 release.
44+
*/
45+
@Deprecated
46+
public AllureOkHttp withRequestTemplate(final String templatePath) {
47+
return setRequestTemplate(templatePath);
48+
}
49+
50+
/**
51+
* @deprecated use {@link #setResponseTemplate(String)} instead.
52+
* Scheduled for removal in 3.0 release.
53+
*/
54+
@Deprecated
55+
public AllureOkHttp withResponseTemplate(final String templatePath) {
56+
return setResponseTemplate(templatePath);
57+
}
58+
59+
@Override
60+
public Response intercept(final Chain chain) throws IOException {
61+
final AttachmentProcessor<AttachmentData> processor = new DefaultAttachmentProcessor();
62+
63+
final Request request = chain.request();
64+
final String requestUrl = request.url().toString();
65+
final HttpRequestAttachment.Builder requestAttachmentBuilder = HttpRequestAttachment.Builder
66+
.create("Request", requestUrl)
67+
.setMethod(request.method())
68+
.setHeaders(toMapConverter(request.headers().toMultimap()));
69+
70+
final RequestBody requestBody = request.body();
71+
if (Objects.nonNull(requestBody)) {
72+
requestAttachmentBuilder.setBody(readRequestBody(requestBody));
73+
}
74+
final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
75+
processor.addAttachment(requestAttachment, new FreemarkerAttachmentRenderer(requestTemplatePath));
76+
77+
final Response response = chain.proceed(request);
78+
final HttpResponseAttachment.Builder responseAttachmentBuilder = HttpResponseAttachment.Builder
79+
.create("Response")
80+
.setResponseCode(response.code())
81+
.setHeaders(toMapConverter(response.headers().toMultimap()));
82+
83+
final Response.Builder responseBuilder = response.newBuilder();
84+
85+
final ResponseBody responseBody = response.body();
86+
87+
if (Objects.nonNull(responseBody)) {
88+
final byte[] bytes = responseBody.bytes();
89+
responseAttachmentBuilder.setBody(new String(bytes, StandardCharsets.UTF_8));
90+
responseBuilder.body(ResponseBody.create(responseBody.contentType(), bytes));
91+
}
92+
93+
final HttpResponseAttachment responseAttachment = responseAttachmentBuilder.build();
94+
processor.addAttachment(responseAttachment, new FreemarkerAttachmentRenderer(responseTemplatePath));
95+
96+
return responseBuilder.build();
97+
}
98+
99+
private static Map<String, String> toMapConverter(final Map<String, List<String>> items) {
100+
final Map<String, String> result = new HashMap<>();
101+
items.forEach((key, value) -> result.put(key, String.join("; ", value)));
102+
return result;
103+
}
104+
105+
private static String readRequestBody(final RequestBody requestBody) throws IOException {
106+
final Buffer buffer = new Buffer();
107+
requestBody.writeTo(buffer);
108+
return buffer.readString(StandardCharsets.UTF_8);
109+
}
110+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package io.qameta.allure.okhttp;
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import io.qameta.allure.model.Attachment;
5+
import io.qameta.allure.model.TestResult;
6+
import io.qameta.allure.test.AllureResults;
7+
import com.squareup.okhttp.OkHttpClient;
8+
import com.squareup.okhttp.Request;
9+
import com.squareup.okhttp.Response;
10+
import com.squareup.okhttp.ResponseBody;
11+
import org.junit.jupiter.api.AfterEach;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import java.io.IOException;
16+
import java.util.Objects;
17+
import java.util.function.Consumer;
18+
import java.util.stream.Stream;
19+
20+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
23+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
24+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
25+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
26+
import static io.qameta.allure.test.RunUtils.runWithinTestContext;
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.fail;
29+
30+
/**
31+
* @author charlie (Dmitry Baev).
32+
*/
33+
class AllureOkHttp3Test {
34+
35+
private static final String BODY_STRING = "Hello world!";
36+
37+
private WireMockServer server;
38+
39+
@BeforeEach
40+
void setUp() {
41+
server = new WireMockServer(options().dynamicPort());
42+
server.start();
43+
configureFor(server.port());
44+
45+
stubFor(get(urlEqualTo("/hello"))
46+
.willReturn(aResponse()
47+
.withBody(BODY_STRING)));
48+
}
49+
50+
@AfterEach
51+
void tearDown() {
52+
if (Objects.nonNull(server)) {
53+
server.stop();
54+
}
55+
}
56+
57+
@Test
58+
void shouldCreateRequestAttachment() {
59+
final Request request = new Request.Builder()
60+
.url(server.url("hello"))
61+
.build();
62+
63+
final AllureResults results = execute(request, checkBody(BODY_STRING));
64+
65+
assertThat(results.getTestResults())
66+
.flatExtracting(TestResult::getAttachments)
67+
.extracting(Attachment::getName)
68+
.contains("Request");
69+
}
70+
71+
@Test
72+
void shouldCreateResponseAttachment() {
73+
final Request request = new Request.Builder()
74+
.url(server.url("hello"))
75+
.build();
76+
77+
final AllureResults results = execute(request, checkBody(BODY_STRING));
78+
79+
assertThat(results.getTestResults())
80+
.flatExtracting(TestResult::getAttachments)
81+
.extracting(Attachment::getName)
82+
.contains("Response");
83+
}
84+
85+
@SafeVarargs
86+
protected final AllureResults execute(final Request request, final Consumer<Response>... matchers) {
87+
final OkHttpClient client = new OkHttpClient();
88+
client.interceptors().add(new AllureOkHttp());
89+
90+
return runWithinTestContext(() -> {
91+
try {
92+
final Response response = client.newCall(request).execute();
93+
Stream.of(matchers).forEach(matcher -> matcher.accept(response));
94+
} catch (IOException e) {
95+
throw new RuntimeException("Could not execute request " + request, e);
96+
}
97+
});
98+
}
99+
100+
protected Consumer<Response> checkBody(final String expectedBody) {
101+
return response -> {
102+
try {
103+
final ResponseBody body = response.body();
104+
if (Objects.isNull(body)) {
105+
fail("empty response body");
106+
}
107+
assertThat(body.string()).isEqualTo(expectedBody);
108+
} catch (IOException e) {
109+
fail("could not read response body");
110+
}
111+
};
112+
}
113+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allure.results.directory=build/allure-results
2+
allure.label.epic=#project.description#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.slf4j.simpleLogger.log.org.eclipse.jetty=off

allure-okhttp3/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ description = "Allure OkHttp3 Integration"
22

33
val agent by configurations.creating
44

5+
val okhttpVersion = "3.10.0"
6+
57
dependencies {
68
agent("org.aspectj:aspectjweaver")
79

810
compile(project(":allure-attachments"))
9-
compile("com.squareup.okhttp3:okhttp")
11+
compile("com.squareup.okhttp3:okhttp:$okhttpVersion")
1012

1113
testCompile("com.github.tomakehurst:wiremock")
1214
testCompile("org.jboss.resteasy:resteasy-client")
@@ -22,7 +24,7 @@ dependencies {
2224
tasks.named<Jar>("jar") {
2325
manifest {
2426
attributes(mapOf(
25-
"Automatic-Module-Name" to "io.qameta.allure.okhttp"
27+
"Automatic-Module-Name" to "io.qameta.allure.okhttp3"
2628
))
2729
}
2830
}

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ configure(subprojects) {
8181
dependency("com.github.tomakehurst:wiremock:2.18.0")
8282
dependency("com.google.inject:guice:4.2.0")
8383
dependency("com.google.testing.compile:compile-testing:0.15")
84-
dependency("com.squareup.okhttp3:okhttp:3.10.0")
8584
dependency("com.squareup.retrofit2:retrofit:2.4.0")
8685
dependency("commons-io:commons-io:2.6")
8786
dependency("io.github.benas:random-beans:3.7.0")

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include("allure-junit4")
1919
include("allure-junit4-aspect")
2020
include("allure-junit5")
2121
include("allure-model")
22+
include("allure-okhttp")
2223
include("allure-okhttp3")
2324
include("allure-rest-assured")
2425
include("allure-selenide")

0 commit comments

Comments
 (0)