2016-11-10 01:15:27 +00:00
|
|
|
import * as models from '../models';
|
2016-11-19 07:11:10 +00:00
|
|
|
import {getRenderedRequest} from './render';
|
|
|
|
import {jarFromCookies} from './cookies';
|
|
|
|
import * as util from './misc';
|
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) {
|
|
|
|
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,
|
2016-10-02 20:57:00 +00:00
|
|
|
postData: {text: renderedRequest.body},
|
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
|
|
|
}));
|
|
|
|
}
|