MCPcopy
hub / github.com/loopbackio/loopback-next / stubExpressContext

Function stubExpressContext

packages/testlab/src/shot.ts:95–135  ·  view source on GitHub ↗
(
  requestOptions: ShotRequestOptions = {url: '/'},
)

Source from the content-addressed store, hash-verified

93}
94
95export function stubExpressContext(
96 requestOptions: ShotRequestOptions = {url: '/'},
97): ExpressContextStub {
98 const app = express();
99
100 const request = new ShotRequest(requestOptions) as express.Request;
101 // mix in Express Request API
102 const RequestApi = (express as any).request;
103 for (const key of Object.getOwnPropertyNames(RequestApi)) {
104 Object.defineProperty(
105 request,
106 key,
107 Object.getOwnPropertyDescriptor(RequestApi, key)!,
108 );
109 }
110 request.app = app;
111 request.originalUrl = request.url;
112 parseQuery(request);
113
114 let response: express.Response | undefined;
115 const result = new Promise<ObservedResponse>(resolve => {
116 response = new ShotResponse(request, resolve) as express.Response;
117 // mix in Express Response API
118 Object.assign(response, (express as any).response);
119 const ResponseApi = (express as any).response;
120 for (const key of Object.getOwnPropertyNames(ResponseApi)) {
121 Object.defineProperty(
122 response,
123 key,
124 Object.getOwnPropertyDescriptor(ResponseApi, key)!,
125 );
126 }
127 response.app = app;
128 (response as any).req = request;
129 (request as any).res = response;
130 });
131
132 const context = {app, request, response: response!, result};
133 defineCustomContextInspect(context, requestOptions);
134 return context;
135}
136
137/**
138 * Use `express.query` to parse the query string into `request.query` object

Callers 9

givenRequestFunction · 0.90
givenRequestFunction · 0.90
setupResponseMockFunction · 0.90
givenRequestFunction · 0.90
givenRequestFunction · 0.90
givenStubbedContextFunction · 0.90
givenRequestFunction · 0.90
givenRequestFunction · 0.90

Calls 2

parseQueryFunction · 0.85

Tested by 8

givenRequestFunction · 0.72
givenRequestFunction · 0.72
setupResponseMockFunction · 0.72
givenRequestFunction · 0.72
givenRequestFunction · 0.72
givenStubbedContextFunction · 0.72
givenRequestFunction · 0.72
givenRequestFunction · 0.72