centra icon indicating copy to clipboard operation
centra copied to clipboard

IPv6 with brackets causes a DNS error

Open jmcdo29 opened this issue 3 years ago • 0 comments

Bug Description

When working with pactum for E2E testing and NestJS for the HTTP server framework, I ran into a bug when trying to make a request to http://[::1]:3000 about [::1] not being as found address. After digging in more I found that pactum uses phin and phin uses centra.

Current Behavior

You can run the following in a Node REPL. I'll make a full minimum reproduction repository if you'd like me to as well :smile:

centra('http://[::1]:3000').send().then(console.log)

And receive the error

Uncaught Error: getaddrinfo ENOTFOUND [::1]
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
    at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '[::1]'
}

Expected Behavior

I should be able to make an HTTP request using an IPv6 with brackets separating the host and the port

Proposed Solution

The reason for this error is due to the URL.hostname property. The hostname itself should resolve to ::1 instead of [::1] as described in this issue on the Node repo. For node 15+ we can use urlToHttpOptions() from the url package instead of using Object.assign() on line 109 onf CentraRequest.js. However, if support for Node 12 and 14 is still required, we can do two replaces for the [ and ] characters, as they're not valid http host characters anyways.

I'm more than happy to implement this, just need to know which approach you'd prefer

Other information

Node: 16.13.0

jmcdo29 avatar Jan 11 '22 22:01 jmcdo29