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

fix: Interceptor.reply to allow callback and raw headers. #1517

Closed

Conversation

mastermatt
Copy link
Member

Fixes #1513.

Ensures raw headers are still returned when using the form reply(callback, rawHeaders).

Fixes nock#1513.

Ensures raw headers are still returned when using the form `reply(callback, rawHeaders)`.
@mastermatt mastermatt force-pushed the bug/reply-allow-callback-and-headers branch from 7c51d97 to 2249dda Compare May 1, 2019 17:00
@mastermatt
Copy link
Member Author

fyi @paulmelnikow

@@ -453,7 +454,7 @@ Interceptor.prototype.basicAuth = function basicAuth(options) {
/**
* Set query strings for the interceptor
* @name query
* @param Object Object of query string name,values (accepts regexp values)
* @param queries Object of query string name,values (accepts regexp values)
Copy link
Member

Choose a reason for hiding this comment

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

👍

@@ -495,7 +496,7 @@ Interceptor.prototype.query = function query(queries) {
/**
* Set number of times will repeat the interceptor
* @name times
* @param Integer Number of times to repeat (should be > 0)
* @param newCounter Number of times to repeat (should be > 0)
Copy link
Member

Choose a reason for hiding this comment

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

😀

.reply(
() => [202, 'body', { 'x-key': 'value', 'x-key-2': 'value 2 override' }],
{ 'x-key-2': 'value 2', 'x-key-3': 'value 3' }
)
Copy link
Member

Choose a reason for hiding this comment

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

Oh no! I didn't realize it was possible to pass the headers to the callback. That reverses what I wrote at #1513 (comment). I'm sorry for my misunderstanding!

I do not think we need to provide so many different ways of doing this. 😝

What about supporting these forms:

  1. .reply(() => 'body')
  2. .reply(() => ['body', { 'x-key': 'value', 'x-key-2': 'value 2 override' }])
  3. .reply(() => [202, 'body', { 'x-key': 'value', 'x-key-2': 'value 2 override' }])
  4. .reply((uri, requestBody, cb) => cb(null, 'body'), 1000))
  5. .reply((uri, requestBody, cb) => cb(null, ['body', { 'x-key': 'value', 'x-key-2': 'value 2 override' }]), 1000))
  6. .reply((uri, requestBody, cb) => cb(null, [201, 'body', { 'x-key': 'value', 'x-key-2': 'value 2 override' }]), 1000))

That way the function and async forms have effectively the same interfaces as the synchronous calls to .reply().

Ideally all these paths would be tested, and the documentation would make it clear what is possible.

Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think my issue/question/change relates to the sync vs async forms of the callback.

I thinks it's more about a confusion behind the intended signature of the method, regardless of what the callbadk returns..
My original assumption was the intended signature was the two forms below. This seems to closely match how I interpret the docs as they stand today.
.replay(statusCode, [bodyOrCallback, [rawHeaders]])
.replay(callback)

If that is correct, I think we should update the beginning of the method to the following

  if (_.isFunction(statusCode)) {
    body = statusCode
    statusCode = 200
    rawHeaders = undefined
  }

The arguments.length <= 2 is confusing/misleading, and clearing the rawHeaders clarifies the method's intention to anyone digging around, plus reduces the changes for bugs in the future.

That being said, this PR does not do this.
Instead it makes the signature of the method more like this
.replay([statusCode], [bodyOrCallback, [rawHeaders]])
or maybe it's better described as
.replay(statusCode, [bodyOrCallback, [rawHeaders]])
.replay(callback, [rawHeaders])

Personally, I'm less of a fan of having all these ways of accomplishing the same thing, so I'd be happy to close this PR. I just want to make sure we're on the same page.

Copy link
Member

Choose a reason for hiding this comment

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

My original assumption was the intended signature was the two forms below. This seems to closely match how I interpret the docs as they stand today.
.replay(statusCode, [bodyOrCallback, [rawHeaders]])
.replay(callback)

Yes, I agree this is a better interface.

Personally, I'm less of a fan of having all these ways of accomplishing the same thing, so I'd be happy to close this PR. I just want to make sure we're on the same page.

Sounds good. Thanks!

@paulmelnikow
Copy link
Member

@mastermatt Thanks so much for this! I realized from reading your tests that I misunderstood what was currently possible with our API. Left you a comment; happy to discuss!

@mastermatt mastermatt closed this May 1, 2019
@mastermatt mastermatt deleted the bug/reply-allow-callback-and-headers branch May 1, 2019 19:21
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.

Interceptor.reply to allow callback and raw headers.
2 participants