Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we create a pact for Multipart file upload using POST #123

Closed
tarakesh-nv opened this issue May 25, 2015 · 10 comments
Closed

How do we create a pact for Multipart file upload using POST #123

tarakesh-nv opened this issue May 25, 2015 · 10 comments

Comments

@tarakesh-nv
Copy link

We have a scenario where we need to upload a file using post. Is there an option in pact jvm to pass file input in .body()? Is there an example? How do we go about it?

@uglyog
Copy link
Member

uglyog commented May 26, 2015

Pact currently only supports text formats. If you need to post a binary body, we could implement it but it would have to be base64 encoded in the pact file.

The other option is with mime multipart bodies (like from a FORM POST with a file selection).
Currently there is no direct support for creating a multipart body. I can update the DSL and provider verifiers to handle them, but in the meantime you can create the bodies yourself and set the body as a string.

You will have to set the content type to include the boundary marker in the body, i.e.

Content-Type: multipart/form-data; boundary=5aa569203ce8f671

The MultipartEntityBuilder from Apache HTTP components can generate the content type with the boundary marker and bodies.

@tarakesh-nv
Copy link
Author

Thanks! I'll explore and get back to you.

@radubutnaru
Copy link

We are in a similar situation where we would like to use Pact for a service provider accepting mime multipart bodies. Support for multipart matching would definitely be useful.

@radubutnaru
Copy link

We went for the full string body matching as a workaround, however we are a bit limited by the fact that there are no regex request matching rules for string bodies.

Given that the MIME multipart boundary string is usually randomly generated, it is a bit hard to match exactly...

Any other ideas? Thank you.

@uglyog
Copy link
Member

uglyog commented Jul 30, 2015

I have been thinking about this, and it does not make sense to have matchers in this case. What you probably want to do is confirm that a file was included with the body, i.e., the contents are not empty.

Are you using the JUnit DSL?

@radubutnaru
Copy link

Yes, we're using the JUnit DSL. The provider service accepts POST multipart/form-data, with multiple parts being sent, one of which is the uploaded file. Ideally, we should be able to match/verify each part independently. For the uploaded file part, simply confirming that a file was included should be OK.

@coding-yogi
Copy link
Contributor

any pointers to example of file upload?

@coding-yogi
Copy link
Contributor

Can we have this enhancement implemented please?

@uglyog
Copy link
Member

uglyog commented Nov 23, 2017

I've managed to do this with a pact between JS and JVM. The interaction on the consumer side is setup as

{
  "method": "POST",
  "path": "/promotion-groups/a_group_id/product-promotions",
  "headers": {
    "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary1bvPHrYmtx0UNv2e"
  },
  "body": "------WebKitFormBoundary1bvPHrYmtx0UNv2e\r\nContent-Disposition: form-data; name=\"file\"; filename=\"476.csv\"\r\nContent-Type: text/csv\r\n\r\ndata\r\n------WebKitFormBoundary1bvPHrYmtx0UNv2e--\r\n"
}

where data is the actual file data.

@uglyog
Copy link
Member

uglyog commented Dec 6, 2017

Support for matching multi-part file uploads has now been added.

@uglyog uglyog closed this as completed Dec 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants