-
-
Notifications
You must be signed in to change notification settings - Fork 735
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
Use @mswjs/interceptors for mocking - WIP #2517
Draft
mikicho
wants to merge
30
commits into
nock:main
Choose a base branch
from
mikicho:Michael/fetch-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+549
−479
Draft
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
576b296
wip
mikicho 94b9b90
wip
mikicho ef59cf6
wip
mikicho d7d17bd
fixes
mikicho 855914e
fix
mikicho 51dd0c6
wip
mikicho 21cf9c2
more fixes
mikicho 091d876
more fixes
mikicho 79f5162
more fixes
mikicho 2028d4e
more fixes
mikicho bba8bdb
small fix
mikicho 5e521a1
more fixes
mikicho e1c3eb4
more fixes
mikicho e89acec
Merge remote-tracking branch 'upstream/main' into Michael/fetch-support
mikicho cbd31cb
wip
mikicho bc4327a
Merge remote-tracking branch 'upstream/main' into Michael/fetch-support
mikicho 7df18f2
wip
mikicho 53e3d2a
fix
mikicho ca9e616
fix
mikicho 2c0e4f1
more-tests
mikicho bcbdd04
wip
mikicho ab8037d
wip
mikicho e7a6309
fix
mikicho dac5a26
fix
mikicho 36f6778
fix
mikicho 73e507d
fix
mikicho 7c89896
fix
mikicho dfd069c
fix
mikicho cf3b522
fix
mikicho 16453f7
Merge remote-tracking branch 'origin/main' into Michael/fetch-support
mikicho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const { IncomingHttpHeaders, IncomingMessage } = require('http') | ||
Check failure on line 1 in lib/create_response.js GitHub Actions / Lint JavaScript
Check failure on line 1 in lib/create_response.js GitHub Actions / Lint JavaScript
|
||
|
||
/** | ||
* Creates a Fetch API `Response` instance from the given | ||
* `http.IncomingMessage` instance. | ||
* copied from: https://github.com/mswjs/interceptors/blob/04152ed914f8041272b6e92ed374216b8177e1b2/src/interceptors/ClientRequest/utils/createResponse.ts#L8 | ||
*/ | ||
|
||
/** | ||
* @param {IncomingMessage} message | ||
*/ | ||
function createResponse(message) { | ||
const readable = new ReadableStream({ | ||
start(controller) { | ||
message.on('data', (chunk) => controller.enqueue(chunk)) | ||
message.on('end', () => controller.close()) | ||
|
||
/** | ||
* @todo Should also listen to the "error" on the message | ||
* and forward it to the controller. Otherwise the stream | ||
* will pend indefinitely. | ||
*/ | ||
}, | ||
}) | ||
|
||
return new Response(readable, { | ||
status: message.statusCode, | ||
statusText: message.statusMessage, | ||
headers: createHeadersFromIncomingHttpHeaders(message.headers), | ||
}) | ||
} | ||
|
||
/** | ||
* @param {IncomingHttpHeaders} httpHeaders | ||
*/ | ||
function createHeadersFromIncomingHttpHeaders(httpHeaders) { | ||
const headers = new Headers() | ||
|
||
for (const headerName in httpHeaders) { | ||
const headerValues = httpHeaders[headerName] | ||
|
||
if (typeof headerValues === 'undefined') { | ||
continue | ||
} | ||
|
||
if (Array.isArray(headerValues)) { | ||
headerValues.forEach((headerValue) => { | ||
headers.append(headerName, headerValue) | ||
}) | ||
|
||
continue | ||
} | ||
|
||
headers.set(headerName, headerValues) | ||
} | ||
|
||
return headers | ||
} | ||
|
||
module.exports = { createResponse } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 like the version of Node Nock uses doesn't have the
ReadableStream
global. I'd double-check if this isn't the linter's problem as the first measure.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.
Sometimes the linter doesn't play well with relatively newly added globals. This can be modified in the linter's
globals
key, if talking about ESLint.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.
Thanks!
I think we can fix this after we will solve the
got
error. Most of Nock's tests are based on it and I'm eager to solve it and run all tests to find cases that we haven't covered yet.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 will try to look into the
got
issue this week based on my availability. Right now, the best next step is to reliably reproduce it in an automated test. I've already written one (see https://github.com/mswjs/interceptors/pull/432?notification_referrer_id=NT_kwDOAOSmz7M3NzYwMzQ3MDA1OjE0OTg0OTEx¬ifications_query=is%3Aunread#issuecomment-1724139617) but you've provided some input that I need to reflect in the 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.
I've fixed the
got
issue, would appreciate a quick review of the pull request!