Skip to content

Commit 5751c1f

Browse files
committed
feat(compatibility-suite): Implement steps for V3 message consumer
1 parent 8a131f9 commit 5751c1f

File tree

10 files changed

+410
-30
lines changed

10 files changed

+410
-30
lines changed

compatibility-suite/src/test/groovy/steps/shared/MockServerSharedSteps.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import static au.com.dius.pact.consumer.MockHttpServerKt.mockServer
2323
import static au.com.dius.pact.core.model.PactReaderKt.queryStringToMap
2424
import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
2525
import static steps.shared.SharedSteps.configureBody
26+
import static steps.shared.SharedSteps.determineContentType
2627

2728
class MockServerData {
2829
RequestResponsePact pact
@@ -96,7 +97,9 @@ class MockServerSharedSteps {
9697
}
9798

9899
if (entry['body']) {
99-
configureBody(entry['body'], request)
100+
def part = configureBody(entry['body'], determineContentType(entry['body'], request.contentTypeHeader()))
101+
request.body = part.body
102+
request.headers.putAll(part.headers)
100103
}
101104

102105
IProviderInfo providerInfo = new ProviderInfo()

compatibility-suite/src/test/groovy/steps/shared/SharedHttpProvider.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import org.apache.hc.core5.http.io.entity.StringEntity
4242

4343
import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
4444
import static steps.shared.SharedSteps.configureBody
45+
import static steps.shared.SharedSteps.determineContentType
4546

4647
@SuppressWarnings(['ThrowRuntimeException', 'AbcMetric'])
4748
class SharedHttpProvider {
@@ -112,7 +113,10 @@ class SharedHttpProvider {
112113
}
113114

114115
if (entry['body']) {
115-
configureBody(entry['body'], interaction.response)
116+
def part = configureBody(entry['body'], determineContentType(entry['body'],
117+
interaction.response.contentTypeHeader()))
118+
interaction.response.body = part.body
119+
interaction.response.headers.putAll(part.headers)
116120
}
117121

118122
Pact pact = new RequestResponsePact(new Provider('p'),

compatibility-suite/src/test/groovy/steps/shared/SharedSteps.groovy

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ class SharedSteps {
6767
}
6868

6969
if (entry['body']) {
70-
configureBody(entry['body'], interaction.request)
70+
def part = configureBody(entry['body'], determineContentType(entry['body'],
71+
interaction.request.contentTypeHeader()))
72+
interaction.request.body = part.body
73+
interaction.request.headers.putAll(part.headers)
7174
}
7275

7376
if (entry['matching rules']) {
@@ -103,7 +106,10 @@ class SharedSteps {
103106
}
104107

105108
if (entry['response body']) {
106-
configureBody(entry['response body'], interaction.response)
109+
def part = configureBody(entry['response body'], determineContentType(entry['response body'],
110+
interaction.response.contentTypeHeader()))
111+
interaction.response.body = part.body
112+
interaction.response.headers.putAll(part.headers)
107113
}
108114

109115
if (entry['response matching rules']) {
@@ -123,36 +129,38 @@ class SharedSteps {
123129
}
124130
}
125131

126-
static void configureBody(String entry, HttpPart part) {
132+
static HttpPart configureBody(String entry, String detectedContentType) {
133+
def request = new Request()
127134
if (entry.startsWith('JSON:')) {
128-
part.headers['content-type'] = ['application/json']
129-
part.body = OptionalBody.body(entry[5..-1].bytes, new ContentType('application/json'))
135+
request.headers['content-type'] = ['application/json']
136+
request.body = OptionalBody.body(entry[5..-1].bytes, new ContentType('application/json'))
130137
} else if (entry.startsWith('XML:')) {
131-
part.headers['content-type'] = ['application/xml']
132-
part.body = OptionalBody.body(entry[4..-1].trim().bytes, new ContentType('application/xml'))
138+
request.headers['content-type'] = ['application/xml']
139+
request.body = OptionalBody.body(entry[4..-1].trim().bytes, new ContentType('application/xml'))
133140
} else if (entry.startsWith('file:')) {
134141
if (entry.endsWith('-body.xml')) {
135142
File contents = new File("pact-compatibility-suite/fixtures/${entry[5..-1].trim()}")
136143
def fixture = new XmlSlurper().parse(contents)
137144
def contentType = fixture.contentType.toString()
138-
part.headers['content-type'] = [contentType]
139-
part.body = OptionalBody.body(fixture.contents.text(), new ContentType(contentType))
145+
request.headers['content-type'] = [contentType]
146+
request.body = OptionalBody.body(fixture.contents.text(), new ContentType(contentType))
140147
} else {
141-
String contentType = determineContentType(entry, part)
142-
part.headers['content-type'] = [contentType]
148+
String contentType = detectedContentType
149+
request.headers['content-type'] = [contentType]
143150
File contents = new File("pact-compatibility-suite/fixtures/${entry[5..-1].trim()}")
144151
contents.withInputStream {
145-
part.body = OptionalBody.body(it.readAllBytes(), new ContentType(contentType))
152+
request.body = OptionalBody.body(it.readAllBytes(), new ContentType(contentType))
146153
}
147154
}
148155
} else {
149-
part.headers['content-type'] = [determineContentType(entry, part)]
150-
part.body = OptionalBody.body(entry)
156+
request.headers['content-type'] = [detectedContentType]
157+
request.body = OptionalBody.body(entry)
151158
}
159+
request
152160
}
153161

154-
private static String determineContentType(String entry, HttpPart part) {
155-
String contentType = part.contentTypeHeader()
162+
static String determineContentType(String entry, String contentTypeHeader) {
163+
String contentType = contentTypeHeader
156164
if (entry.endsWith('.json')) {
157165
contentType = 'application/json'
158166
} else if (entry.endsWith('.xml')) {

compatibility-suite/src/test/groovy/steps/v2/HttpConsumer.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import static au.com.dius.pact.consumer.MockHttpServerKt.mockServer
1414
import static au.com.dius.pact.core.model.PactReaderKt.queryStringToMap
1515
import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
1616
import static steps.shared.SharedSteps.configureBody
17+
import static steps.shared.SharedSteps.determineContentType
1718

1819
class HttpConsumer {
1920
CompatibilitySuiteWorld world
@@ -56,7 +57,10 @@ class HttpConsumer {
5657
}
5758

5859
if (entry['body']) {
59-
configureBody(entry['body'], interaction.request)
60+
def part = configureBody(entry['body'], determineContentType(entry['body'],
61+
interaction.request.contentTypeHeader()))
62+
interaction.request.body = part.body
63+
interaction.request.headers.putAll(part.headers)
6064
}
6165

6266
mockServerData.pact = new RequestResponsePact(new Provider('p'),

compatibility-suite/src/test/groovy/steps/v3/Generators.groovy

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import io.cucumber.java.en.Then
1414
import io.cucumber.java.en.When
1515

1616
import static steps.shared.SharedSteps.configureBody
17+
import static steps.shared.SharedSteps.determineContentType
1718

1819
@SuppressWarnings('SpaceAfterOpeningBrace')
1920
class Generators {
@@ -31,7 +32,9 @@ class Generators {
3132
request = new Request('GET', '/path/one')
3233
def entry = dataTable.entries().first()
3334
if (entry['body']) {
34-
configureBody(entry['body'], request)
35+
def part = configureBody(entry['body'], determineContentType(entry['body'], request.contentTypeHeader()))
36+
request.body = part.body
37+
request.headers.putAll(part.headers)
3538
}
3639
if (entry['generators']) {
3740
JsonValue json
@@ -52,7 +55,9 @@ class Generators {
5255
response = new Response()
5356
def entry = dataTable.entries().first()
5457
if (entry['body']) {
55-
configureBody(entry['body'], response)
58+
def part = configureBody(entry['body'], determineContentType(entry['body'], response.contentTypeHeader()))
59+
response.body = part.body
60+
response.headers.putAll(part.headers)
5661
}
5762
if (entry['generators']) {
5863
JsonValue json
@@ -98,11 +103,15 @@ class Generators {
98103
JsonParser.INSTANCE.parseString(generatedRequest.body.valueAsString()) : null
99104
}
100105

101-
@Then('the body value for {string} will have been replaced with a {string}')
106+
@Then('the body value for {string} will have been replaced with a(n) {string}')
102107
void the_body_value_for_will_have_been_replaced_with_a_value(String path, String type) {
103108
def originalElement = JsonUtils.INSTANCE.fetchPath(originalJson, path)
104109
def element = JsonUtils.INSTANCE.fetchPath(generatedJson, path)
105110
assert originalElement != element
111+
matchTypeOfElement(type, element)
112+
}
113+
114+
static void matchTypeOfElement(String type, JsonValue element) {
106115
switch (type) {
107116
case 'integer' -> {
108117
assert element.type() == 'Integer'

compatibility-suite/src/test/groovy/steps/v3/HttpMatching.groovy

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import io.cucumber.java.en.When
1616
import static au.com.dius.pact.core.matchers.RequestMatching.requestMismatches
1717
import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
1818
import static steps.shared.SharedSteps.configureBody
19+
import static steps.shared.SharedSteps.determineContentType
1920

2021
@SuppressWarnings('SpaceAfterOpeningBrace')
2122
class HttpMatching {
@@ -40,7 +41,9 @@ class HttpMatching {
4041
expectedRequest = new Request()
4142
def entry = dataTable.entries().first()
4243
if (entry['body']) {
43-
configureBody(entry['body'], expectedRequest)
44+
def part = configureBody(entry['body'], determineContentType(entry['body'], expectedRequest.contentTypeHeader()))
45+
expectedRequest.body = part.body
46+
expectedRequest.headers.putAll(part.headers)
4447
}
4548

4649
if (entry['matching rules']) {
@@ -62,7 +65,10 @@ class HttpMatching {
6265
receivedRequests << new Request()
6366
def entry = dataTable.entries().first()
6467
if (entry['body']) {
65-
configureBody(entry['body'], receivedRequests[0])
68+
def part = configureBody(entry['body'], determineContentType(entry['body'],
69+
receivedRequests[0].contentTypeHeader()))
70+
receivedRequests[0].body = part.body
71+
receivedRequests[0].headers.putAll(part.headers)
6672
}
6773
}
6874

@@ -71,7 +77,9 @@ class HttpMatching {
7177
for (entry in dataTable.entries()) {
7278
def request = new Request()
7379
if (entry['body']) {
74-
configureBody(entry['body'], request)
80+
def part = configureBody(entry['body'], determineContentType(entry['body'], request.contentTypeHeader()))
81+
request.body = part.body
82+
request.headers.putAll(part.headers)
7583
}
7684
receivedRequests << request
7785
}

0 commit comments

Comments
 (0)