Skip to content

Commit

Permalink
spike: investigate localhost resolution RFC6761
Browse files Browse the repository at this point in the history
  • Loading branch information
filfreire committed Nov 22, 2024
1 parent 4b316bc commit aab6868
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions test/curl/localhost.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'should'
import { app, host, port, server } from '../helper/server'
import { Curl } from '../../lib'

let curl: Curl
const urlLocalhost = `http://localhost:${port}/`
const urlTestLocalhost = `http://test.localhost:${port}/`

describe('DNS Resolution', () => {
before((done) => {
server.listen(port, host, () => done())

app.get('/', (req, res) => {
res.send({ message: 'resolved', ip: req.ip })
})
})

after((done) => {
server.close()
app._router.stack.pop()
done()
})

beforeEach(() => {
curl = new Curl()
})

afterEach(() => {
curl.close()
})

it('should resolve localhost to 127.0.0.1', (done) => {
curl.setOpt('URL', urlLocalhost)

curl.on('end', (status, data) => {
if (status !== 200) {
throw Error(`Invalid status code: ${status}`)
}

const result = JSON.parse(data as string)
result.message.should.be.equal('resolved')
result.ip.should.be.equal('::1') // IPv4-mapped IPv6 address often used by Node.js

done()
})

curl.on('error', done)
curl.perform()
})

it('should resolve test.localhost to 127.0.0.1', (done) => {
curl.setOpt('URL', urlTestLocalhost)

curl.on('end', (status, data) => {
if (status !== 200) {
throw Error(`Invalid status code: ${status}`)
}

const result = JSON.parse(data as string)
result.message.should.be.equal('resolved')
result.ip.should.be.equal('::1') // IPv4-mapped IPv6 address often used by Node.js

done()
})

curl.on('error', done)
curl.perform()
})
})

0 comments on commit aab6868

Please sign in to comment.