Skip to content

Commit

Permalink
feat: Changes OptionalBody to store byte arrays in prep for supportin…
Browse files Browse the repository at this point in the history
…g binary playloads #600
  • Loading branch information
Ronald Holshausen committed Feb 17, 2019
1 parent 2af233c commit b845bf0
Show file tree
Hide file tree
Showing 83 changed files with 495 additions and 436 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ class PactBuilder extends BaseBuilder {
requestDescription,
providerStates,
new Request(requestData[i].method ?: 'get', path, query, headers,
requestData[i].containsKey(BODY) ? OptionalBody.body(requestData[i].body) : OptionalBody.missing(),
requestData[i].containsKey(BODY) ? OptionalBody.body(requestData[i].body.bytes) : OptionalBody.missing(),
requestMatchers, requestGenerators),
new Response(responseData[i].status ?: 200, responseHeaders,
responseData[i].containsKey(BODY) ? OptionalBody.body(responseData[i].body) : OptionalBody.missing(),
responseData[i].containsKey(BODY) ? OptionalBody.body(responseData[i].body.bytes) : OptionalBody.missing(),
responseMatchers, responseGenerators)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class PactMessageBuilder extends BaseBuilder {
def body = new PactBodyBuilder(mimetype: options.contentType, prettyPrintBody: options.prettyPrint)
closure.delegate = body
closure.call()
messages.last().contents = OptionalBody.body(body.body)
messages.last().contents = OptionalBody.body(body.body.bytes)
messages.last().matchingRules.addCategory(body.matchers)

this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PactBodyBuilderSpec extends Specification {

when:
service.buildInteractions()
def keys = new JsonSlurper().parseText(service.interactions[0].request.body.value).keySet()
def keys = new JsonSlurper().parseText(service.interactions[0].request.body.valueAsString()).keySet()
def requestMatchingRules = service.interactions[0].request.matchingRules
def bodyMatchingRules = requestMatchingRules.rulesForCategory('body').matchingRules
def responseMatchingRules = service.interactions[0].response.matchingRules
Expand Down Expand Up @@ -123,7 +123,7 @@ class PactBodyBuilderSpec extends Specification {
keys == ['name', 'surname', 'position', 'happy', 'hexCode', 'hexCode2', 'id', 'id2', 'localAddress',
'localAddress2', 'age', 'age2', 'salary', 'timestamp', 'ts', 'values', 'role', 'roles'] as Set

service.interactions[0].response.body.value == new JsonBuilder([name: 'harry']).toPrettyString()
service.interactions[0].response.body.valueAsString() == new JsonBuilder([name: 'harry']).toPrettyString()

requestGenerators.keySet() == ['$.hexCode', '$.id', '$.age2', '$.salary', '$.ts', '$.timestamp', '$.values[3]',
'$.role.id', '$.role.dob', '$.roles[0].id'] as Set
Expand Down Expand Up @@ -160,7 +160,7 @@ class PactBodyBuilderSpec extends Specification {

when:
service.buildInteractions()
def keys = walkGraph(new JsonSlurper().parseText(service.interactions[0].request.body.value))
def keys = walkGraph(new JsonSlurper().parseText(service.interactions[0].request.body.valueAsString()))
def rules = service.interactions[0].request.matchingRules.rulesForCategory('body').matchingRules

then:
Expand Down Expand Up @@ -208,7 +208,7 @@ class PactBodyBuilderSpec extends Specification {

when:
service.buildInteractions()
def body = new JsonSlurper().parseText(service.interactions[0].request.body.value)
def body = new JsonSlurper().parseText(service.interactions[0].request.body.valueAsString())

then:
service.interactions.size() == 1
Expand Down Expand Up @@ -247,7 +247,7 @@ class PactBodyBuilderSpec extends Specification {

when:
service.buildInteractions()
def body = new JsonSlurper().parseText(service.interactions[0].request.body.value)
def body = new JsonSlurper().parseText(service.interactions[0].request.body.valueAsString())

then:
service.interactions.size() == 1
Expand Down Expand Up @@ -279,7 +279,7 @@ class PactBodyBuilderSpec extends Specification {

when:
service.buildInteractions()
def body = new JsonSlurper().parseText(service.interactions[0].request.body.value)
def body = new JsonSlurper().parseText(service.interactions[0].request.body.valueAsString())

then:
service.interactions.size() == 1
Expand Down Expand Up @@ -319,13 +319,13 @@ class PactBodyBuilderSpec extends Specification {
def response = service.interactions.first().response

then:
request.body.value == '''|{
request.body.valueAsString() == '''|{
| "name": "harry",
| "surname": "larry",
| "position": "staff",
| "happy": true
|}'''.stripMargin()
response.body.value == '''|{
response.body.valueAsString() == '''|{
| "name": "harry"
|}'''.stripMargin()
}
Expand Down Expand Up @@ -353,13 +353,13 @@ class PactBodyBuilderSpec extends Specification {
def response = service.interactions.first().response

then:
request.body.value == '''|{
request.body.valueAsString() == '''|{
| "name": "harry",
| "surname": "larry",
| "position": "staff",
| "happy": true
|}'''.stripMargin()
response.body.value == '''|{
response.body.valueAsString() == '''|{
| "name": "harry"
|}'''.stripMargin()
}
Expand Down Expand Up @@ -387,8 +387,8 @@ class PactBodyBuilderSpec extends Specification {
def response = service.interactions.first().response

then:
request.body.value == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.value == '{"name":"harry"}'
request.body.valueAsString() == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.valueAsString() == '{"name":"harry"}'
}

def 'does not pretty print bodies if mimetype corresponds to one that requires compact bodies'() {
Expand All @@ -414,8 +414,8 @@ class PactBodyBuilderSpec extends Specification {
def response = service.interactions.first().response

then:
request.body.value == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.value == '{"name":"harry"}'
request.body.valueAsString() == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.valueAsString() == '{"name":"harry"}'
}

def 'No Special Handling For Field Names Formerly Not Conforming Gatling Fields'() {
Expand All @@ -441,7 +441,7 @@ class PactBodyBuilderSpec extends Specification {

when:
service.buildInteractions()
def keys = walkGraph(new JsonSlurper().parseText(service.interactions[0].request.body.value))
def keys = walkGraph(new JsonSlurper().parseText(service.interactions[0].request.body.valueAsString()))

then:
service.interactions.size() == 1
Expand Down Expand Up @@ -489,10 +489,10 @@ class PactBodyBuilderSpec extends Specification {
def response = service.interactions.first().response

then:
request.body.value == '[["e8cda07e","sony"]]'
request.body.valueAsString() == '[["e8cda07e","sony"]]'
request.matchingRules.rulesForCategory('body').matchingRules == expectedMatchingRules

response.body.value == '["test"]'
response.body.valueAsString() == '["test"]'
response.matchingRules.rulesForCategory('body').matchingRules == [
'$': new MatchingRuleGroup([TypeMatcher.INSTANCE]),
'$[*]': new MatchingRuleGroup([new RegexMatcher('\\w+', 'test')])
Expand Down Expand Up @@ -555,7 +555,8 @@ class PactBodyBuilderSpec extends Specification {
def request = service.interactions.first().request

then:
request.body.value == '{"answers":[{"questionId":"books","answer":[[{"questionId":"title","answer":"BBBB"},' +
request.body.valueAsString() ==
'{"answers":[{"questionId":"books","answer":[[{"questionId":"title","answer":"BBBB"},' +
'{"questionId":"author","answer":"B.B."}]],"answer2":[[{"questionId":"title","answer":"BBBB"},' +
'{"questionId":"title","answer":"BBBB"}],[{"questionId":"title","answer":"BBBB"},' +
'{"questionId":"title","answer":"BBBB"}]],"answer3":[[{"questionId":"title","answer":"BBBB"}]]}]}'
Expand Down Expand Up @@ -584,7 +585,7 @@ class PactBodyBuilderSpec extends Specification {
def request = service.interactions.first().request

then:
request.body.value == '''|{
request.body.valueAsString() == '''|{
| "items": [
| {
| "id": "100abc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class PactBuilderSpec extends Specification {
then:
aliceService.interactions.size() == 1
firstInteraction.response.body.value == '[\n' +
firstInteraction.response.body.valueAsString() == '[\n' +
' 1,\n' +
' 2,\n' +
' 3\n' +
Expand Down Expand Up @@ -154,7 +154,7 @@ class PactBuilderSpec extends Specification {
then:
aliceService.interactions.size() == 1
firstInteraction.response.body.value == '[\n' +
firstInteraction.response.body.valueAsString() == '[\n' +
' {\n' +
' "id": 1,\n' +
' "name": "item1"\n' +
Expand Down Expand Up @@ -187,7 +187,7 @@ class PactBuilderSpec extends Specification {
then:
aliceService.interactions.size() == 1
firstInteraction.response.body.value == '[\n' +
firstInteraction.response.body.valueAsString() == '[\n' +
' {\n' +
' "id": 1,\n' +
' "name": "item1"\n' +
Expand Down Expand Up @@ -216,13 +216,13 @@ class PactBuilderSpec extends Specification {
def response = aliceService.interactions.first().response
then:
request.body.value == '''|{
request.body.valueAsString() == '''|{
| "name": "harry",
| "surname": "larry",
| "position": "staff",
| "happy": true
|}'''.stripMargin()
response.body.value == '''|{
response.body.valueAsString() == '''|{
| "name": "harry"
|}'''.stripMargin()
}
Expand All @@ -246,13 +246,13 @@ class PactBuilderSpec extends Specification {
def response = aliceService.interactions.first().response
then:
request.body.value == '''|{
request.body.valueAsString() == '''|{
| "name": "harry",
| "surname": "larry",
| "position": "staff",
| "happy": true
|}'''.stripMargin()
response.body.value == '''|{
response.body.valueAsString() == '''|{
| "name": "harry"
|}'''.stripMargin()
}
Expand All @@ -276,8 +276,8 @@ class PactBuilderSpec extends Specification {
def response = aliceService.interactions.first().response
then:
request.body.value == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.value == '{"name":"harry"}'
request.body.valueAsString() == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.valueAsString() == '{"name":"harry"}'
}
def 'does not pretty print bodies if the mimetype corresponds to one that requires compact bodies'() {
Expand All @@ -299,8 +299,8 @@ class PactBuilderSpec extends Specification {
def response = aliceService.interactions.first().response
then:
request.body.value == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.value == '{"name":"harry"}'
request.body.valueAsString() == '{"name":"harry","surname":"larry","position":"staff","happy":true}'
response.body.valueAsString() == '{"name":"harry"}'
}
def 'does not overwrite the content type if it has been set in a header'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class PactResultSpec extends Specification {
'\theaders: [Content-Type:application/json]\n' +
'\tmatchers: MatchingRules(rules={body=Category(name=body, matchingRules={}), path=Category(name=path, matchingRules={})})\n' +
'\tgenerators: Generators(categories={})\n' +
'\tbody: OptionalBody(state=PRESENT, value={\n' +
'\tbody: PRESENT({\n' +
' "status": "isGood"\n' +
'})')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ExampleFileUploadSpec {
.withFileUpload('file', 'data.csv', 'text/csv', '1,2,3,4\n5,6,7,8'.bytes)
.willRespondWith()
.status(201)
.body('file uploaded ok')
.body('file uploaded ok', 'text/plain')
.toPact()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void assertResponseKeysEqualTo(PactFragment fragment, String... ke
}

public static void assertResponseKeysEqualTo(RequestResponsePact pact, String... keys) {
String body = pact.getInteractions().get(0).getResponse().getBody().getValue();
String body = pact.getInteractions().get(0).getResponse().getBody().valueAsString();
Map hashMap = null;
try {
hashMap = new ObjectMapper().readValue(body, HashMap.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void assertException(Throwable e) {
"\theaders: [testreqheader:testreqheadervalue]\n" +
"\tmatchers: MatchingRules(rules={path=Category(name=path, matchingRules={}), header=Category(name=header, matchingRules={})})\n" +
"\tgenerators: Generators(categories={})\n" +
"\tbody: OptionalBody(state=EMPTY, value=)"));
"\tbody: EMPTY"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ trait UnitSpecsSupport extends Specification {
headers: Map[String, String] = Map(),
body: String = "",
matchers: MatchingRules = new MatchingRulesImpl()): Request =
new Request(method, path, PactReaderKt.queryStringToMap(query), headers.asJava, OptionalBody.body(body), matchers)
new Request(method, path, PactReaderKt.queryStringToMap(query), headers.asJava, OptionalBody.body(body.getBytes), matchers)

def buildResponse(status: Int = 200,
headers: Map[String, String] = Map(),
maybeBody: Option[String] = None,
matchers: MatchingRules = new MatchingRulesImpl()): Response = {
val optionalBody = maybeBody match {
case Some(body) => OptionalBody.body(body)
case Some(body) => OptionalBody.body(body.getBytes)
case None => OptionalBody.missing()
}

Expand All @@ -53,7 +53,7 @@ trait UnitSpecsSupport extends Specification {
bodyAndMatchers: DslPart): Response = {
val matchers = new MatchingRulesImpl()
matchers.addCategory(bodyAndMatchers.getMatchers)
new Response(status, headers.asJava, OptionalBody.body(bodyAndMatchers.toString), matchers)
new Response(status, headers.asJava, OptionalBody.body(bodyAndMatchers.toString.getBytes), matchers)
}

def buildInteraction(description: String, states: List[ProviderState], request: Request, response: Response): RequestResponseInteraction =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object HttpClient {
org.apache.http.entity.ContentType.parse(res.getContentType)
val charset = if (contentType.getCharset == null) Charset.forName("UTF-8") else contentType.getCharset
val body = if (res.hasResponseBody) {
OptionalBody.body(res.getResponseBody(charset))
OptionalBody.body(res.getResponseBody(charset).getBytes)
} else {
OptionalBody.empty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ case class ConsumerService(serverUrl: String) {
implicit val executionContext = ExecutionContext.fromExecutor(Executors.newCachedThreadPool)

private def extractFrom(body: OptionalBody): Boolean = {
body.orElse("") == "{\"responsetest\": true}"
body.valueAsString == "{\"responsetest\": true}"
}

def extractResponseTest(path: String = request.getPath): Future[Boolean] = {
Expand All @@ -25,19 +25,19 @@ case class ConsumerService(serverUrl: String) {

def simpleGet(path: String): Future[(Int, String)] = {
toScala[(Int, String)](HttpClient.run(new Request("GET", serverUrl + path)).thenApply { response =>
(response.getStatus, response.getBody.getValue)
(response.getStatus, response.getBody.valueAsString)
})
}

def simpleGet(path: String, query: String): Future[(Int, String)] = {
toScala[(Int, String)](HttpClient.run(new Request("GET", serverUrl + path, PactReaderKt.queryStringToMap(query, true))).thenApply { response =>
(response.getStatus, response.getBody.getValue)
(response.getStatus, response.getBody.valueAsString)
})
}

def options(path: String): Future[(Int, String, Map[String, String])] = {
toScala[(Int, String, Map[String, String])](HttpClient.run(new Request("OPTION", serverUrl + path)).thenApply { response =>
(response.getStatus, response.getBody.orElse(""), JavaConversions.mapAsScalaMap(response.getHeaders).toMap)
(response.getStatus, response.getBody.valueAsString, JavaConversions.mapAsScalaMap(response.getHeaders).toMap)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public MessagePactBuilder withContent(DslPart body) {
}

DslPart parent = body.close();
message.setContents(OptionalBody.body(parent.toString()));
message.setContents(OptionalBody.body(parent.toString().getBytes()));
message.getMatchingRules().addCategory(parent.getMatchers());

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void setupFileUpload(String partName, String fileName, String fileCont
OutputStream os = new ByteArrayOutputStream();
multipart.writeTo(os);

requestBody = OptionalBody.body(os.toString());
requestBody = OptionalBody.body(os.toString().getBytes());
requestMatchers.addCategory("header").addRule(CONTENT_TYPE, new RegexMatcher(MULTIPART_HEADER_REGEX,
multipart.getContentType().getValue()));
requestHeaders.put(CONTENT_TYPE, multipart.getContentType().getValue());
Expand Down
Loading

0 comments on commit b845bf0

Please sign in to comment.