2016-11-22 22:26:52 +00:00
|
|
|
import fs from 'fs';
|
2016-11-10 01:15:27 +00:00
|
|
|
import * as models from '../models';
|
2016-11-22 19:42:10 +00:00
|
|
|
import {getRenderedRequest} from './render';
|
2016-11-19 07:11:10 +00:00
|
|
|
import {jarFromCookies} from './cookies';
|
|
|
|
import * as util from './misc';
|
2016-11-22 19:42:10 +00:00
|
|
|
import * as misc from './misc';
|
2016-11-22 22:26:52 +00:00
|
|
|
import {CONTENT_TYPE_FILE} from './constants';
|
|
|
|
import {newBodyRaw} from '../models/request';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function exportHarWithRequest (renderedRequest, addContentLength = false) {
|
2016-09-24 03:06:38 +00:00
|
|
|
if (addContentLength) {
|
2016-11-22 19:42:10 +00:00
|
|
|
const hasContentLengthHeader = misc.filterHeaders(
|
|
|
|
renderedRequest.headers,
|
|
|
|
'content-length'
|
|
|
|
).length > 0;
|
2016-08-15 22:31:30 +00:00
|
|
|
|
2016-09-24 03:06:38 +00:00
|
|
|
if (!hasContentLengthHeader) {
|
|
|
|
const name = 'content-length';
|
2016-11-22 19:42:10 +00:00
|
|
|
const value = Buffer.byteLength(body).toString();
|
2016-09-24 03:06:38 +00:00
|
|
|
renderedRequest.headers.push({name, value})
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 22:31:30 +00:00
|
|
|
|
2016-11-22 22:26:52 +00:00
|
|
|
let postData = '';
|
|
|
|
if (renderedRequest.body.fileName) {
|
|
|
|
try {
|
|
|
|
postData = newBodyRaw(fs.readFileSync(renderedRequest.body.fileName, 'base64'));
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('[code gen] Failed to read file', e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For every other type, Insomnia uses the same body format as HAR
|
|
|
|
postData = renderedRequest.body;
|
|
|
|
}
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2016-09-24 03:06:38 +00:00
|
|
|
return {
|
|
|
|
method: renderedRequest.method,
|
|
|
|
url: util.prepareUrlForSending(renderedRequest.url),
|
|
|
|
httpVersion: 'HTTP/1.1',
|
|
|
|
cookies: getCookies(renderedRequest),
|
|
|
|
headers: renderedRequest.headers,
|
|
|
|
queryString: renderedRequest.parameters,
|
2016-11-22 19:42:10 +00:00
|
|
|
postData: postData,
|
2016-09-24 03:06:38 +00:00
|
|
|
headersSize: -1,
|
|
|
|
bodySize: -1
|
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-08-15 22:31:30 +00:00
|
|
|
|
2016-11-17 18:45:54 +00:00
|
|
|
export async function exportHar (requestId, environmentId, addContentLength = false) {
|
2016-11-10 01:15:27 +00:00
|
|
|
const request = await models.request.getById(requestId);
|
2016-11-17 18:45:54 +00:00
|
|
|
const renderedRequest = await getRenderedRequest(request, environmentId);
|
2016-10-02 20:57:00 +00:00
|
|
|
return exportHarWithRequest(renderedRequest, addContentLength);
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
function getCookies (renderedRequest) {
|
|
|
|
const jar = jarFromCookies(renderedRequest.cookieJar.cookies);
|
2016-10-05 15:07:49 +00:00
|
|
|
const domainCookies = jar.getCookiesSync(renderedRequest.url);
|
2016-10-05 16:34:13 +00:00
|
|
|
return domainCookies.map(c => Object.assign(c.toJSON(), {
|
|
|
|
name: c.key
|
2016-08-15 17:04:36 +00:00
|
|
|
}));
|
|
|
|
}
|