Hey guys, I'm having trouble writing a Pact consum...
# pact-js
j
Hey guys, I'm having trouble writing a Pact consumer test using form data. The form data object includes a file and some metadata. Example:
Copy code
const formData = new FormData();

formData.append(
 'meta',
 JSON.stringify({
  some_key: "some_value",
 }),
);
formData.append('file', createFile());

provider
 .given('I have file with more than 1000 values')
 .uponReceiving('a request to create a rule using a file with more than 1000 values')
 .withRequest({
   method: 'POST',
   path: '/rules',
   body: formData,
   headers: {
    'Content-Type': 'multipart/form-data',
   },
  })
 .willRespondWith({
   status: 201,
   body: mockedResponse,
   contentType: 'application/json',
  });

return provider.executeTest(async (mockServer) => {
 <http://axiosInstance.post|axiosInstance.post>("/rules", formData, {
  baseURL: mockServer.url,
  headers: {
  'Content-Type': 'multipart/form-data',
  },
 })

 // some assertion
});
I'm getting the following error:
Copy code
Mock server failed with the following mismatches:

        0) The following request was incorrect: 

                POST /rules
            
                         1.0 Mismatch with header 'Content-Type': Expected value 'multipart/form-data' at index 1 but was missing (actual has 1 value(s))
                         1.1 Expected a body of 'application/json' but the actual content type was 'multipart/form-data;boundary=--------------------------087154011299288967847178'
Axios adds this annoying boundary value to the content type but Pact doesn't know about this value. How can I solve this?
Btw I also tried using the native fetch API, didn't work either. For some reason it adds the multipart/form-data content type twice 😅 Here is the error:
Copy code
Mock server failed with the following mismatches:

        0) The following request was expected but not received: 
            Method: POST
            Path: /rules
            Headers:
              Content-Type: multipart/form-data;, multipart/form-data;
            Body: {}... (2 length)
I created a ticket with a reproducible example 🙏 https://github.com/pact-foundation/pact-js/issues/1140
m
Thanks for raising it. I think what is required is to build up the body manually, but axios doesn’t seem to provide a way to introspect the boundary.
I wonder if we could improve the matching so that we can ignore the boundaries as they seem to be dynamic
j
Yeah, indeed, would be helpful, but even with the browser's native fetch, I am having issues.
m
You would need to know in advance of the Pact test what the name of the boundary is, and include that in the
body
expectation.