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

feat(mock): add support reply option callbacks #1211

Merged
merged 4 commits into from
Feb 8, 2022

Conversation

suneettipirneni
Copy link
Contributor

This PR is similar to #1200, except it allows dynamic mocking of all reply options rather than just the data.

mockPool.intercept({
  path: '/echo',
  method: 'GET',
  headers: {
    'User-Agent': 'undici',
    Host: 'example.com'
  }
}).reply(({ headers }) => ({ statusCode: 200, data: { message: headers.get('message') }})))

const { statusCode, body, headers } = await request('http://localhost:3000', {
  headers: {
    message: 'hello world!'
  }
})

console.log('response received', statusCode) // response received 200
console.log('headers', headers) // { 'content-type': 'application/json' }

for await (const data of body) {
  console.log('data', data.toString('utf8')) // { "message":"hello world!" }
}

This is useful in situations where the status code and the response headers need to be dynamically mocked.

@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2022

Codecov Report

Merging #1211 (3620ca1) into main (d94dc15) will increase coverage by 0.05%.
The diff coverage is 100.00%.

❗ Current head 3620ca1 differs from pull request most recent head db09c2d. Consider uploading reports for the commit db09c2d to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1211      +/-   ##
==========================================
+ Coverage   93.38%   93.43%   +0.05%     
==========================================
  Files          43       43              
  Lines        4035     4052      +17     
==========================================
+ Hits         3768     3786      +18     
+ Misses        267      266       -1     
Impacted Files Coverage Δ
lib/mock/mock-interceptor.js 100.00% <100.00%> (ø)
lib/mock/mock-utils.js 100.00% <100.00%> (ø)
lib/fetch/request.js 82.57% <0.00%> (+0.41%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d94dc15...db09c2d. Read the comment docs.

test/mock-interceptor.js Outdated Show resolved Hide resolved
test/types/mock-interceptor.test-d.ts Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 0b92cb4 into nodejs:main Feb 8, 2022
@suneettipirneni suneettipirneni deleted the feat/reply-options-callback branch February 8, 2022 22:54
KhafraDev pushed a commit to KhafraDev/undici that referenced this pull request Jun 23, 2022
metcoder95 pushed a commit to metcoder95/undici that referenced this pull request Dec 26, 2022
crysmags pushed a commit to crysmags/undici that referenced this pull request Feb 27, 2024
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

Successfully merging this pull request may close these issues.

3 participants