-
-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathend-to-end.test.mjs
More file actions
145 lines (128 loc) · 3.72 KB
/
Copy pathend-to-end.test.mjs
File metadata and controls
145 lines (128 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// @ts-check
/**
* @import { GraphQLFieldConfig } from "graphql/type"
* @import { FileUpload } from "./processRequest.mjs"
*/
import { deepStrictEqual } from "node:assert";
import { createServer } from "node:http";
import { text } from "node:stream/consumers";
import { suite, test } from "node:test";
import { listen } from "async-listen";
import { graphql } from "graphql";
import {
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from "graphql/type";
import GraphQLUpload from "./GraphQLUpload.mjs";
import processRequest from "./processRequest.mjs";
import serverClose from "./test-helpers/serverClose.mjs";
suite(
"End-to-end.",
{
concurrency: true,
},
() => {
test("A GraphQL multipart request with a file upload.", async () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: {
inspectFile:
/**
* @satisfies {GraphQLFieldConfig<unknown, unknown, {
* file: Promise<FileUpload>,
* }>}
*/
({
description:
"Inspects a given file, resolving the file properties as JSON.",
type: GraphQLString,
args: {
file: {
description: "File to inspect.",
type: new GraphQLNonNull(GraphQLUpload),
},
},
async resolve(_parent, { file }) {
const { filename, mimetype, encoding, createReadStream } =
await file;
return JSON.stringify({
filename,
mimetype,
encoding,
content: await text(createReadStream()),
});
},
}),
},
}),
});
let serverError;
const server = createServer(async (request, response) => {
try {
const operation =
/**
* @type {{
* query: string,
* variables: {
* [name: string]: unknown,
* },
* }}
*/
(await processRequest(request, response));
const result = await graphql({
schema,
source: operation.query,
variableValues: operation.variables,
});
const json = JSON.stringify(result);
response.end(json);
} catch (error) {
serverError = error;
response.end();
}
});
const url = await listen(server);
try {
const fileProperties = {
filename: "a.txt",
mimetype: "text/plain",
encoding: "7bit",
content: "a",
};
const body = new FormData();
body.append(
"operations",
JSON.stringify({
query: /* GraphQL */ `
query ($file: Upload!) {
inspectFile(file: $file)
}
`,
variables: {
file: null,
},
}),
);
body.append("map", JSON.stringify({ 1: ["variables.file"] }));
body.append(
"1",
new File([fileProperties.content], fileProperties.filename, {
type: fileProperties.mimetype,
}),
);
const response = await fetch(url, { method: "POST", body });
if (serverError) throw serverError;
deepStrictEqual(await response.json(), {
data: {
inspectFile: JSON.stringify(fileProperties),
},
});
} finally {
await serverClose(server);
}
});
},
);