insomnia/packages/insomnia-httpsnippet/test/requests.js

86 lines
2.2 KiB
JavaScript
Raw Normal View History

/* global describe, it */
2018-06-25 17:42:50 +00:00
'use strict';
2018-06-25 17:42:50 +00:00
var fixtures = require('./fixtures');
var HTTPSnippet = require('../src');
var targets = require('../src/targets');
var shell = require('child_process');
var util = require('util');
2018-06-25 17:42:50 +00:00
require('should');
2018-06-25 17:42:50 +00:00
var base = './test/fixtures/output/';
var requests = [
'application-form-encoded',
'application-json',
'cookies',
'custom-method',
'headers',
'https',
'multipart-data',
'multipart-form-data',
'short'
2018-06-25 17:42:50 +00:00
];
// test all the things!
2018-06-25 17:42:50 +00:00
fixtures.cli.forEach(function(cli) {
describe(targets[cli.target].info.title + ' Request Validation', function() {
cli.clients.forEach(function(client) {
requests.forEach(function(request) {
it(client + ' request should match mock for ' + request, function(
done
) {
var stdout = '';
var fixture =
cli.target +
'/' +
client +
'/' +
request +
HTTPSnippet.extname(cli.target);
var command = util.format(cli.run, base + fixture);
2018-06-25 17:42:50 +00:00
var ls = shell.exec(command);
2018-06-25 17:42:50 +00:00
ls.stdout.on('data', function(data) {
stdout += data;
});
2018-06-25 17:42:50 +00:00
ls.on('exit', function(code) {
try {
2018-06-25 17:42:50 +00:00
var har = JSON.parse(stdout);
} catch (err) {
2018-06-25 17:42:50 +00:00
err.should.be.null;
}
// make an exception for multipart/form-data
if (fixtures.requests[request].headers) {
2018-06-25 17:42:50 +00:00
fixtures.requests[request].headers.forEach(function(
header,
index
) {
if (
header.name === 'content-type' &&
header.value === 'multipart/form-data'
) {
delete fixtures.requests[request].headers[index];
}
2018-06-25 17:42:50 +00:00
});
}
2018-06-25 17:42:50 +00:00
har.should.have.property('log');
har.log.should.have.property('entries').and.be.Array;
har.log.entries[0].should.have.property('request');
har.log.entries[0].request.should.containDeep(
fixtures.requests[request]
);
2018-06-25 17:42:50 +00:00
done();
});
});
});
});
});
});