Skip to content

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

Merged
mcollina merged 4 commits intonodejs:mainfrom
suneettipirneni:feat/reply-options-callback
Feb 8, 2022
Merged

feat(mock): add support reply option callbacks#1211
mcollina merged 4 commits intonodejs:mainfrom
suneettipirneni:feat/reply-options-callback

Conversation

@suneettipirneni
Copy link
Copy Markdown
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.

@suneettipirneni suneettipirneni force-pushed the feat/reply-options-callback branch from 97544f7 to bec4b76 Compare February 8, 2022 02:23
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 8, 2022

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.43%. Comparing base (d94dc15) to head (db09c2d).
⚠️ Report is 2159 commits behind head on main.

Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@suneettipirneni suneettipirneni force-pushed the feat/reply-options-callback branch from bec4b76 to f467961 Compare February 8, 2022 02:33
Comment thread test/mock-interceptor.js Outdated
Comment thread test/types/mock-interceptor.test-d.ts
Copy link
Copy Markdown
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