|
| 1 | +import {Agent as HttpAgent} from 'http'; |
| 2 | +import {Agent as HttpsAgent} from 'https'; |
1 | 3 | import test from 'ava'; |
2 | 4 | import pem from 'pem'; |
3 | 5 | import pify from 'pify'; |
| 6 | +import sinon from 'sinon'; |
4 | 7 | import got from '..'; |
5 | 8 | import {createServer, createSSLServer} from './helpers/server'; |
6 | 9 |
|
@@ -35,12 +38,22 @@ test.before('setup', async () => { |
35 | 38 | const cert = keys.certificate; |
36 | 39 |
|
37 | 40 | https = await createSSLServer({key, cert}); // eslint-disable-line object-property-newline |
| 41 | + http = await createServer(); |
| 42 | + |
| 43 | + // HTTPS Handlers |
38 | 44 |
|
39 | 45 | https.on('/', (req, res) => { |
40 | 46 | res.end('https'); |
41 | 47 | }); |
42 | 48 |
|
43 | | - http = await createServer(); |
| 49 | + https.on('/httpsToHttp', (req, res) => { |
| 50 | + res.writeHead(302, { |
| 51 | + location: http.url |
| 52 | + }); |
| 53 | + res.end(); |
| 54 | + }); |
| 55 | + |
| 56 | + // HTTP Handlers |
44 | 57 |
|
45 | 58 | http.on('/', (req, res) => { |
46 | 59 | res.end('reached'); |
@@ -173,6 +186,29 @@ test('redirects from http to https works', async t => { |
173 | 186 | t.truthy((await got(`${http.url}/httpToHttps`, {rejectUnauthorized: false})).body); |
174 | 187 | }); |
175 | 188 |
|
| 189 | +test('redirects from https to http works', async t => { |
| 190 | + t.truthy((await got(`${https.url}/httpsToHttp`, {rejectUnauthorized: false})).body); |
| 191 | +}); |
| 192 | + |
| 193 | +test('redirects from https to http works with an agent object', async t => { |
| 194 | + const httpAgent = new HttpAgent({keepAlive: true}); |
| 195 | + const httpsAgent = new HttpsAgent({keepAlive: true}); |
| 196 | + const httpSpy = sinon.spy(httpAgent, 'addRequest'); |
| 197 | + const httpsSpy = sinon.spy(httpsAgent, 'addRequest'); |
| 198 | + t.truthy((await got(`${https.url}/httpsToHttp`, { |
| 199 | + rejectUnauthorized: false, |
| 200 | + agent: { |
| 201 | + http: httpAgent, |
| 202 | + https: httpsAgent |
| 203 | + } |
| 204 | + })).body); |
| 205 | + t.true(httpSpy.calledOnce); |
| 206 | + t.true(httpsSpy.calledOnce); |
| 207 | + // Make sure to close all open sockets |
| 208 | + httpAgent.destroy(); |
| 209 | + httpsAgent.destroy(); |
| 210 | +}); |
| 211 | + |
176 | 212 | test('redirects works with lowercase method', async t => { |
177 | 213 | const body = (await got(`${http.url}/relative`, {method: 'head'})).body; |
178 | 214 | t.is(body, ''); |
|
0 commit comments