1 | var concat = require('simple-concat')
|
2 | var get = require('../')
|
3 | var http = require('http')
|
4 | var selfSignedHttps = require('self-signed-https')
|
5 | var test = require('tape')
|
6 |
|
7 | test('follow redirects (up to 10)', function (t) {
|
8 | t.plan(15)
|
9 |
|
10 | var num = 0
|
11 | var server = http.createServer(function (req, res) {
|
12 | t.equal(req.url, '/' + num, 'visited /' + num)
|
13 |
|
14 | if (num < 10) {
|
15 | num += 1
|
16 | res.statusCode = 301
|
17 | res.setHeader('Location', '/' + num)
|
18 | res.end()
|
19 | } else {
|
20 | res.statusCode = 200
|
21 | res.end('response')
|
22 | }
|
23 | })
|
24 |
|
25 | server.listen(0, function () {
|
26 | var port = server.address().port
|
27 | get('http://localhost:' + port + '/0', function (err, res) {
|
28 | t.error(err)
|
29 | t.equal(res.statusCode, 200)
|
30 | concat(res, function (err, data) {
|
31 | t.error(err)
|
32 | t.equal(data.toString(), 'response')
|
33 | server.close()
|
34 | })
|
35 | })
|
36 | })
|
37 | })
|
38 |
|
39 | test('do not follow redirects', function (t) {
|
40 | t.plan(2)
|
41 |
|
42 | var server = http.createServer(function (req, res) {
|
43 | t.equal(req.url, '/0', 'visited /0')
|
44 |
|
45 | res.statusCode = 301
|
46 | res.setHeader('Location', '/1')
|
47 | res.end()
|
48 | })
|
49 |
|
50 | server.listen(0, function () {
|
51 | var port = server.address().port
|
52 | get({
|
53 | url: 'http://localhost:' + port + '/0',
|
54 | maxRedirects: 0
|
55 | }, function (err) {
|
56 | t.ok(err instanceof Error, 'got error')
|
57 | server.close()
|
58 | })
|
59 | })
|
60 | })
|
61 |
|
62 | test('follow redirects (11 is too many)', function (t) {
|
63 | t.plan(12)
|
64 |
|
65 | var num = 0
|
66 | var server = http.createServer(function (req, res) {
|
67 | t.equal(req.url, '/' + num, 'visited /' + num)
|
68 |
|
69 | if (num < 11) {
|
70 | num += 1
|
71 | res.statusCode = 301
|
72 | res.setHeader('Location', '/' + num)
|
73 | res.end()
|
74 | } else {
|
75 | t.fail('no request to /11 should be made, should error first')
|
76 | }
|
77 | })
|
78 |
|
79 | server.listen(0, function () {
|
80 | var port = server.address().port
|
81 | get('http://localhost:' + port + '/0', function (err) {
|
82 | t.ok(err instanceof Error, 'got error')
|
83 | server.close()
|
84 | })
|
85 | })
|
86 | })
|
87 |
|
88 | test('redirect https to http', function (t) {
|
89 | t.plan(6)
|
90 |
|
91 | var httpPort = null
|
92 | var httpsPort = null
|
93 |
|
94 | var httpsServer = selfSignedHttps(function (req, res) {
|
95 | t.equal(req.url, '/path1')
|
96 | res.statusCode = 301
|
97 | res.setHeader('Location', 'http://localhost:' + httpPort + '/path2')
|
98 | res.end()
|
99 | })
|
100 |
|
101 | var httpServer = http.createServer(function (req, res) {
|
102 | t.equal(req.url, '/path2')
|
103 | res.statusCode = 200
|
104 | res.end('response')
|
105 | })
|
106 |
|
107 | httpsServer.listen(0, function () {
|
108 | httpsPort = httpsServer.address().port
|
109 | httpServer.listen(0, function () {
|
110 | httpPort = httpServer.address().port
|
111 | get({
|
112 | url: 'https://localhost:' + httpsPort + '/path1',
|
113 | rejectUnauthorized: false
|
114 | }, function (err, res) {
|
115 | t.error(err)
|
116 | t.equal(res.statusCode, 200)
|
117 | concat(res, function (err, data) {
|
118 | t.error(err)
|
119 | t.equal(data.toString(), 'response')
|
120 | httpsServer.close()
|
121 | httpServer.close()
|
122 | })
|
123 | })
|
124 | })
|
125 | })
|
126 | })
|
127 |
|
128 | test('redirect http to https', function (t) {
|
129 | t.plan(6)
|
130 |
|
131 | var httpsPort = null
|
132 | var httpPort = null
|
133 |
|
134 | var httpServer = http.createServer(function (req, res) {
|
135 | t.equal(req.url, '/path1')
|
136 | res.statusCode = 301
|
137 | res.setHeader('Location', 'https://localhost:' + httpsPort + '/path2')
|
138 | res.end()
|
139 | })
|
140 |
|
141 | var httpsServer = selfSignedHttps(function (req, res) {
|
142 | t.equal(req.url, '/path2')
|
143 | res.statusCode = 200
|
144 | res.end('response')
|
145 | })
|
146 |
|
147 | httpServer.listen(0, function () {
|
148 | httpPort = httpServer.address().port
|
149 | httpsServer.listen(0, function () {
|
150 | httpsPort = httpsServer.address().port
|
151 | get({
|
152 | url: 'http://localhost:' + httpPort + '/path1',
|
153 | rejectUnauthorized: false
|
154 | }, function (err, res) {
|
155 | t.error(err)
|
156 | t.equal(res.statusCode, 200)
|
157 | concat(res, function (err, data) {
|
158 | t.error(err)
|
159 | t.equal(data.toString(), 'response')
|
160 | httpsServer.close()
|
161 | httpServer.close()
|
162 | })
|
163 | })
|
164 | })
|
165 | })
|
166 | })
|
167 |
|
168 | test('redirect to different host/port', function (t) {
|
169 | t.plan(7)
|
170 |
|
171 | var port1 = null
|
172 | var port2 = null
|
173 |
|
174 | var server1 = http.createServer(function (req, res) {
|
175 | t.equal(req.url, '/path1')
|
176 | res.statusCode = 301
|
177 |
|
178 | res.setHeader('Location', 'http://127.0.0.1:' + port2 + '/path2')
|
179 | res.end()
|
180 | })
|
181 |
|
182 | var server2 = http.createServer(function (req, res) {
|
183 | t.equal(req.url, '/path2')
|
184 |
|
185 | t.equal(req.headers.host, `127.0.0.1:${port2}`)
|
186 | res.statusCode = 200
|
187 | res.end('response')
|
188 | })
|
189 |
|
190 | server1.listen(0, function () {
|
191 | port1 = server1.address().port
|
192 | server2.listen(0, function () {
|
193 | port2 = server2.address().port
|
194 | console.log(port1, port2)
|
195 | get('http://localhost:' + port1 + '/path1', function (err, res) {
|
196 | t.error(err)
|
197 | t.equal(res.statusCode, 200)
|
198 | concat(res, function (err, data) {
|
199 | t.error(err)
|
200 | t.equal(data.toString(), 'response')
|
201 | server1.close()
|
202 | server2.close()
|
203 | })
|
204 | })
|
205 | })
|
206 | })
|
207 | })
|
208 |
|
209 |
|
210 | test('redirect should clear explicitly specified `host` header', function (t) {
|
211 | t.plan(8)
|
212 |
|
213 | var port1 = null
|
214 | var port2 = null
|
215 |
|
216 | var server1 = http.createServer(function (req, res) {
|
217 | t.equal(req.url, '/path1')
|
218 | t.equal(req.headers.host, `localhost:${port1}`)
|
219 | res.statusCode = 301
|
220 |
|
221 | res.setHeader('Location', 'http://127.0.0.1:' + port2 + '/path2')
|
222 | res.end()
|
223 | })
|
224 |
|
225 | var server2 = http.createServer(function (req, res) {
|
226 | t.equal(req.url, '/path2')
|
227 |
|
228 |
|
229 | t.equal(req.headers.host, `127.0.0.1:${port2}`)
|
230 | res.statusCode = 200
|
231 | res.end('response')
|
232 | })
|
233 |
|
234 | server1.listen(0, function () {
|
235 | port1 = server1.address().port
|
236 | server2.listen(0, function () {
|
237 | port2 = server2.address().port
|
238 | console.log(port1, port2)
|
239 | get({
|
240 | url: `http://localhost:${port1}/path1`,
|
241 |
|
242 | headers: {
|
243 | host: `localhost:${port1}`
|
244 | }
|
245 | }, function (err, res) {
|
246 | t.error(err)
|
247 | t.equal(res.statusCode, 200)
|
248 | concat(res, function (err, data) {
|
249 | t.error(err)
|
250 | t.equal(data.toString(), 'response')
|
251 | server1.close()
|
252 | server2.close()
|
253 | })
|
254 | })
|
255 | })
|
256 | })
|
257 | })
|