|
1 | | -"use strict"; |
| 1 | +'use strict' |
2 | 2 |
|
3 | | -const Fastify = require("fastify"); |
4 | | -const FormData = require("form-data"); |
5 | | -const http = require("node:http"); |
6 | | -const multipart = require(".."); |
7 | | -const { once } = require("node:events"); |
8 | | -const fs = require("node:fs"); |
9 | | -const path = require("node:path"); |
10 | | -const test = require("node:test"); |
| 3 | +const Fastify = require('fastify') |
| 4 | +const FormData = require('form-data') |
| 5 | +const http = require('node:http') |
| 6 | +const multipart = require('..') |
| 7 | +const { once } = require('node:events') |
| 8 | +const fs = require('node:fs') |
| 9 | +const path = require('node:path') |
| 10 | +const test = require('node:test') |
11 | 11 |
|
12 | | -const filePath = path.join(__dirname, "../README.md"); |
| 12 | +const filePath = path.join(__dirname, '../README.md') |
13 | 13 |
|
14 | | -test("show modify the generated schema", async (t) => { |
15 | | - t.plan(6); |
| 14 | +test('show modify the generated schema', async (t) => { |
| 15 | + t.plan(6) |
16 | 16 |
|
17 | 17 | const fastify = Fastify({ |
18 | 18 | ajv: { |
19 | | - plugins: [multipart.ajvFilePlugin], |
20 | | - }, |
21 | | - }); |
| 19 | + plugins: [multipart.ajvFilePlugin] |
| 20 | + } |
| 21 | + }) |
22 | 22 |
|
23 | | - t.after(() => fastify.close()); |
| 23 | + t.after(() => fastify.close()) |
24 | 24 |
|
25 | | - await fastify.register(multipart, { attachFieldsToBody: true }); |
26 | | - await fastify.register(require("@fastify/swagger"), { |
27 | | - mode: "dynamic", |
| 25 | + await fastify.register(multipart, { attachFieldsToBody: true }) |
| 26 | + await fastify.register(require('@fastify/swagger'), { |
| 27 | + mode: 'dynamic', |
28 | 28 |
|
29 | 29 | openapi: { |
30 | | - openapi: "3.1.0", |
31 | | - }, |
32 | | - }); |
| 30 | + openapi: '3.1.0' |
| 31 | + } |
| 32 | + }) |
33 | 33 |
|
34 | 34 | fastify.post( |
35 | | - "/", |
| 35 | + '/', |
36 | 36 | { |
37 | 37 | schema: { |
38 | | - operationId: "test", |
39 | | - consumes: ["multipart/form-data"], |
| 38 | + operationId: 'test', |
| 39 | + consumes: ['multipart/form-data'], |
40 | 40 | body: { |
41 | | - type: "object", |
| 41 | + type: 'object', |
42 | 42 | properties: { |
43 | | - field: { isFile: true }, |
44 | | - }, |
45 | | - }, |
46 | | - }, |
| 43 | + field: { isFile: true } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
47 | 47 | }, |
48 | 48 | async function (_req, reply) { |
49 | | - reply.code(200).send(); |
50 | | - }, |
51 | | - ); |
| 49 | + reply.code(200).send() |
| 50 | + } |
| 51 | + ) |
52 | 52 |
|
53 | | - await fastify.ready(); |
| 53 | + await fastify.ready() |
54 | 54 |
|
55 | | - const post = fastify.swagger().paths["/"].post; |
56 | | - t.assert.strictEqual(post.operationId, "test"); |
| 55 | + const post = fastify.swagger().paths['/'].post |
| 56 | + t.assert.strictEqual(post.operationId, 'test') |
57 | 57 | // requestBody.required is not asserted because @fastify/swagger v9.7.0 |
58 | 58 | // introduced it (fastify/fastify-swagger#903), making it version-dependent. |
59 | 59 | t.assert.deepStrictEqual(post.requestBody.content, { |
60 | | - "multipart/form-data": { |
| 60 | + 'multipart/form-data': { |
61 | 61 | schema: { |
62 | | - type: "object", |
| 62 | + type: 'object', |
63 | 63 | properties: { |
64 | | - field: { type: "string", format: "binary" }, |
65 | | - }, |
66 | | - }, |
67 | | - }, |
68 | | - }); |
| 64 | + field: { type: 'string', format: 'binary' } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
69 | 69 | t.assert.deepStrictEqual(post.responses, { |
70 | | - 200: { description: "Default Response" }, |
71 | | - }); |
| 70 | + 200: { description: 'Default Response' } |
| 71 | + }) |
72 | 72 |
|
73 | | - await fastify.listen({ port: 0 }); |
| 73 | + await fastify.listen({ port: 0 }) |
74 | 74 |
|
75 | 75 | // request without file |
76 | 76 | { |
77 | | - const form = new FormData(); |
| 77 | + const form = new FormData() |
78 | 78 | const req = http.request({ |
79 | | - protocol: "http:", |
80 | | - hostname: "localhost", |
| 79 | + protocol: 'http:', |
| 80 | + hostname: 'localhost', |
81 | 81 | port: fastify.server.address().port, |
82 | | - path: "/", |
| 82 | + path: '/', |
83 | 83 | headers: form.getHeaders(), |
84 | | - method: "POST", |
85 | | - }); |
86 | | - |
87 | | - form.append("field", JSON.stringify({}), { |
88 | | - contentType: "application/json", |
89 | | - }); |
90 | | - form.pipe(req); |
91 | | - |
92 | | - const [res] = await once(req, "response"); |
93 | | - res.resume(); |
94 | | - await once(res, "end"); |
95 | | - t.assert.strictEqual(res.statusCode, 400); // body/field should be a file |
| 84 | + method: 'POST' |
| 85 | + }) |
| 86 | + |
| 87 | + form.append('field', JSON.stringify({}), { |
| 88 | + contentType: 'application/json' |
| 89 | + }) |
| 90 | + form.pipe(req) |
| 91 | + |
| 92 | + const [res] = await once(req, 'response') |
| 93 | + res.resume() |
| 94 | + await once(res, 'end') |
| 95 | + t.assert.strictEqual(res.statusCode, 400) // body/field should be a file |
96 | 96 | } |
97 | 97 |
|
98 | 98 | // request with file |
99 | 99 | { |
100 | | - const form = new FormData(); |
| 100 | + const form = new FormData() |
101 | 101 | const req = http.request({ |
102 | | - protocol: "http:", |
103 | | - hostname: "localhost", |
| 102 | + protocol: 'http:', |
| 103 | + hostname: 'localhost', |
104 | 104 | port: fastify.server.address().port, |
105 | | - path: "/", |
| 105 | + path: '/', |
106 | 106 | headers: form.getHeaders(), |
107 | | - method: "POST", |
108 | | - }); |
109 | | - |
110 | | - form.append("field", fs.createReadStream(filePath), { |
111 | | - contentType: "multipart/form-data", |
112 | | - }); |
113 | | - form.pipe(req); |
114 | | - |
115 | | - const [res] = await once(req, "response"); |
116 | | - res.resume(); |
117 | | - await once(res, "end"); |
118 | | - t.assert.strictEqual(res.statusCode, 200); |
| 107 | + method: 'POST' |
| 108 | + }) |
| 109 | + |
| 110 | + form.append('field', fs.createReadStream(filePath), { |
| 111 | + contentType: 'multipart/form-data' |
| 112 | + }) |
| 113 | + form.pipe(req) |
| 114 | + |
| 115 | + const [res] = await once(req, 'response') |
| 116 | + res.resume() |
| 117 | + await once(res, 'end') |
| 118 | + t.assert.strictEqual(res.statusCode, 200) |
119 | 119 | } |
120 | | - t.assert.ok("res ended successfully"); |
121 | | -}); |
| 120 | + t.assert.ok('res ended successfully') |
| 121 | +}) |
0 commit comments