Tags: cule/nock
Tags
fix(overrider): allow calling empty `req.end` multiple times Node source showing this is expected behavior: https://github.com/nodejs/node/blob/39a7f7663e8f70fc774105d8fa41b8e4cc69149f/lib/_http_outgoing.js#L816 Two tests were added, one for the regression itself, and one to fix coverage for a block that lost coverage when the bug was patched.
fix: Parallel requests on same Interceptor are exposed correctly in r… …eply functions (nock#2056) Fixes regression created in nock#2033. Reply functions get access to the current request via the Interceptor, which acts as the context during the function executions. The previous change, incorrectly moved the line where the request was attached to the Interceptor up in the flow and out of the same microtask that calls the reply functions. Causing requests in parallel to lose the reference to the correct request.
fix: data comparison with undefined object values (nock#2049) Updates the `dataEqual` utility and underlying `deepEqual` recursive functions to treat explicit and implicit `undefined` object values as equal. Also ensures that `expand` is only called on top-level data if they're plain objects, thus avoiding accidentally equating an object and an array if the object's keys happen to be integers.
fix(intercept): mark Interceptors consumed immediately (nock#2033) When an request is matched to an Interceptor, mark it consumed immediately so that similar requests made in parallel don't get matched incorrectly. This changes the behavior of aborted requests slightly. Previously, an Interceptor was marked consumed just prior to the "response" event. Now it will be marked consumed just prior to the "finished" event. The change can only be noticed if a request is aborted while in a "finished" listener.
fix(ClientRequest): Use native `abort` and `destroy` (nock#2000) - Stop monkey patching `abort` on the overridden `ClientRequest`. - Stop prematurely associating the socket with the request. - Prefer `destroy` instead of emitting an error when appropriate.
feat: remove socketDelay (nock#1974) From a user's perspective, having both `delayConnection` and `socketDelay` is confusing and unnecessarily. The differences between the two are subtle, and in my experience, users need to be fairly well versed in both the Node HTTP module and the internals of Nock in order to accurately determine which to use. On the surface there seems to be two use cases: - A user wants to test how their code handles a `timeout` event. Whether their client is expected to abort the request, or some other action is taken in response to the event. Either way, the user doesn't care if wall-clock time passes, and would prefer the timeout be simulated so their test suite can get on with it. - A user wants to force wall-clock time to pass before a `response` event. This is usually to test some timeout feature that is not based on the `timeout` event, or to give other code time to complete a task first. Based on those two use cases, it seems obvious that there should be two different delay functions, like we seem to have now. However, there are two subtle aspects that blur the line. - When a socket emits a `timeout` event, which the request propagates, the request does not end. This is true in Node and in Nock today. Clients my choose to abort a request upon a timeout event, they may not. - In Nock today, when the socket is "applying" the artificial timeout, to determine if it should emit a `timeout`, it doesn't just use the value passed to `socketDelay`. It uses the sum of the values passed to `delayConnection` and `socketDelay`. This covers the flow of a user wanting to ensure their code sees a `timeout` event even if no action is taken. Therefore, there is no reason to have two different options for users. The value passed to `delayConnection` can trigger a `timeout` event right away and if the code chooses not to act on the event, then it will wait in real time until `response` is triggered. In fact, when I began working on this, I went into the `test_socket_delay.js` file and replaced all `socketDelay` with `delayConnection`. All the tests passed. Other minor tweaks included in this change: - The delay methods on the Interceptor are now just setters instead of additive. This was undocumented, unintuitive behavior. - Fixed a bug from nock#1973, where `replayWithError` would cause Interceptors to be consumed twice. BREAKING CHANGE: - `socketDelay` has been removed. Use `delayConnection` instead. - `delay`, `delayConnection`, and `delayBody` are now setters instead of additive. example: ```js nock('https://example.com') .get('/') .delay(1) .delay({ head: 2, body: 3 }) .delayConnection(4) .delayBody(5) .delayBody(6) .reply() ``` Previously, the connection would have been delayed by 7 and the body delayed by 14. Now, the connection will be delayed by 4 and the body delayed by 6.
feature: remove Scope.log in favor of more debug (nock#1966) Consolidates the behavior around debugging and logging to exclusively use [`debug`](https://github.com/visionmedia/debug). The use of `scope.log(console.log)` was problematic and confusing for consumers as that appeared to be the correct approach when debugging unless they found the correct section in the README. The use of the logger vs directly using `debug` in the core of Nock was inconsistent, especially when matching. Now any failure to match a request will be directed to `debug` and if a single Interceptor is in context, the namespace in the log will include the host from the Scope. This allows for filtering of logs to stdout as well as differentiating colors from `debug`, when enabled. BREAKING CHANGE: `Scope.log` has been removed. Use the `debug` library when [debugging](https://github.com/nock/nock#debugging) failed matches.
fix(router): bring behavior of `abort()` inline with native Node (noc… …k#1960) Nocks behavior around events and errors relating to aborting and ending requests has not lined up with Node for while. I dug into an investigation of how it differs, if there were git commits/pull-requests explaining why they differ, and trying to answer the question of should Nock change to line up better. The single largest deviation were the errors emitted around aborting or already aborted requests. Emitting an error on abort was added to fix issue nock#439. The conversation talks about how Node emits an error post abort. > ... in practice the `http` module does emit an `ECONNRESET` error after aborting. ... - Jan 7, 2016 However, this is only true between the 'socket' and 'response' events. Otherwise calling abort does not emit an error of any kind. Determining exactly what Nock should do is a bit of moving target. As Node's internals of HTTP, Net, Stream, and micro task timing have changed a lot since iojs/v4, when Nock added these errors, and v13, current at this time. My conclusion is that none of Nock's deviations from Node's _documented_ behavior are user features that should be maintained. If anything, bringing Nock closer to Node better fulfills original requests. nock#282 The entire `test_abort.js` file was scraped and rewritten by writing assertions against Node behavior, without Nock first. Then adding Nock into each test to ensure events and errors were correct. BREAKING CHANGE: Calling `request.abort()`: - Use to always emit a 'socket hang up' error. Now only emits the error if `abort` is called between the 'socket' and 'response' events. - The emitted 'abort' event now happens on `nextTick`. - The socket is now only destroyed if the 'socket' event has fired, and now emits a 'close' event on `nextTick` that propagates though the request object. - `request.aborted` attribute is set to `true` instead of a timestamp. Changed in Node v11.0 nodejs/node#20230 Calling `write`, `end`, or `flushHeaders` on an aborted request no longer emits an error. However, writing to a request that is already finished (ended) will emit a 'write after end' error. Playback of a mocked responses will now never happen until the 'socket' event is emitted. The 'socket' event is still artificially set to emit on `nextTick` when a ClientRequest is created. This means in the following code the Scope will never be done because at least one tick needs to happen before any matched Interceptor is considered consumed. ```js const scope = nock(...).get('/').reply() const req = http.get(...) scope.done() ```
PreviousNext