Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added withFileUpload method to the JUnit DSL to support multipart fil…
…e uploads #462
- Loading branch information
Ronald Holshausen
committed
Dec 6, 2017
1 parent
2aee521
commit 3f9a03e
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...nsumer-junit/src/test/groovy/au/com/dius/pact/consumer/junit/ExampleFileUploadSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package au.com.dius.pact.consumer.junit | ||
|
||
import au.com.dius.pact.consumer.Pact | ||
import au.com.dius.pact.consumer.PactProviderRuleMk2 | ||
import au.com.dius.pact.consumer.PactVerification | ||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider | ||
import au.com.dius.pact.model.RequestResponsePact | ||
import org.apache.http.client.methods.RequestBuilder | ||
import org.apache.http.entity.ContentType | ||
import org.apache.http.entity.mime.HttpMultipartMode | ||
import org.apache.http.entity.mime.MultipartEntityBuilder | ||
import org.apache.http.impl.client.CloseableHttpClient | ||
import org.apache.http.impl.client.HttpClients | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class ExampleFileUploadSpec { | ||
|
||
@Rule | ||
@SuppressWarnings('PublicInstanceField') | ||
public final PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2('File Service', this) | ||
|
||
@Pact(provider = 'File Service', consumer= 'Junit Consumer') | ||
RequestResponsePact createPact(PactDslWithProvider builder) { | ||
builder | ||
.uponReceiving('a multipart file POST') | ||
.path('/upload') | ||
.method('POST') | ||
.withFileUpload('file', 'data.csv', 'text/csv', '1,2,3,4\n5,6,7,8'.bytes) | ||
.willRespondWith() | ||
.status(201) | ||
.body('file uploaded ok') | ||
.toPact() | ||
} | ||
|
||
@Test | ||
@PactVerification | ||
void runTest() { | ||
CloseableHttpClient httpclient = HttpClients.createDefault() | ||
httpclient.withCloseable { | ||
def data = MultipartEntityBuilder.create() | ||
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE) | ||
.addBinaryBody('file', '1,2,3,4\n5,6,7,8'.bytes, ContentType.create('text/csv'), 'data.csv') | ||
.build() | ||
def request = RequestBuilder | ||
.post(mockProvider.url + '/upload') | ||
.setEntity(data) | ||
.build() | ||
println('Executing request ' + request.requestLine) | ||
httpclient.execute(request) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters