Alternate send request callback for app to use (#2334)

This commit is contained in:
Gregory Schier 2020-06-30 15:40:15 -07:00 committed by GitHub
parent 9440bfe098
commit bab945a6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 21 deletions

View File

@ -3,8 +3,8 @@ import { types as modelTypes } from '../models';
import { send } from '../network/network';
import { getBodyBuffer } from '../models/response';
export async function getSendRequestCallback(environmentId, memDB) {
// Initialize the DB in-memory and fill it with data
export async function getSendRequestCallbackMemDb(environmentId, memDB) {
// Initialize the DB in-memory and fill it with data if we're given one
await db.init(modelTypes(), { inMemoryOnly: true }, true);
const docs = [];
@ -18,20 +18,30 @@ export async function getSendRequestCallback(environmentId, memDB) {
// Return callback helper to send requests
return async function sendRequest(requestId) {
const res = await send(requestId, environmentId);
const headersObj = {};
for (const h of res.headers || []) {
const name = h.name || '';
headersObj[name.toLowerCase()] = h.value || '';
}
const bodyBuffer = await getBodyBuffer(res);
return {
status: res.statusCode,
statusMessage: res.statusMessage,
data: bodyBuffer ? bodyBuffer.toString('utf8') : undefined,
headers: headersObj,
};
return sendAndTransform(requestId, environmentId);
};
}
export function getSendRequestCallback(environmentId) {
return async function sendRequest(requestId) {
return sendAndTransform(requestId, environmentId);
};
}
async function sendAndTransform(requestId, environmentId) {
const res = await send(requestId, environmentId);
const headersObj = {};
for (const h of res.headers || []) {
const name = h.name || '';
headersObj[name.toLowerCase()] = h.value || '';
}
const bodyBuffer = await getBodyBuffer(res);
return {
status: res.statusCode,
statusMessage: res.statusMessage,
data: bodyBuffer ? bodyBuffer.toString('utf8') : undefined,
headers: headersObj,
};
}

View File

@ -1 +1 @@
export { getSendRequestCallback } from '../app/common/send-request';
export { getSendRequestCallbackMemDb, getSendRequestCallback } from '../app/common/send-request';

View File

@ -2,7 +2,8 @@
declare module 'insomnia-send-request' {
declare module.exports: {
getSendRequestCallback: (
getSendRequestCallback: ( environmentId: string ) => Promise<Object>,
getSendRequestCallbackMemDb: (
environmentId: string,
memDb: { [string]: Array<Object> },
) => Promise<Object>,

View File

@ -1,7 +1,7 @@
// @flow
import { generate, runTestsCli } from 'insomnia-testing';
import { getSendRequestCallback } from 'insomnia-send-request';
import { getSendRequestCallbackMemDb } from 'insomnia-send-request';
import type { GlobalOptions } from '../util';
import { loadDb } from '../db';
@ -63,6 +63,6 @@ export async function runInsomniaTests(options: RunTestsOptions): Promise<boolea
const environmentId = 'env_env_ca046a738f001eb3090261a537b1b78f86c2094c_sub';
const testFileContents = await generate(suites);
const sendRequest = await getSendRequestCallback(environmentId, db);
const sendRequest = await getSendRequestCallbackMemDb(environmentId, db);
return await runTestsCli(testFileContents, { reporter, bail, keepFile, sendRequest });
}