Skip to content

Commit

Permalink
feat(compatibility-suite): Implement steps for V3 message consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 1, 2023
1 parent 8a131f9 commit 5751c1f
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import static au.com.dius.pact.consumer.MockHttpServerKt.mockServer
import static au.com.dius.pact.core.model.PactReaderKt.queryStringToMap
import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType

class MockServerData {
RequestResponsePact pact
Expand Down Expand Up @@ -96,7 +97,9 @@ class MockServerSharedSteps {
}

if (entry['body']) {
configureBody(entry['body'], request)
def part = configureBody(entry['body'], determineContentType(entry['body'], request.contentTypeHeader()))
request.body = part.body
request.headers.putAll(part.headers)
}

IProviderInfo providerInfo = new ProviderInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import org.apache.hc.core5.http.io.entity.StringEntity

import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType

@SuppressWarnings(['ThrowRuntimeException', 'AbcMetric'])
class SharedHttpProvider {
Expand Down Expand Up @@ -112,7 +113,10 @@ class SharedHttpProvider {
}

if (entry['body']) {
configureBody(entry['body'], interaction.response)
def part = configureBody(entry['body'], determineContentType(entry['body'],
interaction.response.contentTypeHeader()))
interaction.response.body = part.body
interaction.response.headers.putAll(part.headers)
}

Pact pact = new RequestResponsePact(new Provider('p'),
Expand Down
40 changes: 24 additions & 16 deletions compatibility-suite/src/test/groovy/steps/shared/SharedSteps.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class SharedSteps {
}

if (entry['body']) {
configureBody(entry['body'], interaction.request)
def part = configureBody(entry['body'], determineContentType(entry['body'],
interaction.request.contentTypeHeader()))
interaction.request.body = part.body
interaction.request.headers.putAll(part.headers)
}

if (entry['matching rules']) {
Expand Down Expand Up @@ -103,7 +106,10 @@ class SharedSteps {
}

if (entry['response body']) {
configureBody(entry['response body'], interaction.response)
def part = configureBody(entry['response body'], determineContentType(entry['response body'],
interaction.response.contentTypeHeader()))
interaction.response.body = part.body
interaction.response.headers.putAll(part.headers)
}

if (entry['response matching rules']) {
Expand All @@ -123,36 +129,38 @@ class SharedSteps {
}
}

static void configureBody(String entry, HttpPart part) {
static HttpPart configureBody(String entry, String detectedContentType) {
def request = new Request()
if (entry.startsWith('JSON:')) {
part.headers['content-type'] = ['application/json']
part.body = OptionalBody.body(entry[5..-1].bytes, new ContentType('application/json'))
request.headers['content-type'] = ['application/json']
request.body = OptionalBody.body(entry[5..-1].bytes, new ContentType('application/json'))
} else if (entry.startsWith('XML:')) {
part.headers['content-type'] = ['application/xml']
part.body = OptionalBody.body(entry[4..-1].trim().bytes, new ContentType('application/xml'))
request.headers['content-type'] = ['application/xml']
request.body = OptionalBody.body(entry[4..-1].trim().bytes, new ContentType('application/xml'))
} else if (entry.startsWith('file:')) {
if (entry.endsWith('-body.xml')) {
File contents = new File("pact-compatibility-suite/fixtures/${entry[5..-1].trim()}")
def fixture = new XmlSlurper().parse(contents)
def contentType = fixture.contentType.toString()
part.headers['content-type'] = [contentType]
part.body = OptionalBody.body(fixture.contents.text(), new ContentType(contentType))
request.headers['content-type'] = [contentType]
request.body = OptionalBody.body(fixture.contents.text(), new ContentType(contentType))
} else {
String contentType = determineContentType(entry, part)
part.headers['content-type'] = [contentType]
String contentType = detectedContentType
request.headers['content-type'] = [contentType]
File contents = new File("pact-compatibility-suite/fixtures/${entry[5..-1].trim()}")
contents.withInputStream {
part.body = OptionalBody.body(it.readAllBytes(), new ContentType(contentType))
request.body = OptionalBody.body(it.readAllBytes(), new ContentType(contentType))
}
}
} else {
part.headers['content-type'] = [determineContentType(entry, part)]
part.body = OptionalBody.body(entry)
request.headers['content-type'] = [detectedContentType]
request.body = OptionalBody.body(entry)
}
request
}

private static String determineContentType(String entry, HttpPart part) {
String contentType = part.contentTypeHeader()
static String determineContentType(String entry, String contentTypeHeader) {
String contentType = contentTypeHeader
if (entry.endsWith('.json')) {
contentType = 'application/json'
} else if (entry.endsWith('.xml')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import static au.com.dius.pact.consumer.MockHttpServerKt.mockServer
import static au.com.dius.pact.core.model.PactReaderKt.queryStringToMap
import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType

class HttpConsumer {
CompatibilitySuiteWorld world
Expand Down Expand Up @@ -56,7 +57,10 @@ class HttpConsumer {
}

if (entry['body']) {
configureBody(entry['body'], interaction.request)
def part = configureBody(entry['body'], determineContentType(entry['body'],
interaction.request.contentTypeHeader()))
interaction.request.body = part.body
interaction.request.headers.putAll(part.headers)
}

mockServerData.pact = new RequestResponsePact(new Provider('p'),
Expand Down
15 changes: 12 additions & 3 deletions compatibility-suite/src/test/groovy/steps/v3/Generators.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.cucumber.java.en.Then
import io.cucumber.java.en.When

import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType

@SuppressWarnings('SpaceAfterOpeningBrace')
class Generators {
Expand All @@ -31,7 +32,9 @@ class Generators {
request = new Request('GET', '/path/one')
def entry = dataTable.entries().first()
if (entry['body']) {
configureBody(entry['body'], request)
def part = configureBody(entry['body'], determineContentType(entry['body'], request.contentTypeHeader()))
request.body = part.body
request.headers.putAll(part.headers)
}
if (entry['generators']) {
JsonValue json
Expand All @@ -52,7 +55,9 @@ class Generators {
response = new Response()
def entry = dataTable.entries().first()
if (entry['body']) {
configureBody(entry['body'], response)
def part = configureBody(entry['body'], determineContentType(entry['body'], response.contentTypeHeader()))
response.body = part.body
response.headers.putAll(part.headers)
}
if (entry['generators']) {
JsonValue json
Expand Down Expand Up @@ -98,11 +103,15 @@ class Generators {
JsonParser.INSTANCE.parseString(generatedRequest.body.valueAsString()) : null
}

@Then('the body value for {string} will have been replaced with a {string}')
@Then('the body value for {string} will have been replaced with a(n) {string}')
void the_body_value_for_will_have_been_replaced_with_a_value(String path, String type) {
def originalElement = JsonUtils.INSTANCE.fetchPath(originalJson, path)
def element = JsonUtils.INSTANCE.fetchPath(generatedJson, path)
assert originalElement != element
matchTypeOfElement(type, element)
}

static void matchTypeOfElement(String type, JsonValue element) {
switch (type) {
case 'integer' -> {
assert element.type() == 'Integer'
Expand Down
14 changes: 11 additions & 3 deletions compatibility-suite/src/test/groovy/steps/v3/HttpMatching.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.cucumber.java.en.When
import static au.com.dius.pact.core.matchers.RequestMatching.requestMismatches
import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue
import static steps.shared.SharedSteps.configureBody
import static steps.shared.SharedSteps.determineContentType

@SuppressWarnings('SpaceAfterOpeningBrace')
class HttpMatching {
Expand All @@ -40,7 +41,9 @@ class HttpMatching {
expectedRequest = new Request()
def entry = dataTable.entries().first()
if (entry['body']) {
configureBody(entry['body'], expectedRequest)
def part = configureBody(entry['body'], determineContentType(entry['body'], expectedRequest.contentTypeHeader()))
expectedRequest.body = part.body
expectedRequest.headers.putAll(part.headers)
}

if (entry['matching rules']) {
Expand All @@ -62,7 +65,10 @@ class HttpMatching {
receivedRequests << new Request()
def entry = dataTable.entries().first()
if (entry['body']) {
configureBody(entry['body'], receivedRequests[0])
def part = configureBody(entry['body'], determineContentType(entry['body'],
receivedRequests[0].contentTypeHeader()))
receivedRequests[0].body = part.body
receivedRequests[0].headers.putAll(part.headers)
}
}

Expand All @@ -71,7 +77,9 @@ class HttpMatching {
for (entry in dataTable.entries()) {
def request = new Request()
if (entry['body']) {
configureBody(entry['body'], request)
def part = configureBody(entry['body'], determineContentType(entry['body'], request.contentTypeHeader()))
request.body = part.body
request.headers.putAll(part.headers)
}
receivedRequests << request
}
Expand Down
Loading

0 comments on commit 5751c1f

Please sign in to comment.