insomnia/app/common/har.js

45 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-11-10 01:15:27 +00:00
import * as models from '../models';
import {getRenderedRequest} from './render';
import {jarFromCookies} from './cookies';
import * as util from './misc';
export function exportHarWithRequest (renderedRequest, addContentLength = false) {
2016-09-24 03:06:38 +00:00
if (addContentLength) {
const hasContentLengthHeader = !!renderedRequest.headers.find(
h => h.name.toLowerCase() === 'content-length'
);
2016-08-15 22:31:30 +00:00
2016-09-24 03:06:38 +00:00
if (!hasContentLengthHeader) {
const name = 'content-length';
const value = Buffer.byteLength(renderedRequest.body).toString();
renderedRequest.headers.push({name, value})
}
}
2016-08-15 22:31:30 +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,
postData: {text: renderedRequest.body},
2016-09-24 03:06:38 +00:00
headersSize: -1,
bodySize: -1
};
}
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);
return exportHarWithRequest(renderedRequest, addContentLength);
}
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
}));
}