Skip to content

refactor: Refactor RequestOverrider#1674

Merged
paulmelnikow merged 7 commits intonextfrom
refactor-request-overrider
Aug 17, 2019
Merged

refactor: Refactor RequestOverrider#1674
paulmelnikow merged 7 commits intonextfrom
refactor-request-overrider

Conversation

@paulmelnikow
Copy link
Copy Markdown
Member

This makes RequestOverrider into a class and breaks its logic up into several shorter methods. The goal is to make this code easier to understand, and easier to change. There is more that can be done here, though this seems like a meaningful place to pause.

Git and GitHub are not detecting this as a rename, though it is probably just as well, as the diff would be hard to read. I think the best way to review this is by first reading the new implementation to see where things ended up, then reviewing the old one chunk by chunk. I'll leave some comments in the old code indicating where it ended up in the new code.

This makes RequestOverrider into a class and breaks its logic up into several shorter methods. There is more that can be done here, though this seems like a meaningful place to pause.
Comment thread lib/request_overrider.js

if (options.headers) {
// We use lower-case header field names throughout Nock.
options.headers = common.headersFieldNamesToLowerCase(options.headers)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved to the constructor.

Comment thread lib/request_overrider.js
// changes affecting the user so we use a clone of the object.
options = _.clone(options)

response.req = req
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved to attachToReq().

Comment thread lib/request_overrider.js

// We may be changing the options object and we don't want those
// changes affecting the user so we use a clone of the object.
options = _.clone(options)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved to the constructor.

Comment thread lib/request_overrider.js
if (req.socket.authorized) {
req.socket.emit('secureConnect')
}
})
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All moved to attachToReq().

Comment thread lib/request_overrider.js
}
})

req.write = function(buffer, encoding, callback) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These implementations were moved to e.g. handleWrite, which in attachToReq() are set to override e.g. req.write.

Comment thread lib/request_overrider.js
const end = function() {
debug('ending')
ended = true
let requestBody, responseBody, responseBuffers
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

requestBody is now a local variable in end(). { responseBody, responseBuffers } become named parameters to continueWithResponseBody().

Comment thread lib/request_overrider.js

return continueWithResponseBody(responseBody)

function continueWithFullResponse(fullReplyResult) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved to continueWithFullResponse() (a method).

Comment thread lib/request_overrider.js
continueWithResponseBody(responseBody)
}

function continueWithResponseBody(responseBody) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved to continueWithResponseBody() (a method).

Comment thread lib/request_overrider.js

process.nextTick(respond)

function respond() {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved to respondUsingInterceptor() (a method).

Comment thread lib/request_overrider.js
_respond()
}

function _respond() {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Now a local function inside respondUsingInterceptor().

@mastermatt
Copy link
Copy Markdown
Member

I have reservations on losing all the git history for the overrider.
Could you split the renaming and the classification up into separate commits, then ensure they stay separate when merged onto the base branch?

@paulmelnikow
Copy link
Copy Markdown
Member Author

I could rename it back in this PR (and then squash it). I think the file will still be rewritten on this commit, though, so I'm not sure if it will make of a difference.

@paulmelnikow
Copy link
Copy Markdown
Member Author

GitHub is noticing the rename now, so I guess this way it will be detected as a rename when it's actually renamed.

Copy link
Copy Markdown
Member

@mastermatt mastermatt left a comment

Choose a reason for hiding this comment

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

This looks good.

Comment thread lib/request_overrider.js Outdated
@paulmelnikow
Copy link
Copy Markdown
Member Author

Thanks for reviewing, Matt!

@paulmelnikow paulmelnikow merged commit c09afda into next Aug 17, 2019
@paulmelnikow paulmelnikow deleted the refactor-request-overrider branch August 17, 2019 13:54
@nockbot
Copy link
Copy Markdown
Collaborator

nockbot commented Aug 20, 2019

🎉 This PR is included in version 11.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

gr2m pushed a commit that referenced this pull request Sep 4, 2019
This makes RequestOverrider into a class and breaks its logic up into several shorter methods. The goal is to make this code easier to understand, and easier to change. There is more that can be done here, though this seems like a meaningful place to pause.
gr2m pushed a commit that referenced this pull request Sep 4, 2019
This makes RequestOverrider into a class and breaks its logic up into several shorter methods. The goal is to make this code easier to understand, and easier to change. There is more that can be done here, though this seems like a meaningful place to pause.
gr2m pushed a commit that referenced this pull request Sep 5, 2019
This makes RequestOverrider into a class and breaks its logic up into several shorter methods. The goal is to make this code easier to understand, and easier to change. There is more that can be done here, though this seems like a meaningful place to pause.
juninmd pushed a commit to juninmd/nock that referenced this pull request Mar 21, 2026
This makes RequestOverrider into a class and breaks its logic up into several shorter methods. The goal is to make this code easier to understand, and easier to change. There is more that can be done here, though this seems like a meaningful place to pause.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants