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

BodyTypeMismatch is thrown for multipart requests with different boundary attributes #482

Open
mefellows opened this issue Jan 7, 2025 · 1 comment
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@mefellows
Copy link
Member

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.

@mefellows mefellows added the bug Indicates an unexpected problem or unintended behavior label Jan 7, 2025
@mefellows
Copy link
Member Author

mefellows commented Jan 7, 2025

Feels like it's in here somewhere: https://github.com/pact-foundation/pact-reference/blob/master/rust/pact_matching/src/lib.rs#L1629-L1661

It uses is_equivalent_to which has the following definition:

 /// Equals, ignoring attributes if not present on self
  pub fn is_equivalent_to(&self, other: &ContentType) -> bool {
    if self.is_strict_xml() && other.is_strict_xml() {
      self.attributes.is_empty() || self.attributes == other.attributes
    }
    else if self.attributes.is_empty() {
      self.main_type == other.main_type && self.sub_type == other.sub_type
    } else {
      self == other
    }
  }

There will always be attributes on self (because it needs the boundary set), so I think it will always fall into self == other which won't pass.

Update

With this minor adjustment to the above function, the test passes:

diff --git a/rust/pact_models/src/content_types.rs b/rust/pact_models/src/content_types.rs
index 38442d36..16659b78 100644
--- a/rust/pact_models/src/content_types.rs
+++ b/rust/pact_models/src/content_types.rs
@@ -147,7 +147,7 @@ impl ContentType {
     if self.is_strict_xml() && other.is_strict_xml() {
       self.attributes.is_empty() || self.attributes == other.attributes
     }
-    else if self.attributes.is_empty() {
+    else if !self.attributes.is_empty() {
       self.main_type == other.main_type && self.sub_type == other.sub_type
     } else {
       self == other

I don't know if this is the correct change (or what side effects may occur if it were made), but it does show that in this case, the comparison of the MIME types is falling to exact match, rather than just the main types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

1 participant