Description
Given the test below, the following mismatches will be generated:
Mismatches
Mismatches: [BodyTypeMismatch { expected: "multipart/form-data;boundary=d79da11d45fff2d33b9abdc83fa82b85676b6be49783211ee847923acd11", actual: "multipart/form-data;boundary=21ab08b0e8448ae4ed94a64f0e02baaea0206dda648442d5b06d49972523", mismatch: "Expected a body of 'multipart/form-data;boundary=d79da11d45fff2d33b9abdc83fa82b85676b6be49783211ee847923acd11' but the actual content type was 'multipart/form-data;boundary=21ab08b0e8448ae4ed94a64f0e02baaea0206dda648442d5b06d49972523'", expected_body: Some(b"--d79da11d45fff2d33b9abdc83fa82b85676b6be49783211ee847923acd11\r\nContent-Disposition: form-data; name=\"baz\"\r\n\r\nbat\r\n--d79da11d45fff2d33b9abdc83fa82b85676b6be49783211ee847923acd11--\r\n"), actual_body: Some(b"--21ab08b0e8448ae4ed94a64f0e02baaea0206dda648442d5b06d49972523\r\nContent-Disposition: form-data; name=\"baz\"\r\n\r\nbat\r\n--21ab08b0e8448ae4ed94a64f0e02baaea0206dda648442d5b06d49972523--\r\n") }]
2025-01-07T04:38:48.163393Z DEBUG tokio-runtime-worker pact_mock_server::hyper_server: Request did not match: Request did not match - HTTP Request ( method: POST, path: /formpost, query: None, headers: Some({"Content-Type": ["multipart/form-data;boundary=d79da11d45fff2d33b9abdc83fa82b85676b6be49783211ee847923acd11"]}), body: Present(181 bytes) ) 0) Expected a body of 'multipart/form-data;boundary=d79da11d45fff2d33b9abdc83fa82b85676b6be49783211ee847923acd11' but the actual content type was 'multipart/form-data;boundary=21ab08b0e8448ae4ed94a64f0e02baaea0206dda648442d5b06d49972523'
Test
func TestMultipart(t *testing.T) {
log.SetLogLevel("TRACE")
mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoV2Consumer",
Provider: "V2Provider",
Host: "127.0.0.1",
})
assert.NoError(t, err)
// Set up our expected interactions.
mockProvider.
AddInteraction().
UponReceiving("A multipart request").
WithRequestPathMatcher("POST", S("/formpost"), func(b *consumer.V2RequestBuilder) {
var buf bytes.Buffer
content := multipart.NewWriter(&buf)
// content.SetBoundary("abcd")
content.WriteField("baz", "bat")
content.Close()
b.Body(fmt.Sprintf("multipart/form-data;boundary=%s", content.Boundary()), buf.Bytes())
b.Header("Content-Type", Regex(fmt.Sprintf("multipart/form-data;boundary=%s", content.Boundary()), "multipart/form-data.*"))
}).
WillRespondWith(200).
ExecuteTest(t, func(config consumer.MockServerConfig) error {
client := &http.Client{}
var buf bytes.Buffer
content := multipart.NewWriter(&buf)
//content.SetBoundary("abcd")
content.WriteField("baz", "bat")
content.Close()
req := &http.Request{
Method: "POST",
URL: &url.URL{
Host: fmt.Sprintf("%s:%d", "localhost", config.Port),
Scheme: "http",
Path: "/formpost",
},
Body: io.NopCloser(&buf),
Header: make(http.Header),
}
req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data;boundary=%s", content.Boundary()))
_, err := client.Do(req)
return err
})
assert.NoError(t, err)
}
Uncommenting the SetBoundary
methods will cause the test setup and HTTP calls to have the same boundary, and the tests pass.
Desired behaviour
The user should be able to specify a regex that bypasses the specific boundary attribute checking (see working JVM example with desired behaviour: https://github.com/pact-foundation/pact-jvm/blob/master/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/MultipartRequestTest.java#L50-L55)
Discussion
The pactffi_with_body
FFI method is the underlying function used here to set the initial body and content type.
The body content matcher should only be checking the main type to see if it got the correct content type (multipart/form-data
) and not the attributes. This seems to be the cause of the issue and perhaps is only present in the FFI.