Skip to content

Commit

Permalink
test: add tests for request-shim
Browse files Browse the repository at this point in the history
  • Loading branch information
imatlopez committed Nov 3, 2020
1 parent 42d2053 commit ff3ca3a
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions test/test-request-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
'use strict'

const { test } = require('tap')
const fs = require('fs')
const path = require('path')
const http = require('http')
const https = require('https')
const { test: { readCAFile } } = require('../lib/install')
const Request = require('../lib/request-shim')

test('request over http', t => {
t.plan(1)

const server = http.createServer((_, res) => {
res.end('ok')
server.close()
})

const host = 'localhost'
server.listen(0, host, () => {
const { port } = server.address()

const req = new Request({
uri: `http://${host}:${port}`
})

req.on('response', res => {
let body = ''
res.setEncoding('utf8')
res.on('data', data => {
body += data
})
res.on('end', () => {
t.strictEqual(body, 'ok')
})
})
})
})

test('request over https with custom ca', t => {
t.plan(2)

const cert = fs.readFileSync(path.join(__dirname, 'fixtures/server.crt'), 'utf8')
const key = fs.readFileSync(path.join(__dirname, 'fixtures/server.key'), 'utf8')

const cafile = path.join(__dirname, '/fixtures/ca.crt')
const ca = readCAFile(cafile)
t.strictEqual(ca.length, 1)

const options = { ca: ca, cert: cert, key: key }
const server = https.createServer(options, (_, res) => {
res.end('ok')
server.close()
})

server.on('clientError', err => {
throw err
})

const host = 'localhost'
server.listen(0, host, () => {
const { port } = server.address()

const req = new Request({
uri: `https://${host}:${port}`,
ca
})

req.on('response', res => {
let body = ''
res.on('data', data => {
body += data
})
res.on('end', () => {
t.strictEqual(body, 'ok')
})
})
})
})

test('request over http with proxy', t => {
t.plan(1)

const server = http.createServer((_, res) => {
res.end('ok')
pserver.close(() => {
this.close()
})
})

const pserver = http.createServer((_, res) => {
res.end('proxy ok')
server.close(() => {
pserver.close()
})
})

const host = 'localhost'
server.listen(0, host, () => {
const { port } = server.address()
pserver.listen(port + 1, host, () => {
const req = new Request({
uri: `http://${host}:${port}`,
proxy: `http://${host}:${port + 1}`
})

req.on('response', res => {
let body = ''
res.on('data', data => {
body += data
})
res.on('end', () => {
t.strictEqual(body, 'proxy ok')
})
})
})
})
})

test('request over redirected http', t => {
t.plan(2)

const server = http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(301, { location: '/redirect' })
res.end()
} else {
t.strictEqual(req.url, '/redirect')
res.end('redirect ok')
server.close()
}
})

const host = 'localhost'
server.listen(0, host, () => {
const { port } = server.address()

const req = new Request({
uri: `http://${host}:${port}`
})

req.on('response', res => {
let body = ''
res.on('data', data => {
body += data
})
res.on('end', () => {
t.strictEqual(body, 'redirect ok')
})
})
})
})

0 comments on commit ff3ca3a

Please sign in to comment.