insomnia/app/backend/__tests__/network.test.js

141 lines
4.2 KiB
JavaScript
Raw Normal View History

import * as networkUtils from '../network';
import * as db from '../database';
import nock from 'nock';
import {getRenderedRequest} from '../render';
2016-11-10 01:15:27 +00:00
import * as models from '../models';
2016-09-08 06:54:35 +00:00
describe('buildRequestConfig()', () => {
2016-11-10 01:15:27 +00:00
beforeEach(() => db.initDB(models.types(), {inMemoryOnly: true}, true));
2016-09-08 06:54:35 +00:00
it('builds a default config', async () => {
2016-11-10 01:15:27 +00:00
const workspace = await models.workspace.create();
const request = Object.assign(models.request.init(), {
parentId: workspace._id
});
2016-09-08 06:54:35 +00:00
const renderedRequest = await getRenderedRequest(request);
const config = networkUtils._buildRequestConfig(renderedRequest);
expect(config).toEqual({
body: '',
2016-10-26 23:22:15 +00:00
encoding: null,
followAllRedirects: true,
2016-10-11 17:40:17 +00:00
followRedirect: true,
2016-10-11 16:57:06 +00:00
forever: true,
gzip: true,
headers: {host: ''},
maxRedirects: 20,
method: 'GET',
proxy: null,
rejectUnauthorized: true,
time: true,
timeout: 0,
url: 'http://'
});
2016-09-08 06:54:35 +00:00
});
it('builds a complex config', async () => {
2016-11-10 01:15:27 +00:00
const workspace = await models.workspace.create();
const request = Object.assign(models.request.init(), {
parentId: workspace._id,
headers: [{host: '', name: 'Content-Type', value: 'application/json'}],
parameters: [{name: 'foo bar', value: 'hello&world'}],
method: 'POST',
body: 'foo=bar',
2016-10-11 16:57:06 +00:00
url: 'http://foo.com:3332/★/hi@gmail.com/foo%20bar?bar=baz',
authentication: {
username: 'user',
password: 'pass'
}
});
2016-09-08 06:54:35 +00:00
const renderedRequest = await getRenderedRequest(request);
const config = networkUtils._buildRequestConfig(renderedRequest);
expect(config).toEqual({
body: 'foo=bar',
2016-10-26 23:22:15 +00:00
encoding: null,
followAllRedirects: true,
2016-10-11 17:40:17 +00:00
followRedirect: true,
2016-10-11 16:57:06 +00:00
forever: true,
gzip: true,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic dXNlcjpwYXNz',
'host': 'foo.com:3332'
},
maxRedirects: 20,
method: 'POST',
proxy: null,
rejectUnauthorized: true,
time: true,
timeout: 0,
2016-10-11 16:57:06 +00:00
url: 'http://foo.com:3332/%E2%98%85/hi%40gmail.com/' +
'foo%20bar?bar=baz&foo%20bar=hello%26world'
2016-09-08 06:54:35 +00:00
})
})
2016-09-08 06:54:35 +00:00
});
2016-09-08 20:07:19 +00:00
describe('actuallySend()', () => {
2016-11-10 01:15:27 +00:00
beforeEach(() => db.initDB(models.types(), {inMemoryOnly: true}, true));
2016-09-08 20:07:19 +00:00
it('does something', async () => {
let mock;
2016-09-08 20:07:19 +00:00
2016-11-10 01:15:27 +00:00
const workspace = await models.workspace.create();
const settings = await models.settings.create();
const cookies = [{
2016-10-05 05:11:33 +00:00
creation: new Date('2016-10-05T04:40:49.505Z'),
key: 'foo',
value: 'barrrrr',
expires: new Date('2096-10-12T04:40:49.000Z'),
domain: 'notlocalhost',
path: '/',
hostOnly: true,
lastAccessed: new Date('2096-10-05T04:40:49.505Z')
}, {
creation: new Date('2016-10-05T04:40:49.505Z'),
key: 'foo',
value: 'barrrrr',
expires: new Date('2096-10-12T04:40:49.000Z'),
domain: 'localhost',
path: '/',
hostOnly: true,
lastAccessed: new Date('2096-10-05T04:40:49.505Z')
}];
2016-11-10 01:15:27 +00:00
await models.cookieJar.create({
parentId: workspace._id,
cookies
});
2016-09-08 20:07:19 +00:00
2016-10-26 20:30:31 +00:00
mock = nock('http://[::1]:80')
.matchHeader('Content-Type', 'application/json')
.matchHeader('Authorization', 'Basic dXNlcjpwYXNz')
2016-10-05 16:34:13 +00:00
.matchHeader('Cookie', 'foo=barrrrr')
.post('/')
.query({'foo bar': 'hello&world'})
.reply(200, 'response body')
.log(console.log);
2016-09-08 20:07:19 +00:00
2016-11-10 01:15:27 +00:00
const request = Object.assign(models.request.init(), {
_id: 'req_123',
parentId: workspace._id,
headers: [{name: 'Content-Type', value: 'application/json'}],
parameters: [{name: 'foo bar', value: 'hello&world'}],
method: 'POST',
body: 'foo=bar',
url: 'http://localhost',
authentication: {
username: 'user',
password: 'pass'
}
2016-09-08 20:07:19 +00:00
});
const renderedRequest = await getRenderedRequest(request);
const response = await networkUtils._actuallySend(renderedRequest, settings);
2016-10-26 20:30:31 +00:00
expect(mock.basePath).toBe('http://::1:80');
2016-10-05 04:45:18 +00:00
expect(response.url).toBe('http://localhost/?foo%20bar=hello%26world');
2016-10-26 23:22:15 +00:00
expect(response.body).toBe(new Buffer('response body').toString('base64'));
expect(response.statusCode).toBe(200);
2016-09-08 20:07:19 +00:00
});
});