Skip to content

Commit

Permalink
Merge pull request #1340 from keeping-it-up/1330-issue
Browse files Browse the repository at this point in the history
issue # 1330 fixed and a test case added with expression in body with application/xthrift content type
  • Loading branch information
uglyog authored Apr 21, 2021
2 parents ac6a0ea + 8e6d161 commit 049e7c3
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,25 @@ data class Generators(val categories: MutableMap<Category, MutableMap<String, Ge
}

private fun findContentTypeHandler(contentType: ContentType): ContentTypeHandler? {
val typeHandler = contentTypeHandlers[contentType.getBaseType()]
val updatedContentType = getUpdatedContentType(contentType)
val typeHandler = contentTypeHandlers[updatedContentType.getBaseType()]
return if (typeHandler != null) {
typeHandler
} else {
val supertype = contentType.getSupertype()
val supertype = updatedContentType.getSupertype()
if (supertype != null) {
findContentTypeHandler(supertype)
} else {
null
}
}
}

private fun getUpdatedContentType(contentType: ContentType): ContentType {
if (contentType.isJson())
return ContentType.JSON
return contentType
}
}

@JvmOverloads
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package au.com.dius.pact.provider.junit5

import au.com.dius.pact.provider.junitsupport.Provider
import au.com.dius.pact.provider.junitsupport.State
import au.com.dius.pact.provider.junitsupport.loader.PactFolder
import com.github.tomakehurst.wiremock.WireMockServer
import groovy.util.logging.Slf4j
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestTemplate
import org.junit.jupiter.api.extension.ExtendWith
import ru.lanwen.wiremock.ext.WiremockResolver
import ru.lanwen.wiremock.ext.WiremockUriResolver

import static com.github.tomakehurst.wiremock.client.WireMock.*

@Provider('ThriftJsonPostService')
@PactFolder('pacts')
@ExtendWith([
WiremockResolver,
WiremockUriResolver
])
@Slf4j
class ThriftStateInjectedProviderPostTest {

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider)
void testTemplate(PactVerificationContext context) {
context.verifyInteraction()
}

@BeforeEach
void before(PactVerificationContext context, @WiremockResolver.Wiremock WireMockServer server,
@WiremockUriResolver.WiremockUri String uri) throws MalformedURLException {
context.setTarget(HttpTestTarget.fromUrl(new URL(uri)))
System.setProperty('pact.content_type.override.application/x-thrift', 'json')

server.stubFor(
post(urlPathEqualTo('/data/1234'))
.withRequestBody(containing("{\"id\":\"abc\"}"))
.willReturn(aResponse()
.withStatus(200)
.withHeader('Content-Type', 'application/x-thrift')
.withBody('{"accountId": "4545"}'))
)
}

@State('default state')
Map<String, Object> defaultState() {
[
id: 'abc'
]
}
}
73 changes: 73 additions & 0 deletions provider/junit5/src/test/resources/pacts/thrift-pact-post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"provider": {
"name": "ThriftJsonPostService"
},
"consumer": {
"name": "ThriftJsonConsumer"
},
"interactions": [
{
"providerStates": [
{
"name": "default state"
}
],
"description": "Thrift request",
"request": {
"method": "POST",
"path": "/data/1234",
"body": {
"id": "xyz"
},
"generators": {
"body": {
"$.id": {
"expression": "${id}",
"type": "ProviderState"
}
}
},
"headers": {
"Content-Type": ["application/x-thrift"]
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": ["application/x-thrift"]
},
"body": {
"accountId": "4beb44f1-53f7-4281-a78b-12c06d682067"
},
"matchingRules": {
"body": {
"$.accountId": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
}
}
},
"generators": {
"body": {
"$.accountId": {
"type": "ProviderState",
"expression": "${accountId}"
}
}
}
}
}
],
"metadata": {
"pact-specification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "4.2.2"
}
}
}

0 comments on commit 049e7c3

Please sign in to comment.