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

Response headers/raw headers to more closely match Node's functionality. #1564

Merged
merged 4 commits into from
Jun 8, 2019

Commits on Jun 2, 2019

  1. Response headers to more closely match Node.

    Updates the header handling in the `Interceptor` and `RequestOverrider` with
    the intention of mimicking the native behavior of `http.IncomingMessage.rawHeaders`.
    > The raw request/response headers list exactly as they were received.
    
    There are three fundamental changes in this changeset:
    
    1) Header Input Type
    
    Previously, headers could be provided to:
    - `Scope.defaultReplyHeaders` as a plain object
    - `Interceptor.reply(status, body, headers)` as a plain object or an array of raw headers
    - `Interceptor.reply(() => [status, body, headers]` as a plain object
    
    Now, all three allow consistent inputs where the headers can be provided as a
    plain object, an array of raw headers, or a `Map`.
    
    2) Duplicate Headers Folding
    
    This change deviates from the suggested guidelines laid out in nock#1553 because
    those guidelines didn't properly handle duplicate headers, especially when
    some are defined as defaults.
    
    This change was modeled to duplicate [Node's implementation](https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/_http_incoming.js#L245)
    ([relevant docs](https://nodejs.org/api/http.html#http_message_headers)).
    It specifically lays out how duplicate headers are handled depending on the field name.
    
    In the case of default headers, they are not included on the `Response`
    (not even in the raw headers) if the field name exists in the reply headers
    (using a case-insensitive comparison).
    
    3) Raw Headers are the Source of Truth
    
    Previously, the `Interceptor` and `RequestOverrider` mostly keep track of
    headers as a plain object and the array of raw headers was created by looping
    that object. This was the cause for inconsistencies with the final result of
    the raw headers. The problem with that approach is that converting raw headers
    to an object is a lossy process, so going backwards makes it impossible to
    guarantee the correct results.
    
    This change reverses that logic and now the `Interceptor` and `RequestOverrider`
    maintain the header data in raw arrays. All additions to headers are only added
    to raw headers. The plain headers object is never mutated directly, and instead
    is [re]created from the raw headers as needed.
    mastermatt committed Jun 2, 2019
    Configuration menu
    Copy the full SHA
    9474980 View commit details
    Browse the repository at this point in the history
  2. Apply suggestions from code review

    Co-Authored-By: Paul Melnikow <github@paulmelnikow.com>
    mastermatt and paulmelnikow committed Jun 2, 2019
    Configuration menu
    Copy the full SHA
    a8e8c3d View commit details
    Browse the repository at this point in the history
  3. Remove unused mixin util.

    mastermatt committed Jun 2, 2019
    Configuration menu
    Copy the full SHA
    c2579f7 View commit details
    Browse the repository at this point in the history

Commits on Jun 4, 2019

  1. Configuration menu
    Copy the full SHA
    0240201 View commit details
    Browse the repository at this point in the history