MCPcopy Create free account
hub / github.com/oakserver/oak / createMockContext

Function createMockContext

testing.ts:82–188  ·  view source on GitHub ↗
(
  {
    ip = "127.0.0.1",
    method = "GET",
    params,
    path = "/",
    state,
    app = createMockApp(state),
    headers: requestHeaders,
    body = undefined,
  }: MockContextOptions<R> = {},
)

Source from the content-addressed store, hash-verified

80
81/** Create a mock of `Context` or `RouterContext`. */
82export function createMockContext<
83 R extends string,
84 P extends RouteParams<R> = RouteParams<R>,
85 S extends State = Record<string, any>,
86>(
87 {
88 ip = "127.0.0.1",
89 method = "GET",
90 params,
91 path = "/",
92 state,
93 app = createMockApp(state),
94 headers: requestHeaders,
95 body = undefined,
96 }: MockContextOptions<R> = {},
97): RouterContext<R, P, S> {
98 function createMockRequest(): Request {
99 const headers = new Headers(requestHeaders);
100 return {
101 get source(): globalThis.Request | undefined {
102 return new globalThis.Request(new URL(path, "http://localhost/"), {
103 method,
104 headers,
105 });
106 },
107 accepts(...types: string[]) {
108 if (!headers.has("Accept")) {
109 return;
110 }
111 if (types.length) {
112 return accepts({ headers }, ...types);
113 }
114 return accepts({ headers });
115 },
116 acceptsEncodings() {
117 return mockContextState.encodingsAccepted;
118 },
119 headers,
120 ip,
121 method,
122 path,
123 search: undefined,
124 searchParams: new URLSearchParams(),
125 url: new URL(path, "http://localhost/"),
126 hasBody: !!body,
127 body: body ? new Body({ headers, getBody: () => body }) : undefined,
128 } as any;
129 }
130
131 const request = createMockRequest();
132 const response = new Response(request);
133 const cookies = new SecureCookieMap(request, { response });
134
135 return ({
136 app,
137 params,
138 request,
139 cookies,

Callers 5

fnFunction · 0.90
fnFunction · 0.90
setupFunction · 0.90
setupFunction · 0.90
fnFunction · 0.90

Calls 2

createMockAppFunction · 0.70
createMockRequestFunction · 0.70

Tested by 5

fnFunction · 0.72
fnFunction · 0.72
setupFunction · 0.72
setupFunction · 0.72
fnFunction · 0.72