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

[Spring][Multipart Request] The library parse all parameters from multipart/form-data request as files #1290

Closed
isadounikau opened this issue Jan 20, 2021 · 1 comment

Comments

@isadounikau
Copy link

isadounikau commented Jan 20, 2021

The multipart request may contain not only files
The current pact version doesn't differentiate Parameters and Files objects for MockMultipartHttpServletRequestBuilder and adds all request parameters as files

As a result, the pact contract can't be used correctly for API healthiness measurement

multipartRequest.file(MockMultipartFile(name, filename, bodyPart.contentType, bodyPart.inputStream))

while (i < multipart.count) {
      val bodyPart = multipart.getBodyPart(i)
      val contentDisposition = ContentDisposition(bodyPart.getHeader("Content-Disposition").first())
      val name = StringUtils.defaultString(contentDisposition.getParameter("name"), "file")
      val filename = contentDisposition.getParameter("filename").orEmpty()
      multipartRequest.file(MockMultipartFile(name, filename, bodyPart.contentType, bodyPart.inputStream))
      i++
}

Example:
Web HTTP

POST /api/entity HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Length: 838

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="page001"; filename="2020-05-04-everywhere.png"
Content-Type: image/png

(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="entityId"

99199292
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="entityType"

TYPE
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="documentType"

TYPE
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="expirationDate"

2022-07-03T21:44:49.468+05:30
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="startDate"

2019-07-03T21:44:49.468+05:30
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="documentNumber"

12345
----WebKitFormBoundary7MA4YWxkTrZu0gW

Expected mock build:

multipart(BASE_PATH)
      .file(new MockMultipartFile("page001", "file", "image/png", "test data".getBytes()))
      .param("entityId", entityId)
      .param("entityType", entityType.toString())
      .param("documentType", documentType.toString())
      .param("expirationDate", expirationDate)
      .param("startDate", startDate)
      .param("documentNumber", documentNumber)
      .param("status", status)

Actual mock build:

multipart(BASE_PATH)
      .file(new MockMultipartFile("page001", "file", "text/plain", "test data".getBytes()))
      .file(new MockMultipartFile("entityId", "file", "text/plain", "test data".getBytes()))
      .file(new MockMultipartFile("entityType", "file", "text/plain", "test data".getBytes()))
      .file(new MockMultipartFile("documentType", "file", "text/plain", "test data".getBytes()))
      .file(new MockMultipartFile("expirationDate", "file", "text/plain", "test data".getBytes()))
      .file(new MockMultipartFile("startDate", "file", "text/plain", "test data".getBytes()))
      .file(new MockMultipartFile("documentNumber", "file", "text/plain", "test data".getBytes()))
      .file(new MockMultipartFile("status", "file", "text/plain", "test data".getBytes()))
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

2 participants