Skip to content

Commit c9ef690

Browse files
author
Ronald Holshausen
committedMar 21, 2020
test: add tests for generated values with XML within JSON #1031
1 parent a8d6ea2 commit c9ef690

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed
 

‎core/model/src/test/groovy/au/com/dius/pact/core/model/PactReaderSpec.groovy

+21-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import com.amazonaws.services.s3.model.S3Object
77
import com.amazonaws.services.s3.model.S3ObjectInputStream
88
import com.google.gson.JsonParser
99
import org.apache.http.impl.client.BasicCredentialsProvider
10+
import spock.lang.Issue
1011
import spock.lang.Specification
1112
import spock.lang.Unroll
13+
import spock.util.environment.RestoreSystemProperties
14+
15+
import static au.com.dius.pact.core.model.generators.Category.BODY
1216

1317
@SuppressWarnings('DuplicateMapLiteral')
1418
class PactReaderSpec extends Specification {
@@ -313,7 +317,7 @@ class PactReaderSpec extends Specification {
313317
@Unroll
314318
def 'determining pact spec version'() {
315319
expect:
316-
DefaultPactReader.INSTANCE.determineSpecVersion(new JsonParser().parse(json)) == version
320+
DefaultPactReader.INSTANCE.determineSpecVersion(JsonParser.parseString(json)) == version
317321

318322
where:
319323

@@ -329,4 +333,20 @@ class PactReaderSpec extends Specification {
329333

330334
}
331335

336+
@Issue('#1031')
337+
def 'handle encoded values in the pact file'() {
338+
given:
339+
def pactUrl = PactReaderSpec.classLoader.getResource('encoded-values-pact.json')
340+
341+
when:
342+
def pact = DefaultPactReader.INSTANCE.loadPact(pactUrl)
343+
344+
then:
345+
pact instanceof RequestResponsePact
346+
pact.interactions[0].request.body.valueAsString() ==
347+
'{"entityName":"mock-name","xml":"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n"}'
348+
pact.interactions[0].request.generators.categories[BODY]['$'].expression ==
349+
'{\n "entityName": "${eName}",\n "xml": "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n"\n}'
350+
}
351+
332352
}

‎core/model/src/test/groovy/au/com/dius/pact/core/model/generators/ProviderStateGeneratorSpec.groovy

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package au.com.dius.pact.core.model.generators
22

3+
import spock.lang.Issue
34
import spock.lang.Specification
45
import spock.lang.Unroll
56

@@ -40,4 +41,17 @@ class ProviderStateGeneratorSpec extends Specification {
4041
[a: 'A', b: 100] | '/${a}/${c}' | '/A/null'
4142
}
4243

44+
@Issue('#1031')
45+
def 'handles encoded values in the expressions'() {
46+
given:
47+
def expression = '{\n "entityName": "${eName}",\n "xml": "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n"\n}'
48+
def context = [eName: 'Entity-Name']
49+
50+
when:
51+
def result = new ProviderStateGenerator(expression).generate([providerState: context])
52+
53+
then:
54+
result == '{\n "entityName": "Entity-Name",\n "xml": "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n"\n}'
55+
}
56+
4357
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"provider": {
3+
"name": "Test_Provider"
4+
},
5+
"consumer": {
6+
"name": "Test_Consumer"
7+
},
8+
"interactions": [
9+
{
10+
"description": "A request to create new entity",
11+
"request": {
12+
"method": "POST",
13+
"path": "/test",
14+
"headers": {
15+
"Content-Type": "application/json"
16+
},
17+
"body": {
18+
"entityName": "mock-name",
19+
"xml": "\u003c?xml version\u003d\"1.0\" encoding\u003d\"UTF-8\"?\u003e\n"
20+
},
21+
"generators": {
22+
"body": {
23+
"$": {
24+
"type": "ProviderState",
25+
"expression": "{\n \"entityName\": \"${eName}\",\n \"xml\": \"\u003c?xml version\u003d\\\"1.0\\\" encoding\u003d\\\"UTF-8\\\"?\u003e\\n\"\n}"
26+
}
27+
}
28+
}
29+
},
30+
"response": {
31+
"status": 200,
32+
"headers": {
33+
"Content-Type": "application/json;charset\u003dUTF-8"
34+
},
35+
"body": {
36+
"workflowDefinitionName": "mock-name"
37+
},
38+
"matchingRules": {
39+
"body": {
40+
"$.workflowDefinitionName": {
41+
"matchers": [
42+
{
43+
"match": "type"
44+
}
45+
],
46+
"combine": "AND"
47+
}
48+
}
49+
}
50+
},
51+
"providerStates": [
52+
{
53+
"name": "A entity name not exists"
54+
}
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"pactSpecification": {
60+
"version": "3.0.0"
61+
},
62+
"pact-jvm": {
63+
"version": "3.6.11"
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)
Please sign in to comment.