MCPcopy
hub / github.com/promptfoo/promptfoo / createMockResponse

Function createMockResponse

test/util/utils.ts:41–73  ·  view source on GitHub ↗
(
  options: {
    ok?: boolean;
    body?: any;
    statusText?: string;
    status?: number;
    headers?: Headers;
    text?: () => Promise<string>;
    json?: () => Promise<any>;
  } = { ok: true },
)

Source from the content-addressed store, hash-verified

39}
40
41export function createMockResponse(
42 options: {
43 ok?: boolean;
44 body?: any;
45 statusText?: string;
46 status?: number;
47 headers?: Headers;
48 text?: () => Promise<string>;
49 json?: () => Promise<any>;
50 } = { ok: true },
51): Response {
52 const isOk = options.ok ?? (options.status ? options.status < 400 : true);
53 const mockResponse: Response = {
54 ok: isOk,
55 status: options.status || (isOk ? 200 : 400),
56 statusText: options.statusText || (isOk ? 'OK' : 'Bad Request'),
57 headers: options.headers || new Headers(),
58 redirected: false,
59 type: 'basic',
60 url: 'https://example.com',
61 json: options.json || (() => Promise.resolve(options.body || {})),
62 text: options.text || (() => Promise.resolve('')),
63 blob: () => Promise.resolve(new Blob([])),
64 arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
65 formData: () => Promise.resolve(new FormData()),
66 bodyUsed: false,
67 body: null,
68 clone() {
69 return createMockResponse(options);
70 },
71 } as Response;
72 return mockResponse;
73}
74
75export function stripAnsi(value: string): string {
76 return value.replace(/\u001b\[[0-9;]*m/g, '');

Callers 10

fetch.test.tsFile · 0.90
rateLimitedJsonResponseFunction · 0.90
feedback.test.tsFile · 0.90
init.test.tsFile · 0.90
auth.test.tsFile · 0.90
mockRemoteGenerationFunction · 0.90
cloneFunction · 0.70

Calls 1

resolveMethod · 0.80

Tested by 2

rateLimitedJsonResponseFunction · 0.72
mockRemoteGenerationFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…