diff --git a/packages/http/test/helpers/certs.js b/packages/http/test/helpers/certs.js new file mode 100644 index 000000000..bfcc86b3d --- /dev/null +++ b/packages/http/test/helpers/certs.js @@ -0,0 +1,8 @@ +export const ca = + '-----BEGIN CERTIFICATE-----\nMIID3TCCAsWgAwIBAgIUeab489vF43tg4G4IrcI+uf+4W8swDQYJKoZIhvcNAQEL\nBQAwfjELMAkGA1UEBhMCVFoxDzANBgNVBAgMBkFydXNoYTEPMA0GA1UEBwwGQXJ1\nc2hhMQ8wDQYDVQQKDAZPcGVuRm4xCzAJBgNVBAsMAkZuMQ8wDQYDVQQDDAZPcGVu\nRm4xHjAcBgkqhkiG9w0BCQEWD2luZm9Ab3BlbmZuLm9yZzAeFw0yMzEwMTEwODAy\nNDJaFw00MzEwMDYwODAyNDJaMH4xCzAJBgNVBAYTAlRaMQ8wDQYDVQQIDAZBcnVz\naGExDzANBgNVBAcMBkFydXNoYTEPMA0GA1UECgwGT3BlbkZuMQswCQYDVQQLDAJG\nbjEPMA0GA1UEAwwGT3BlbkZuMR4wHAYJKoZIhvcNAQkBFg9pbmZvQG9wZW5mbi5v\ncmcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDZ0kbnOaWrF74YU9GY\nTGWqoEH1bwh8O8EEpVahF+qDoKOEmd8EMxAhqBi8iYbcVI4YCuaaKYpuozDkU8l/\nJwKrZ2saEeE6llt6qCmyWhkBUuB1heQ5/UfQ7RjOsWkahlhsHANwPydd7VoCKCgO\nGjdXMWp/SsoojQylTfqYFUBgPXyxp4PmsrDAPGHvEglLcqE7iv5DtP5OJHFaaIsl\nPQdiI0OjxS2oyIW7F7zELNIFF48YtYJrnLiDzB+u7EV2yUnCzz6G+e+pQFoadoiB\n/RJAhWh+BEgLjmWi+AyMidIcEp9omsfygwR0GNP6jo0TyMQ+V1p77ZhaOCSAVo29\n7lvnAgMBAAGjUzBRMB0GA1UdDgQWBBQ+JQvdzx9MKga1rMSJYCNybmNPzTAfBgNV\nH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Q8EBAMCBPAwFAYDVR0RBA0wC4IJbG9jYWxob3N0MB0GA1Ud\nDgQWBBQaYITNXu4/nRy69bBUTmUfBTwHbDANBgkqhkiG9w0BAQsFAAOCAQEAEmsG\n3lgL6/yxqn9JLoi3PA/bTsfdGEKEp5Mqh1ivqmfDiHkrb0owb46/ACySkyJl422k\nrvCyEBhOpIL7atPjD0u0s3D2kAPWI8gL3He9lvsgtqdw3fO7MR1GXehEpQq76p4e\nj3MJDUpOcolCPyiJKro66Mzin8EQ58yMblA5pv2B7Zum/+lCVpI88nlV8cdzvy2k\nfnL6CrfFOvlE2Bc49aBq4sGk4NkhEpfjInjX0hKexQautuIcAaNCg9Kc4RQoXHl7\n+qM0ICe8RKAnlewq36A38M9oqaCpEu/HWU82I4sHo6KglwCpm/aA3mJTWaHsHDbN\nY47fF2gnrQ6/qgmUmQ==\n-----END CERTIFICATE-----'; diff --git a/packages/http/test/helpers/server.js b/packages/http/test/helpers/server.js deleted file mode 100644 index 1c6ded11f..000000000 --- a/packages/http/test/helpers/server.js +++ /dev/null @@ -1,41 +0,0 @@ -import http from 'http'; -import https from 'https'; - -// Listen on port 8080 -const port = 8080; -// Create an HTTP server to listen for incoming requests -const httpServer = http.createServer((req, res) => { - // Handle the redirect logic here - switch (req.url) { - case '/redirect': - console.log('redirecting to /new-location'); - res.writeHead(301, { Location: `http://localhost:${port}/new-location` }); - res.end(); - break; - case '/new-location': - console.log('redirecting to /new-location-1'); - res.writeHead(302, { - Location: `http://localhost:${port}/new-location-1`, - }); - res.end(); - break; - case '/new-location-1': - console.log('Done redirecting'); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ url: req.url, res: 'Done redirecting!' })); - break; - default: - res.writeHead(200, { 'Content-Type': 'text/plain' }); - res.end('Hello, World!'); - break; - } -}); - -// Create an HTTPS server for handling the redirected request -const httpsServer = https.createServer((req, res) => { - // Handle the redirected request here - res.writeHead(200, { 'Content-Type': 'text/plain' }); - res.end('This is the HTTPS server handling the redirected request.'); -}); - -export { httpServer, httpsServer }; diff --git a/packages/http/test/integration.js b/packages/http/test/integration.js new file mode 100644 index 000000000..60c395203 --- /dev/null +++ b/packages/http/test/integration.js @@ -0,0 +1,143 @@ +import { expect } from 'chai'; +import http from 'http'; +import https from 'https'; + +import { execute, get, post } from '../src'; + +import { ca, key, cert } from './helpers/certs.js'; + +const port = 8080; +const httpServer = http.createServer((req, res) => { + switch (req.url) { + case '/redirect': + res.writeHead(301, { Location: `http://localhost:${port}/new-location` }); + res.end(); + break; + case '/new-location': + res.writeHead(302, { + Location: `http://localhost:${port}/new-location-1`, + }); + res.end(); + break; + case '/new-location-1': + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ url: req.url, res: 'Done redirecting!' })); + break; + default: + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('Hello, World!'); + break; + } +}); + +// Create an HTTPS server for handling the redirected request +const httpsPort = 1443; +const certOptions = { key, cert }; +const httpsServer = https.createServer(certOptions || {}, (req, res) => { + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('Hello, HTTPS World!'); +}); + +describe('Integration tests', () => { + before(() => { + httpServer.listen(port, () => { + console.log(`HTTP server is running on http://localhost:${port}/`); + }); + + httpsServer.listen(httpsPort, () => { + console.log(`HTTPS server is running on https://localhost:${httpsPort}/`); + }); + }); + + it('can make a GET request', async () => { + const state = { + configuration: { + baseUrl: `http://localhost:${httpServer.address().port}`, + }, + data: {}, + }; + const { response } = await execute(get('/'))(state); + + expect(response.body).to.eql('Hello, World!'); + expect(response.method).to.eql('GET'); + }); + + it('can make a POST request', async () => { + const state = { + configuration: { + baseUrl: `http://localhost:${httpServer.address().port}`, + }, + data: {}, + }; + const { response } = await execute( + post('/', { + body: { name: 'Joe' }, + }) + )(state); + + expect(response.body).to.eql('Hello, World!'); + expect(response.method).to.eql('POST'); + }); + + it('can follow redirects', async () => { + const state = { + configuration: { + baseUrl: `http://localhost:${httpServer.address().port}`, + }, + data: {}, + }; + + const { response } = await execute( + get('/redirect', { + headers: { followAllRedirects: true }, + }) + )(state); + + expect(response.url).to.eql('/new-location-1'); + }); + + it('should pass if certs are added to the request', async () => { + const state = { + configuration: { + baseUrl: `https://localhost:${httpsServer.address().port}`, + ca, + }, + data: {}, + }; + + const { response } = await execute( + get('/', { + tls: { + ca, + requestCert: false, + rejectUnauthorized: true, + }, + }) + )(state); + + expect(response.body).to.eql('Hello, HTTPS World!'); + }); + + it('should fail if certs are not added to the request', async () => { + const state = { + configuration: { + // Important!! + // We have to use a different domain here to generate a fresh + // unidici client inside the adatapor + // Otherwise it'll just re-use the credentials from the previous calls + baseUrl: `https://127.0.0.1:${httpsServer.address().port}`, + }, + data: {}, + }; + const error = await execute(get('/no-certs'))(state).catch(e => e); + + expect(error.message).to.eq('unable to verify the first certificate'); + + httpsServer.close(); + }); + + after(() => { + httpServer.close(); + httpsServer.close(); + }); +}); diff --git a/packages/http/test/intergration.js b/packages/http/test/intergration.js deleted file mode 100644 index 987584d44..000000000 --- a/packages/http/test/intergration.js +++ /dev/null @@ -1,40 +0,0 @@ -import { execute, get } from '../src'; -import { expect } from 'chai'; - -import { httpServer, httpsServer } from './helpers/server'; - -describe('Integration tests', () => { - before(() => { - const port = 8080; - httpServer.listen(port, () => { - console.log(`HTTP server is running on http://localhost:${port}/`); - }); - // Listen on port 443 for HTTPS - const httpsPort = 443; - httpsServer.listen(httpsPort, () => { - console.log(`HTTPS server is running on https://localhost:${httpsPort}/`); - }); - }); - - it('can follow redirects', async () => { - const state = { - configuration: { - baseUrl: `http://localhost:${httpServer.address().port}`, - }, - data: {}, - }; - - const { response } = await execute( - get('/redirect', { - headers: { followAllRedirects: true }, - }) - )(state); - - expect(response.url).to.eql('/new-location-1'); - }); - - after(() => { - httpServer.close(); - httpsServer.close(); - }); -});