mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 06:39:48 +00:00
Alternate send request callback for app to use (#2334)
This commit is contained in:
parent
9440bfe098
commit
bab945a6a1
@ -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,
|
||||
};
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
export { getSendRequestCallback } from '../app/common/send-request';
|
||||
export { getSendRequestCallbackMemDb, getSendRequestCallback } from '../app/common/send-request';
|
||||
|
@ -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>,
|
||||
|
@ -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 });
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user