Closed mikicho closed 1 year ago
Hi, @mikicho. Thanks for proposing this.
Can you please tell me more about the expected behavior here? Do you wish to see the username/password represented as the Basic
authentication header on the constructed Request
instance? I do believe we grab the values from the options and forward them onto the ClientRequest
instance as of now.
The request fails when we send auth option to http.request:
You must be referring to some third-party request module. auth
isn't a supported property on the http.ClientRequest
constructor. Instead, Node.js uses username
and password
properties:
http.request({
method: 'POST',
host: 'localhost',
port: 12345,
username: 'john',
password: 'supersecret123'
})
Can you please tell me more about the expected behavior here?
I think this is a bug:
const { ClientRequestInterceptor } = require('@mswjs/interceptors/ClientRequest')
const http = require('http')
const interceptor = new ClientRequestInterceptor({
name: 'my-interceptor',
})
interceptor.apply();
interceptor.on('request', async ({request, a}) => {
await new Promise(r => setTimeout(r, 700));
throw new Error('error')
});
const req = http.request(
{
host: 'example.test',
path: '/wrong-path',
auth: 'user:pass'
},
)
req.end() // TypeError: Request cannot be constructed from a URL that includes credentials: http://user:pass@example.test/wrong-path
Do you wish to see the username/password represented...
Probably yes.
You must be referring to some third-party request module...
It's one of the http.request
options: https://nodejs.org/api/http.html#httprequesturl-options-callback
Oh, I've overlooked that one 🤦 Thanks for pointing out. I wonder why this errors.
The error is from the createRequest
function:
I see. So we need to omit the credentials from the URL itself and instead represent them in the Authorization
request header. I think that's doable! I think this implementation can consist of multiple steps.
username
and password
onto the NodeClientRequest.url
. From the source of it, we don't do that, but it doesn't hurt to check. createRequest
, check request.username
and request.password
, and translate them into the Authorization
header. If you have time, I'd be thankful if you opened a pull request and we iterated on this together. Starting with some integration test would also be a good point.
on it
This has been released in v0.25.4!
Make sure to always update to the latest version (npm i @mswjs/interceptors@latest
) to get the newest features and bug fixes.
Predictable release automation by @ossjs/release.
The request fails when we send
auth
option tohttp.request
:logs
I think we should add some logic into the
createRequest
function https://github.com/mswjs/interceptors/blob/main/src/interceptors/ClientRequest/NodeClientRequest.ts#L141I think I can open a PR if you think I'm correct and we should fix the
createRequest
function