pact-foundation / pact-foundation/pact-reference

`BodyTypeMismatch` is thrown for multipart requests with different boundary attributes

Open
#482 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Rust
Stars
106
Forks
53
Avg merge
1h 37m
Merged PRs (30d)
2

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Go TestMultipart example and trace how pactffi_with_body sets the initial body and content type. Inspect the body content matcher to determine where multipart attributes are compared. Done means a regex can ignore the boundary while still matching multipart/form-data, with the provided test passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, rust
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.