Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integration tests #415

Merged
merged 7 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/http/test/helpers/certs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 0 additions & 41 deletions packages/http/test/helpers/server.js

This file was deleted.

143 changes: 143 additions & 0 deletions packages/http/test/integration.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
40 changes: 0 additions & 40 deletions packages/http/test/intergration.js

This file was deleted.