Skip to content

Commit ff3ca3a

Browse files
committed
test: add tests for request-shim
1 parent 42d2053 commit ff3ca3a

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

test/test-request-shim.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const fs = require('fs')
5+
const path = require('path')
6+
const http = require('http')
7+
const https = require('https')
8+
const { test: { readCAFile } } = require('../lib/install')
9+
const Request = require('../lib/request-shim')
10+
11+
test('request over http', t => {
12+
t.plan(1)
13+
14+
const server = http.createServer((_, res) => {
15+
res.end('ok')
16+
server.close()
17+
})
18+
19+
const host = 'localhost'
20+
server.listen(0, host, () => {
21+
const { port } = server.address()
22+
23+
const req = new Request({
24+
uri: `http://${host}:${port}`
25+
})
26+
27+
req.on('response', res => {
28+
let body = ''
29+
res.setEncoding('utf8')
30+
res.on('data', data => {
31+
body += data
32+
})
33+
res.on('end', () => {
34+
t.strictEqual(body, 'ok')
35+
})
36+
})
37+
})
38+
})
39+
40+
test('request over https with custom ca', t => {
41+
t.plan(2)
42+
43+
const cert = fs.readFileSync(path.join(__dirname, 'fixtures/server.crt'), 'utf8')
44+
const key = fs.readFileSync(path.join(__dirname, 'fixtures/server.key'), 'utf8')
45+
46+
const cafile = path.join(__dirname, '/fixtures/ca.crt')
47+
const ca = readCAFile(cafile)
48+
t.strictEqual(ca.length, 1)
49+
50+
const options = { ca: ca, cert: cert, key: key }
51+
const server = https.createServer(options, (_, res) => {
52+
res.end('ok')
53+
server.close()
54+
})
55+
56+
server.on('clientError', err => {
57+
throw err
58+
})
59+
60+
const host = 'localhost'
61+
server.listen(0, host, () => {
62+
const { port } = server.address()
63+
64+
const req = new Request({
65+
uri: `https://${host}:${port}`,
66+
ca
67+
})
68+
69+
req.on('response', res => {
70+
let body = ''
71+
res.on('data', data => {
72+
body += data
73+
})
74+
res.on('end', () => {
75+
t.strictEqual(body, 'ok')
76+
})
77+
})
78+
})
79+
})
80+
81+
test('request over http with proxy', t => {
82+
t.plan(1)
83+
84+
const server = http.createServer((_, res) => {
85+
res.end('ok')
86+
pserver.close(() => {
87+
this.close()
88+
})
89+
})
90+
91+
const pserver = http.createServer((_, res) => {
92+
res.end('proxy ok')
93+
server.close(() => {
94+
pserver.close()
95+
})
96+
})
97+
98+
const host = 'localhost'
99+
server.listen(0, host, () => {
100+
const { port } = server.address()
101+
pserver.listen(port + 1, host, () => {
102+
const req = new Request({
103+
uri: `http://${host}:${port}`,
104+
proxy: `http://${host}:${port + 1}`
105+
})
106+
107+
req.on('response', res => {
108+
let body = ''
109+
res.on('data', data => {
110+
body += data
111+
})
112+
res.on('end', () => {
113+
t.strictEqual(body, 'proxy ok')
114+
})
115+
})
116+
})
117+
})
118+
})
119+
120+
test('request over redirected http', t => {
121+
t.plan(2)
122+
123+
const server = http.createServer((req, res) => {
124+
if (req.url === '/') {
125+
res.writeHead(301, { location: '/redirect' })
126+
res.end()
127+
} else {
128+
t.strictEqual(req.url, '/redirect')
129+
res.end('redirect ok')
130+
server.close()
131+
}
132+
})
133+
134+
const host = 'localhost'
135+
server.listen(0, host, () => {
136+
const { port } = server.address()
137+
138+
const req = new Request({
139+
uri: `http://${host}:${port}`
140+
})
141+
142+
req.on('response', res => {
143+
let body = ''
144+
res.on('data', data => {
145+
body += data
146+
})
147+
res.on('end', () => {
148+
t.strictEqual(body, 'redirect ok')
149+
})
150+
})
151+
})
152+
})

0 commit comments

Comments
 (0)