Skip to content

Commit 8cb1602

Browse files
committed
getting the tests passing
1 parent b7bac52 commit 8cb1602

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/__tests__/mockServer.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
HttpMethod,
55
HttpRequest,
66
HttpRequestHeaders,
7+
HttpRequestQuery,
78
Logger,
89
} from '@azure/functions';
910
import type {
@@ -13,7 +14,6 @@ import type {
1314
ServerResponse,
1415
} from 'http';
1516
import type { AddressInfo } from 'net';
16-
import { format } from 'url';
1717

1818
export function urlForHttpServer(httpServer: Server): string {
1919
const { address, port } = httpServer.address() as AddressInfo;
@@ -25,12 +25,7 @@ export function urlForHttpServer(httpServer: Server): string {
2525
// when listening).
2626
const hostname = address === '' || address === '::' ? 'localhost' : address;
2727

28-
return format({
29-
protocol: 'http',
30-
hostname,
31-
port,
32-
pathname: '/',
33-
});
28+
return `http://${hostname}:${port}`;
3429
}
3530

3631
export const createMockServer = (handler: AzureFunction) => {
@@ -41,10 +36,10 @@ export const createMockServer = (handler: AzureFunction) => {
4136
req.on('end', async () => {
4237
const azReq: HttpRequest = {
4338
method: (req.method as HttpMethod) || null,
44-
url: req.url || '',
39+
url: new URL(req.url || '', 'http://localhost').toString(),
4540
headers: processHeaders(req.headers),
4641
body,
47-
query: {},
42+
query: processQuery(req.url),
4843
params: {},
4944
user: null,
5045
parseFormBody: () => {
@@ -86,6 +81,24 @@ export const createMockServer = (handler: AzureFunction) => {
8681
};
8782
};
8883

84+
const processQuery: (url: string | undefined) => HttpRequestQuery = (url) => {
85+
if (!url) {
86+
return {};
87+
}
88+
89+
const uri = new URL(url, 'http://localhost');
90+
91+
const query: HttpRequestQuery = {};
92+
for (const [key, value] of uri.searchParams.entries()) {
93+
if (query[key] !== undefined) {
94+
query[key] = `${query[key]},${value}`;
95+
} else {
96+
query[key] = value;
97+
}
98+
}
99+
return query;
100+
};
101+
89102
const processHeaders: (headers: IncomingHttpHeaders) => HttpRequestHeaders = (
90103
headers,
91104
) => {

src/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
Context,
44
HttpRequest,
55
HttpRequestHeaders,
6-
HttpRequestQuery,
76
} from '@azure/functions';
87
import {
98
ApolloServer,
@@ -55,7 +54,7 @@ export function startServerAndCreateHandler<TContext extends BaseContext>(
5554
}
5655

5756
return {
58-
statusCode: status || 200,
57+
status: status || 200,
5958
headers: {
6059
...Object.fromEntries(headers),
6160
'content-length': Buffer.byteLength(body.string).toString(),
@@ -65,7 +64,7 @@ export function startServerAndCreateHandler<TContext extends BaseContext>(
6564
} catch (e) {
6665
context.log.error('Failure processing GraphQL request', e);
6766
return {
68-
statusCode: 400,
67+
status: 400,
6968
body: (e as Error).message,
7069
};
7170
}
@@ -80,28 +79,30 @@ function normalizeRequest(req: HttpRequest): HTTPGraphQLRequest {
8079
return {
8180
method: req.method,
8281
headers: normalizeHeaders(req.headers),
83-
search: normalizeQueryStringParams(req.query),
84-
body: req.body,
82+
search: new URL(req.url).search,
83+
body: parseBody(req.body, req.headers['content-type']),
8584
};
8685
}
8786

87+
function parseBody(
88+
body: string | null | undefined,
89+
contentType: string | undefined,
90+
): object | string {
91+
if (body) {
92+
if (contentType === 'application/json' && typeof body === 'string') {
93+
return JSON.parse(body);
94+
}
95+
if (contentType === 'text/plain') {
96+
return body;
97+
}
98+
}
99+
return '';
100+
}
101+
88102
function normalizeHeaders(headers: HttpRequestHeaders): HeaderMap {
89103
const headerMap = new HeaderMap();
90104
for (const [key, value] of Object.entries(headers)) {
91105
headerMap.set(key, value ?? '');
92106
}
93107
return headerMap;
94108
}
95-
96-
function normalizeQueryStringParams(
97-
queryStringParams: HttpRequestQuery | null,
98-
): string {
99-
const queryStringRecord: Record<string, string> = {};
100-
for (const [key, value] of Object.entries(queryStringParams ?? {})) {
101-
queryStringRecord[key] = value ?? '';
102-
}
103-
return Object.keys(queryStringRecord).reduce(
104-
(acc, key) => `${acc}&${key}=${queryStringRecord[key]}`,
105-
'',
106-
);
107-
}

0 commit comments

Comments
 (0)