Skip to content

Commit

Permalink
feat(compatibility-suite): Implemented steps for V2 matching rule sce…
Browse files Browse the repository at this point in the history
…narios
  • Loading branch information
rholshausen committed Jun 26, 2023
1 parent 25d17df commit a6b6e83
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package steps.v1
package steps.shared

import au.com.dius.pact.consumer.BaseMockServer
import au.com.dius.pact.consumer.KTorMockServer
Expand All @@ -8,6 +8,7 @@ import au.com.dius.pact.core.model.Consumer
import au.com.dius.pact.core.model.ContentType
import au.com.dius.pact.core.model.DefaultPactReader
import au.com.dius.pact.core.model.DefaultPactWriter
import au.com.dius.pact.core.model.HeaderParser
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.Pact
Expand Down Expand Up @@ -38,8 +39,8 @@ import io.cucumber.java.en.Then
import io.cucumber.java.en.When
import org.apache.hc.core5.http.ClassicHttpRequest
import org.apache.hc.core5.http.io.entity.StringEntity
import steps.shared.CompatibilitySuiteWorld
import steps.shared.StubVerificationReporter

import static io.ktor.http.HttpHeaderValueParserKt.parseHeaderValue

@SuppressWarnings('ThrowRuntimeException')
class HttpProvider {
Expand Down Expand Up @@ -96,11 +97,16 @@ class HttpProvider {

if (entry['headers']) {
entry['headers'].split(',').collect {
it.trim()[1..-2].split(':', 2)
}.collectEntries {
Map.entry(it[0].trim(), it[1].trim())
}.each {
interaction.response.headers[it.key.toString()] = [ it.value.toString() ]
it.trim()[1..-2].split(':')
}.collect {
[it[0].trim(), parseHeaderValue(it[1].trim()).collect { HeaderParser.INSTANCE.hvToString(it) }]
}.inject(interaction.response.headers) { headers, e ->
if (headers.containsKey(e[0])) {
headers[e[0]] += e[1].flatten()
} else {
headers[e[0]] = e[1].flatten()
}
headers
}
}

Expand Down Expand Up @@ -335,11 +341,11 @@ class HttpProvider {

if (entry['headers']) {
entry['headers'].split(',').collect {
it.trim()[1..-2].split(':', 2)
}.collectEntries {
Map.entry(it[0].trim(), it[1].trim())
it.trim()[1..-2].split(':')
}.collect {
[it[0].trim(), it[1].trim()]
}.each {
request.addHeader(it.key.toString(), it.value)
request.addHeader(it[0].toString(), it[1])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.cucumber.java.After
import io.cucumber.java.Scenario
import io.cucumber.java.en.Then
import io.cucumber.java.en.When
import org.apache.hc.core5.http.HttpRequest

import static au.com.dius.pact.consumer.MockHttpServerKt.mockServer
import static au.com.dius.pact.core.model.PactReaderKt.queryStringToMap
Expand Down Expand Up @@ -65,10 +66,6 @@ class MockServerSharedSteps {

@When('request {int} is made to the mock server with the following changes:')
void request_is_made_to_the_mock_server_with_the_following_changes(Integer num, DataTable dataTable) {
IProviderInfo providerInfo = new ProviderInfo()
providerInfo.port = mockServerData.mockServer.port
def client = new ProviderClient(providerInfo, new HttpClientFactory())

def request = world.interactions[num - 1].request.copy()
def entry = dataTable.entries().first()
if (entry['method']) {
Expand All @@ -86,8 +83,15 @@ class MockServerSharedSteps {
if (entry['headers']) {
request.headers = entry['headers'].split(',').collect {
it.trim()[1..-2].split(':')
}.collectEntries {
Map.entry(it[0].trim(), parseHeaderValue(it[1].trim()).collect { HeaderParser.INSTANCE.hvToString(it) })
}.collect {
[it[0].trim(), parseHeaderValue(it[1].trim()).collect { HeaderParser.INSTANCE.hvToString(it) }]
}.inject([:]) { acc, e ->
if (acc.containsKey(e[0])) {
acc[e[0]] += e[1].flatten()
} else {
acc[e[0]] = e[1].flatten()
}
acc
}
}

Expand All @@ -114,7 +118,23 @@ class MockServerSharedSteps {
}
}

mockServerData.response = client.makeRequest(request)
IProviderInfo providerInfo = new ProviderInfo()
providerInfo.port = mockServerData.mockServer.port
if (entry['raw headers']) {
def headers = entry['raw headers'].split(',').collect {
it.trim()[1..-2].split(':').collect { it.trim() }
}
providerInfo.requestFilter = { HttpRequest req ->
headers.each {
req.addHeader(it[0], it[1])
}
}
def client = new ProviderClient(providerInfo, new HttpClientFactory())
mockServerData.response = client.makeRequest(request)
} else {
def client = new ProviderClient(providerInfo, new HttpClientFactory())
mockServerData.response = client.makeRequest(request)
}
}

@Then('a {int} success response is returned')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ class SharedSteps {
if (entry['headers']) {
interaction.request.headers = entry['headers'].split(',').collect {
it.trim()[1..-2].split(':')
}.collectEntries {
Map.entry(it[0].trim(), parseHeaderValue(it[1].trim()).collect { HeaderParser.INSTANCE.hvToString(it) })
}.collect {
[it[0].trim(), parseHeaderValue(it[1].trim()).collect { HeaderParser.INSTANCE.hvToString(it) }]
}.inject([:]) { acc, e ->
if (acc.containsKey(e[0])) {
acc[e[0]] += e[1].flatten()
} else {
acc[e[0]] = e[1].flatten()
}
acc
}
}

Expand Down Expand Up @@ -98,8 +105,15 @@ class SharedSteps {
if (entry['response headers']) {
interaction.response.headers = entry['response headers'].split(',').collect {
it.trim()[1..-2].split(':')
}.collectEntries {
Map.entry(it[0].trim(), parseHeaderValue(it[1].trim()).collect { HeaderParser.INSTANCE.hvToString(it) })
}.collect {
[it[0].trim(), parseHeaderValue(it[1].trim()).collect { HeaderParser.INSTANCE.hvToString(it) }]
}.inject([:]) { acc, e ->
if (acc.containsKey(e[0])) {
acc[e[0]] += e[1].flatten()
} else {
acc[e[0]] = e[1].flatten()
}
acc
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package steps.v2

import steps.shared.CompatibilitySuiteWorld

@SuppressWarnings('ThrowRuntimeException')
class HttpProvider {
CompatibilitySuiteWorld world

Expand Down

0 comments on commit a6b6e83

Please sign in to comment.