Skip to content

Commit

Permalink
feat: Add a method for binary data in the Java DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 18, 2020
1 parent 6b0e070 commit 1b34871
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class BinaryFileSpec {

@Pact(provider = 'File Service', consumer= 'PDF Consumer')
RequestResponsePact createPact(PactDslWithProvider builder) {
def pdf = BinaryFileSpec.getResourceAsStream('/sample.pdf').bytes
builder
.uponReceiving('a request for a PDF')
.path('/get-file')
.method('GET')
.willRespondWith()
.status(200)
.body('0111010001110111', 'application/pdf')
.withBinaryData(pdf, 'application/pdf')
.toPact()
}

Expand All @@ -37,7 +38,8 @@ class BinaryFileSpec {
.build()
def response = httpclient.execute(request)
assert response.statusLine.statusCode == 200
assert new String(response.entity.content.bytes) == '0111010001110111'
assert response.entity.contentType.value == 'application/pdf'
assert response.entity.content.bytes[0..7] == [37, 80, 68, 70, 45, 49, 46, 53] as byte[]
}
}
}
Binary file added consumer/junit/src/test/resources/sample.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Supplier;
import java.util.regex.Pattern;

import static au.com.dius.pact.consumer.Headers.MULTIPART_HEADER_REGEX;
import static com.google.common.collect.Lists.newArrayList;

public class PactDslResponse {
Expand Down Expand Up @@ -274,6 +275,17 @@ public PactDslResponse body(Document body) throws TransformerException {
return this;
}

/**
* Response body as a binary data.
* @param example Example contents to use in the consumer test
* @param contentType Content type of the data
*/
public PactDslResponse withBinaryData(byte[] example, String contentType) {
responseBody = OptionalBody.body(example, au.com.dius.pact.core.model.ContentType.fromString(contentType));
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(contentType));
return this;
}

/**
* Match a response header. A random example header value will be generated from the provided regular expression.
*
Expand Down

0 comments on commit 1b34871

Please sign in to comment.