2016-10-02 20:57:00 +00:00
|
|
|
import * as networkUtils from '../network';
|
2017-03-16 17:51:56 +00:00
|
|
|
import * as db from '../../common/database';
|
2016-10-02 20:57:00 +00:00
|
|
|
import nock from 'nock';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {resolve as pathResolve, join as pathJoin} from 'path';
|
2017-03-16 17:51:56 +00:00
|
|
|
import {getRenderedRequest} from '../../common/render';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../models';
|
2017-03-16 17:51:56 +00:00
|
|
|
import {CONTENT_TYPE_FORM_URLENCODED, getAppVersion, CONTENT_TYPE_FILE, CONTENT_TYPE_FORM_DATA} from '../../common/constants';
|
2016-09-08 06:54:35 +00:00
|
|
|
|
|
|
|
describe('buildRequestConfig()', () => {
|
2016-11-16 17:18:39 +00:00
|
|
|
beforeEach(() => db.init(models.types(), {inMemoryOnly: true}, true));
|
2016-09-08 06:54:35 +00:00
|
|
|
|
2016-10-02 20:57:00 +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(), {
|
2016-10-02 20:57:00 +00:00
|
|
|
parentId: workspace._id
|
|
|
|
});
|
2016-09-08 06:54:35 +00:00
|
|
|
|
2016-10-02 20:57:00 +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,
|
2016-10-02 20:57:00 +00:00
|
|
|
followAllRedirects: true,
|
2016-10-11 17:40:17 +00:00
|
|
|
followRedirect: true,
|
2016-10-11 16:57:06 +00:00
|
|
|
forever: true,
|
2016-10-02 20:57:00 +00:00
|
|
|
gzip: true,
|
2016-12-08 23:29:45 +00:00
|
|
|
headers: {
|
|
|
|
'Accept': '*/*',
|
|
|
|
'User-Agent': `insomnia/${getAppVersion()}`
|
|
|
|
},
|
2016-11-22 19:42:10 +00:00
|
|
|
maxRedirects: 50,
|
2016-10-02 20:57:00 +00:00
|
|
|
method: 'GET',
|
|
|
|
proxy: null,
|
|
|
|
rejectUnauthorized: true,
|
|
|
|
time: true,
|
|
|
|
timeout: 0,
|
2017-03-06 03:00:43 +00:00
|
|
|
url: ''
|
2016-10-02 20:57:00 +00:00
|
|
|
});
|
2016-09-08 06:54:35 +00:00
|
|
|
});
|
|
|
|
|
2016-10-02 20:57:00 +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(), {
|
2016-10-02 20:57:00 +00:00
|
|
|
parentId: workspace._id,
|
2016-11-22 19:42:10 +00:00
|
|
|
headers: [
|
|
|
|
{name: 'Content-Type', value: 'application/json', disabled: false},
|
|
|
|
{name: 'hi', value: 'there', disabled: true},
|
2017-03-03 20:09:08 +00:00
|
|
|
{name: 'x-hello', value: 'world'}
|
2016-11-22 19:42:10 +00:00
|
|
|
],
|
|
|
|
parameters: [
|
|
|
|
{name: 'foo bar', value: 'hello&world', disabled: false},
|
|
|
|
{name: 'b', value: 'bb&world', disabled: true},
|
2017-03-03 20:09:08 +00:00
|
|
|
{name: 'a', value: 'aa'}
|
2016-11-22 19:42:10 +00:00
|
|
|
],
|
2016-10-02 20:57:00 +00:00
|
|
|
method: 'POST',
|
2016-11-22 19:42:10 +00:00
|
|
|
body: {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_URLENCODED,
|
|
|
|
params: [
|
|
|
|
{name: 'X', value: 'XX', disabled: false},
|
|
|
|
{name: 'Y', value: 'YY', disabled: true},
|
2017-03-03 20:09:08 +00:00
|
|
|
{name: 'Z', value: 'ZZ'}
|
2016-11-22 19:42:10 +00:00
|
|
|
]
|
|
|
|
},
|
2016-10-11 16:57:06 +00:00
|
|
|
url: 'http://foo.com:3332/★/hi@gmail.com/foo%20bar?bar=baz',
|
2016-10-02 20:57:00 +00:00
|
|
|
authentication: {
|
2016-11-22 19:42:10 +00:00
|
|
|
disabled: false,
|
2016-10-02 20:57:00 +00:00
|
|
|
username: 'user',
|
|
|
|
password: 'pass'
|
|
|
|
}
|
|
|
|
});
|
2016-09-08 06:54:35 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
const renderedRequest = await getRenderedRequest(request);
|
|
|
|
const config = networkUtils._buildRequestConfig(renderedRequest);
|
|
|
|
expect(config).toEqual({
|
2016-11-22 19:42:10 +00:00
|
|
|
body: 'X=XX&Z=ZZ',
|
2016-10-26 23:22:15 +00:00
|
|
|
encoding: null,
|
2016-10-02 20:57:00 +00:00
|
|
|
followAllRedirects: true,
|
2016-10-11 17:40:17 +00:00
|
|
|
followRedirect: true,
|
2016-10-11 16:57:06 +00:00
|
|
|
forever: true,
|
2016-10-02 20:57:00 +00:00
|
|
|
gzip: true,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Basic dXNlcjpwYXNz',
|
2016-12-08 23:29:45 +00:00
|
|
|
'Accept': '*/*',
|
|
|
|
'User-Agent': `insomnia/${getAppVersion()}`,
|
2016-11-22 19:42:10 +00:00
|
|
|
'x-hello': 'world'
|
2016-10-02 20:57:00 +00:00
|
|
|
},
|
2016-11-22 19:42:10 +00:00
|
|
|
maxRedirects: 50,
|
2016-10-02 20:57:00 +00:00
|
|
|
method: 'POST',
|
|
|
|
proxy: null,
|
|
|
|
rejectUnauthorized: true,
|
|
|
|
time: true,
|
|
|
|
timeout: 0,
|
2016-11-22 19:42:10 +00:00
|
|
|
url: 'http://foo.com:3332/%E2%98%85/hi@gmail.com/foo%20bar?bar=baz&foo%20bar=hello%26world&a=aa'
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
|
|
|
});
|
2016-09-08 06:54:35 +00:00
|
|
|
});
|
2016-09-08 20:07:19 +00:00
|
|
|
|
|
|
|
describe('actuallySend()', () => {
|
2016-11-16 17:18:39 +00:00
|
|
|
beforeEach(() => db.init(models.types(), {inMemoryOnly: true}, true));
|
2016-09-08 20:07:19 +00:00
|
|
|
|
2016-10-02 20:57:00 +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();
|
2016-10-05 05:10:48 +00:00
|
|
|
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')
|
|
|
|
}, {
|
2016-10-05 05:10:48 +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: '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({
|
2016-10-05 05:10:48 +00:00
|
|
|
parentId: workspace._id,
|
|
|
|
cookies
|
|
|
|
});
|
2016-09-08 20:07:19 +00:00
|
|
|
|
2016-12-08 18:19:18 +00:00
|
|
|
mock = nock('http://localhost')
|
2016-10-02 20:57:00 +00:00
|
|
|
.matchHeader('Content-Type', 'application/json')
|
|
|
|
.matchHeader('Authorization', 'Basic dXNlcjpwYXNz')
|
2016-12-08 23:29:45 +00:00
|
|
|
.matchHeader('Accept', '*/*')
|
|
|
|
.matchHeader('User-Agent', `insomnia/${getAppVersion()}`)
|
2016-10-05 16:34:13 +00:00
|
|
|
.matchHeader('Cookie', 'foo=barrrrr')
|
2016-11-23 19:33:24 +00:00
|
|
|
.post('/', 'foo=bar')
|
2016-10-02 20:57:00 +00:00
|
|
|
.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(), {
|
2016-10-02 20:57:00 +00:00
|
|
|
_id: 'req_123',
|
|
|
|
parentId: workspace._id,
|
|
|
|
headers: [{name: 'Content-Type', value: 'application/json'}],
|
|
|
|
parameters: [{name: 'foo bar', value: 'hello&world'}],
|
|
|
|
method: 'POST',
|
2016-11-22 19:42:10 +00:00
|
|
|
body: {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_URLENCODED,
|
2016-11-23 19:33:24 +00:00
|
|
|
params: [{name: 'foo', value: 'bar'}]
|
2016-11-22 19:42:10 +00:00
|
|
|
},
|
2016-10-02 20:57:00 +00:00
|
|
|
url: 'http://localhost',
|
|
|
|
authentication: {
|
|
|
|
username: 'user',
|
|
|
|
password: 'pass'
|
|
|
|
}
|
2016-09-08 20:07:19 +00:00
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
|
|
|
|
const renderedRequest = await getRenderedRequest(request);
|
2016-11-29 20:55:31 +00:00
|
|
|
const response = await networkUtils._actuallySend(
|
|
|
|
renderedRequest,
|
|
|
|
workspace,
|
|
|
|
settings
|
|
|
|
);
|
2016-11-23 19:33:24 +00:00
|
|
|
|
2016-12-08 18:19:18 +00:00
|
|
|
expect(mock.basePath).toBe('http://localhost:80');
|
2017-02-01 20:21:14 +00:00
|
|
|
expect(response.error).toBe(undefined);
|
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'));
|
2016-10-02 20:57:00 +00:00
|
|
|
expect(response.statusCode).toBe(200);
|
2016-09-08 20:07:19 +00:00
|
|
|
});
|
2016-11-23 19:33:24 +00:00
|
|
|
|
|
|
|
it('sends a file', async () => {
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
const workspace = await models.workspace.create();
|
|
|
|
const settings = await models.settings.create();
|
|
|
|
await models.cookieJar.create({parentId: workspace._id});
|
|
|
|
|
2016-12-08 18:19:18 +00:00
|
|
|
mock = nock('http://localhost')
|
2016-11-23 19:33:24 +00:00
|
|
|
.matchHeader('Content-Type', 'application/octet-stream')
|
2016-12-08 23:29:45 +00:00
|
|
|
.matchHeader('Accept', '*/*')
|
|
|
|
.matchHeader('User-Agent', `insomnia/${getAppVersion()}`)
|
2016-11-23 19:33:24 +00:00
|
|
|
.post('/', 'Hello World!')
|
|
|
|
.reply(200, 'response body')
|
|
|
|
.log(console.log);
|
|
|
|
|
|
|
|
const request = Object.assign(models.request.init(), {
|
|
|
|
_id: 'req_123',
|
|
|
|
parentId: workspace._id,
|
|
|
|
headers: [{name: 'Content-Type', value: 'application/octet-stream'}],
|
|
|
|
url: 'http://localhost',
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
mimeType: CONTENT_TYPE_FILE,
|
|
|
|
fileName: pathResolve(pathJoin(__dirname, './testfile.txt')) // Let's send ourselves
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const renderedRequest = await getRenderedRequest(request);
|
2016-11-29 20:55:31 +00:00
|
|
|
const response = await networkUtils._actuallySend(
|
|
|
|
renderedRequest,
|
|
|
|
workspace,
|
|
|
|
settings
|
|
|
|
);
|
2016-11-23 19:33:24 +00:00
|
|
|
|
2016-12-08 18:19:18 +00:00
|
|
|
expect(mock.basePath).toBe('http://localhost:80');
|
2017-02-01 20:21:14 +00:00
|
|
|
expect(response.error).toBe(undefined);
|
2016-11-23 19:33:24 +00:00
|
|
|
expect(response.url).toBe('http://localhost/');
|
|
|
|
expect(response.body).toBe(new Buffer('response body').toString('base64'));
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends multipart form data', async () => {
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
const workspace = await models.workspace.create();
|
|
|
|
const settings = await models.settings.create();
|
|
|
|
await models.cookieJar.create({parentId: workspace._id});
|
|
|
|
const fileName = pathResolve(pathJoin(__dirname, './testfile.txt'));
|
|
|
|
let requestBody = 'n/a';
|
2016-12-08 18:19:18 +00:00
|
|
|
mock = nock('http://localhost')
|
2016-11-23 19:33:24 +00:00
|
|
|
.matchHeader('Content-Type', /^multipart\/form-data/)
|
2016-12-08 23:29:45 +00:00
|
|
|
.matchHeader('Accept', '*/*')
|
|
|
|
.matchHeader('User-Agent', `insomnia/${getAppVersion()}`)
|
2016-11-23 19:33:24 +00:00
|
|
|
.post('/', body => {
|
|
|
|
requestBody = body;
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.reply(200, 'response body')
|
|
|
|
.log(console.log);
|
|
|
|
|
|
|
|
const request = Object.assign(models.request.init(), {
|
|
|
|
_id: 'req_123',
|
|
|
|
parentId: workspace._id,
|
|
|
|
headers: [{name: 'Content-Type', value: 'multipart/form-data'}],
|
|
|
|
url: 'http://localhost',
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
mimeType: CONTENT_TYPE_FORM_DATA,
|
|
|
|
params: [
|
|
|
|
// Should ignore value and send the file since type is set to file
|
|
|
|
{name: 'foo', fileName: fileName, value: 'bar', type: 'file'},
|
|
|
|
|
|
|
|
// Some extra params
|
|
|
|
{name: 'a', value: 'AA'},
|
2017-03-03 20:09:08 +00:00
|
|
|
{name: 'baz', value: 'qux', disabled: true}
|
2016-11-23 19:33:24 +00:00
|
|
|
]
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-11-23 19:33:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const renderedRequest = await getRenderedRequest(request);
|
2016-11-29 20:55:31 +00:00
|
|
|
const response = await networkUtils._actuallySend(
|
|
|
|
renderedRequest,
|
|
|
|
workspace,
|
|
|
|
settings
|
|
|
|
);
|
2016-11-23 19:33:24 +00:00
|
|
|
|
2016-12-08 18:19:18 +00:00
|
|
|
expect(mock.basePath).toBe('http://localhost:80');
|
2017-02-01 20:21:14 +00:00
|
|
|
expect(response.error).toBe(undefined);
|
2016-11-23 19:33:24 +00:00
|
|
|
expect(response.url).toBe('http://localhost/');
|
|
|
|
expect(response.body).toBe(new Buffer('response body').toString('base64'));
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
|
|
|
|
const lines = requestBody.split(/\r\n/);
|
|
|
|
expect(lines[0]).toMatch(/^----------------------------\d{24}/);
|
2016-12-30 23:06:27 +00:00
|
|
|
expect(lines[1]).toBe('Content-Disposition: form-data; name="foo"; filename="testfile.txt"');
|
2016-11-23 19:33:24 +00:00
|
|
|
expect(lines[2]).toBe('Content-Type: text/plain');
|
|
|
|
expect(lines[3]).toBe('');
|
|
|
|
expect(lines[4]).toBe('Hello World!\n');
|
|
|
|
expect(lines[5]).toMatch(/^----------------------------\d{24}/);
|
|
|
|
expect(lines[6]).toBe('Content-Disposition: form-data; name="a"');
|
|
|
|
expect(lines[7]).toBe('');
|
|
|
|
expect(lines[8]).toBe('AA');
|
|
|
|
expect(lines[9]).toMatch(/^----------------------------\d{24}--/);
|
|
|
|
expect(lines[10]).toBe('');
|
2017-03-16 17:51:56 +00:00
|
|
|
expect(lines.length).toBe(11);
|
2016-11-23 19:33:24 +00:00
|
|
|
});
|
2016-09-08 20:07:19 +00:00
|
|
|
});
|