insomnia/app/common/__tests__/network.test.js
Gregory Schier 5d5d6eecc4 Switch Request body to be object instead of string (#47)
* Basic import summary

* Started (broken)
2016-11-22 11:42:10 -08:00

161 lines
4.8 KiB
JavaScript

import * as networkUtils from '../network';
import * as db from '../database';
import nock from 'nock';
import {getRenderedRequest} from '../render';
import * as models from '../../models';
import {CONTENT_TYPE_FORM_URLENCODED} from '../constants';
describe('buildRequestConfig()', () => {
beforeEach(() => db.init(models.types(), {inMemoryOnly: true}, true));
it('builds a default config', async () => {
const workspace = await models.workspace.create();
const request = Object.assign(models.request.init(), {
parentId: workspace._id
});
const renderedRequest = await getRenderedRequest(request);
const config = networkUtils._buildRequestConfig(renderedRequest);
expect(config).toEqual({
body: '',
encoding: null,
followAllRedirects: true,
followRedirect: true,
forever: true,
gzip: true,
headers: {host: ''},
maxRedirects: 50,
method: 'GET',
proxy: null,
rejectUnauthorized: true,
time: true,
timeout: 0,
url: 'http://'
});
});
it('builds a complex config', async () => {
const workspace = await models.workspace.create();
const request = Object.assign(models.request.init(), {
parentId: workspace._id,
headers: [
{name: 'Content-Type', value: 'application/json', disabled: false},
{name: 'hi', value: 'there', disabled: true},
{name: 'x-hello', value: 'world'},
],
parameters: [
{name: 'foo bar', value: 'hello&world', disabled: false},
{name: 'b', value: 'bb&world', disabled: true},
{name: 'a', value: 'aa'},
],
method: 'POST',
body: {
mimeType: CONTENT_TYPE_FORM_URLENCODED,
params: [
{name: 'X', value: 'XX', disabled: false},
{name: 'Y', value: 'YY', disabled: true},
{name: 'Z', value: 'ZZ'},
]
},
url: 'http://foo.com:3332/★/hi@gmail.com/foo%20bar?bar=baz',
authentication: {
disabled: false,
username: 'user',
password: 'pass'
}
});
const renderedRequest = await getRenderedRequest(request);
const config = networkUtils._buildRequestConfig(renderedRequest);
expect(config).toEqual({
body: 'X=XX&Z=ZZ',
encoding: null,
followAllRedirects: true,
followRedirect: true,
forever: true,
gzip: true,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic dXNlcjpwYXNz',
'host': 'foo.com:3332',
'x-hello': 'world'
},
maxRedirects: 50,
method: 'POST',
proxy: null,
rejectUnauthorized: true,
time: true,
timeout: 0,
url: 'http://foo.com:3332/%E2%98%85/hi@gmail.com/foo%20bar?bar=baz&foo%20bar=hello%26world&a=aa'
})
})
});
describe('actuallySend()', () => {
beforeEach(() => db.init(models.types(), {inMemoryOnly: true}, true));
it('does something', async () => {
let mock;
const workspace = await models.workspace.create();
const settings = await models.settings.create();
const cookies = [{
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')
}];
await models.cookieJar.create({
parentId: workspace._id,
cookies
});
mock = nock('http://[::1]:80')
.matchHeader('Content-Type', 'application/json')
.matchHeader('Authorization', 'Basic dXNlcjpwYXNz')
.matchHeader('Cookie', 'foo=barrrrr')
.post('/')
.query({'foo bar': '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/json'}],
parameters: [{name: 'foo bar', value: 'hello&world'}],
method: 'POST',
body: {
mimeType: CONTENT_TYPE_FORM_URLENCODED,
text: 'foo=bar'
},
url: 'http://localhost',
authentication: {
username: 'user',
password: 'pass'
}
});
const renderedRequest = await getRenderedRequest(request);
const response = await networkUtils._actuallySend(renderedRequest, settings);
expect(mock.basePath).toBe('http://::1:80');
expect(response.url).toBe('http://localhost/?foo%20bar=hello%26world');
expect(response.body).toBe(new Buffer('response body').toString('base64'));
expect(response.statusCode).toBe(200);
});
});