wiremock: Sending multipart requests to Wiremock fails with "Stream ended unexpectedly"

Issues occurs in Wiremock 3.32.0 and later (did not throw exception in 3.31).

Starting Wiremock in docker with definition:

image: wiremock/wiremock:2.32.0-alpine
command: --local-response-templating

Our application sends multipart requests to an external API (which we have used Wiremock to mock so we can test our API independently). However since updating to Wiremock 3.32, it fails when we send a multipart request. The body of the response:

<h2 style="color: rgb(0, 0, 0); font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">HTTP ERROR 500 wiremock.javax.servlet.ServletException: wiremock.org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly</h2>

URI: | /test-url
-- | --
500
wiremock.javax.servlet.ServletException: wiremock.org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet-3bcbb589
wiremock.javax.servlet.ServletException: wiremock.org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
wiremock.org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
wiremock.org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly

<h3 style="color: rgb(0, 0, 0); font-family: &quot;Times New Roman&quot;; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Caused by:</h3><pre style="color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">wiremock.javax.servlet.ServletException: wiremock.org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
	at wiremock.org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:162)
	at wiremock.org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
	at wiremock.org.eclipse.jetty.server.Server.handle(Server.java:516)
	at wiremock.org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:400)
	at wiremock.org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:645)
	at wiremock.org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:392)
	at wiremock.org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277)
	at wiremock.org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
	at wiremock.org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
	at wiremock.org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)
	at wiremock.org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338)
	at wiremock.org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315)
	at wiremock.org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173)
	at wiremock.org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
	at wiremock.org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:409)
	at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883)
	at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: wiremock.org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
	at wiremock.org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:361)
	at com.github.tomakehurst.wiremock.http.multipart.PartParser.parseFrom(PartParser.java:51)
	at com.github.tomakehurst.wiremock.servlet.WireMockHttpServletRequestAdapter.getParts(WireMockHttpServletRequestAdapter.java:279)
	at com.github.tomakehurst.wiremock.verification.LoggedRequest.createFrom(LoggedRequest.java:71)
	at com.github.tomakehurst.wiremock.stubbing.InMemoryStubMappings.serveFor(InMemoryStubMappings.java:88)
	at com.github.tomakehurst.wiremock.core.WireMockApp.serveStubFor(WireMockApp.java:225)
	at com.github.tomakehurst.wiremock.http.StubRequestHandler.handleRequest(StubRequestHandler.java:57)
	at com.github.tomakehurst.wiremock.http.AbstractRequestHandler.handle(AbstractRequestHandler.java:69)
	at com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet.service(WireMockHandlerDispatchingServlet.java:142)
	at wiremock.javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at wiremock.org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799)
	at wiremock.org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1631)
	at wiremock.org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:548)
	at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
	at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1434)
	at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
	at wiremock.org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501)
	at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)
	at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1349)
	at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at wiremock.org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:763)
	at wiremock.org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
	... 17 more
Caused by: wiremock.org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
	at wiremock.org.apache.commons.fileupload.MultipartStream.readHeaders(MultipartStream.java:570)
	at wiremock.org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:1052)
	at wiremock.org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.&lt;init&gt;(FileUploadBase.java:1017)
	at wiremock.org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:309)
	at wiremock.org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:333)
	... 38 more</pre>

To reproduce, start Wiremock and send this multipart request to any endpoint (replacing the URL for where you have Wiremock running):

curl --location --request POST 'http://stubs:8080/test-url' \
--header 'Content-Type: multipart/related; boundary=xxx;' \
--data-raw '--xxx
Content 1
--xxx
Content 2
--xxx--'

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 20 (12 by maintainers)

Most upvoted comments

I am seeing the same issue mocking an OData request; again - if I change ALL line breaks to \r\n it works. I am running on a mac locally. I don’t want to have to edit our code to please Wiremock - I am trying to figure out if the Wiremock definition is closer to the batch RFC, but at the moment it does look like a bug

While working on the reproducer I realized that our code sends “\n” as line breaks in the multipart request. When I change to “\r\n” the test works again.