Skip to content

Commit 8475311

Browse files
committed
Simulate the DefectDojo Response Headers
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent ab41f7f commit 8475311

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/test/java/io/securecodebox/persistence/defectdojo/service/EndpointServiceTest.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1515
import static org.hamcrest.MatcherAssert.assertThat;
16-
import static org.hamcrest.Matchers.containsInAnyOrder;
17-
import static org.hamcrest.Matchers.hasSize;
16+
import static org.hamcrest.Matchers.*;
1817
import static org.junit.jupiter.api.Assertions.assertAll;
1918

2019
/**
@@ -61,11 +60,13 @@ final class EndpointServiceTest extends WireMockBaseTestCase {
6160

6261
@Test
6362
void search() throws URISyntaxException, IOException {
63+
final var response = readFixtureFile("EndpointService_response_fixture.json");
6464
stubFor(
6565
get("/api/v2/endpoints/?offset=0&limit=100")
6666
.willReturn(
6767
ok()
68-
.withBody(readFixtureFile("EndpointService_response_fixture.json"))
68+
.withHeaders(responseHeaders(response.length()))
69+
.withBody(response)
6970
)
7071
);
7172

@@ -79,11 +80,13 @@ void search() throws URISyntaxException, IOException {
7980

8081
@Test
8182
void search_withQueryParams() throws URISyntaxException, IOException {
83+
final var response = readFixtureFile("EndpointService_response_fixture.json");
8284
stubFor(
8385
get("/api/v2/endpoints/?limit=100&bar=42&offset=0&foo=23")
8486
.willReturn(
8587
ok()
86-
.withBody(readFixtureFile("EndpointService_response_fixture.json"))
88+
.withHeaders(responseHeaders(response.length()))
89+
.withBody(response)
8790
)
8891
);
8992
final var params = new HashMap<String, Object>();

src/test/java/io/securecodebox/persistence/defectdojo/service/WireMockBaseTestCase.java

+25
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
// SPDX-License-Identifier: Apache-2.0
44
package io.securecodebox.persistence.defectdojo.service;
55

6+
import com.github.tomakehurst.wiremock.http.HttpHeader;
7+
import com.github.tomakehurst.wiremock.http.HttpHeaders;
68
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
79
import io.securecodebox.persistence.defectdojo.config.Config;
810
import lombok.Getter;
911
import lombok.experimental.Accessors;
1012

1113
import java.io.IOException;
1214
import java.nio.charset.StandardCharsets;
15+
import java.time.ZoneId;
16+
import java.time.ZonedDateTime;
17+
import java.time.format.DateTimeFormatter;
1318
import java.util.Objects;
1419

1520
/**
@@ -34,4 +39,24 @@ String readFixtureFile(String fixtureFile) throws IOException {
3439
return new String(bytes, StandardCharsets.UTF_8);
3540
}
3641
}
42+
43+
HttpHeaders responseHeaders(int contentLength) {
44+
return HttpHeaders.noHeaders().plus(
45+
new HttpHeader("date", now()),
46+
new HttpHeader("content-type", "application/json"),
47+
new HttpHeader("content-length", String.valueOf(contentLength)),
48+
new HttpHeader("allow", "GET, PUT, PATCH, DELETE, HEAD, OPTIONS"),
49+
new HttpHeader("x-frame-options", "DENY"),
50+
new HttpHeader("x-content-type-options", "nosniff"),
51+
new HttpHeader("referrer-policy", "same-origin"),
52+
new HttpHeader("cross-origin-opener-policy", "same-origin"),
53+
new HttpHeader("vary", "Cookie"),
54+
new HttpHeader("strict-transport-security", "max-age=31536000; includeSubDomains")
55+
);
56+
}
57+
58+
String now() {
59+
return ZonedDateTime.now(ZoneId.of("Europe/Berlin"))
60+
.format(DateTimeFormatter.RFC_1123_DATE_TIME);
61+
}
3762
}

0 commit comments

Comments
 (0)