-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix(fromTraffic): clone response for subsequent requests #67
fix(fromTraffic): clone response for subsequent requests #67
Conversation
test/traffic/response-order.test.ts
Outdated
@@ -33,4 +33,11 @@ it('respects the response sequence when repeatedly requesting the same endpoint' | |||
}, | |||
}, | |||
]) | |||
// Any subsequent requests to the same endpoint should return a readable response. | |||
const repeatedHandlerResponse = await handlers[1]!.run({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put this in a separate test!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, created new separate test, response-cloning.test.ts
, with it's own archive and request script (duplicates of request-timing
).
@@ -69,7 +69,7 @@ export function fromTraffic( | |||
await delay(entry.time) | |||
} | |||
|
|||
return response | |||
return isUniqueHandler ? response.clone() : response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the repercussions of always cloning the response?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have detailed the repercussions of cloning (and not cloning) the response in this comment in the original issue. In brief summary, "cloning" a response does not duplicate the response data in memory, it points to the same internal buffer.
The response data is held in memory until there are no more unread responses. This should be fine since we are already keeping response data in memory for all generated handlers when we call toResponse
.
17352bb
to
187d01d
Compare
Repeated calls to unique request handlers would return unreadable responses that were already consumed. By cloning the response before returning, each returned response is able to be read as normal. Fixes mswjs#66
187d01d
to
d45c219
Compare
ba81836
to
c6ff13e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me! 👏
I've added withHandlers()
in tests to make the requests to say consistent.
Released: v0.3.2 🎉This has been released in v0.3.2! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Repeated calls to unique request handlers would return unreadable responses that were already consumed. By cloning the response before returning, each returned response is able to be read as normal.
Fixes #66