|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const t = require('tap') |
| 6 | +const fastify = require('fastify') |
| 7 | +const sanitize = require('sanitize-filename') |
| 8 | + |
| 9 | +const FjsStandaloneCompiler = require('../standalone') |
| 10 | + |
| 11 | +function generateFileName (routeOpts) { |
| 12 | + return `/fjs-generated-${sanitize(routeOpts.schema.$id)}-${routeOpts.method}-${routeOpts.httpPart}-${sanitize(routeOpts.url)}.js` |
| 13 | +} |
| 14 | + |
| 15 | +t.test('errors', t => { |
| 16 | + t.plan(2) |
| 17 | + t.throws(() => { |
| 18 | + FjsStandaloneCompiler() |
| 19 | + }, 'missing restoreFunction') |
| 20 | + t.throws(() => { |
| 21 | + FjsStandaloneCompiler({ readMode: false }) |
| 22 | + }, 'missing storeFunction') |
| 23 | +}) |
| 24 | + |
| 25 | +t.test('generate standalone code', t => { |
| 26 | + t.plan(5) |
| 27 | + |
| 28 | + const base = { |
| 29 | + $id: 'urn:schema:base', |
| 30 | + definitions: { |
| 31 | + hello: { type: 'string' } |
| 32 | + }, |
| 33 | + type: 'object', |
| 34 | + properties: { |
| 35 | + hello: { $ref: '#/definitions/hello' } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const refSchema = { |
| 40 | + $id: 'urn:schema:ref', |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + hello: { $ref: 'urn:schema:base#/definitions/hello' } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const endpointSchema = { |
| 48 | + schema: { |
| 49 | + $id: 'urn:schema:endpoint', |
| 50 | + $ref: 'urn:schema:ref' |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const schemaMap = { |
| 55 | + [base.$id]: base, |
| 56 | + [refSchema.$id]: refSchema |
| 57 | + } |
| 58 | + |
| 59 | + const factory = FjsStandaloneCompiler({ |
| 60 | + readMode: false, |
| 61 | + storeFunction (routeOpts, schemaValidationCode) { |
| 62 | + t.same(routeOpts, endpointSchema) |
| 63 | + t.type(schemaValidationCode, 'string') |
| 64 | + fs.writeFileSync(path.join(__dirname, '/fjs-generated.js'), schemaValidationCode) |
| 65 | + t.pass('stored the validation function') |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + const compiler = factory(schemaMap) |
| 70 | + compiler(endpointSchema) |
| 71 | + t.pass('compiled the endpoint schema') |
| 72 | + |
| 73 | + t.test('usage standalone code', t => { |
| 74 | + t.plan(3) |
| 75 | + const standaloneSerializer = require('./fjs-generated') |
| 76 | + t.ok(standaloneSerializer) |
| 77 | + |
| 78 | + const valid = standaloneSerializer({ hello: 'world' }) |
| 79 | + t.same(valid, JSON.stringify({ hello: 'world' })) |
| 80 | + |
| 81 | + const invalid = standaloneSerializer({ hello: [] }) |
| 82 | + t.same(invalid, '{"hello":""}') |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +t.test('fastify integration - writeMode', async t => { |
| 87 | + t.plan(4) |
| 88 | + |
| 89 | + const factory = FjsStandaloneCompiler({ |
| 90 | + readMode: false, |
| 91 | + storeFunction (routeOpts, schemaSerializationCode) { |
| 92 | + const fileName = generateFileName(routeOpts) |
| 93 | + t.ok(routeOpts) |
| 94 | + fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode) |
| 95 | + t.pass(`stored the validation function ${fileName}`) |
| 96 | + }, |
| 97 | + restoreFunction () { |
| 98 | + t.fail('write mode ON') |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + const app = buildApp(factory) |
| 103 | + await app.ready() |
| 104 | +}) |
| 105 | + |
| 106 | +t.test('fastify integration - readMode', async t => { |
| 107 | + t.plan(6) |
| 108 | + |
| 109 | + const factory = FjsStandaloneCompiler({ |
| 110 | + readMode: true, |
| 111 | + storeFunction () { |
| 112 | + t.fail('read mode ON') |
| 113 | + }, |
| 114 | + restoreFunction (routeOpts) { |
| 115 | + const fileName = generateFileName(routeOpts) |
| 116 | + t.pass(`restore the validation function ${fileName}}`) |
| 117 | + return require(path.join(__dirname, fileName)) |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + const app = buildApp(factory) |
| 122 | + await app.ready() |
| 123 | + |
| 124 | + let res = await app.inject({ |
| 125 | + url: '/foo', |
| 126 | + method: 'POST' |
| 127 | + }) |
| 128 | + t.equal(res.statusCode, 200) |
| 129 | + t.equal(res.payload, JSON.stringify({ hello: 'world' })) |
| 130 | + |
| 131 | + res = await app.inject({ |
| 132 | + url: '/bar?lang=it', |
| 133 | + method: 'GET' |
| 134 | + }) |
| 135 | + t.equal(res.statusCode, 200) |
| 136 | + t.equal(res.payload, JSON.stringify({ lang: 'en' })) |
| 137 | +}) |
| 138 | + |
| 139 | +function buildApp (factory) { |
| 140 | + const app = fastify({ |
| 141 | + exposeHeadRoutes: false, |
| 142 | + jsonShorthand: false, |
| 143 | + schemaController: { |
| 144 | + compilersFactory: { |
| 145 | + buildSerializer: factory |
| 146 | + } |
| 147 | + } |
| 148 | + }) |
| 149 | + |
| 150 | + app.addSchema({ |
| 151 | + $id: 'urn:schema:foo', |
| 152 | + type: 'object', |
| 153 | + properties: { |
| 154 | + name: { type: 'string' }, |
| 155 | + id: { type: 'integer' } |
| 156 | + } |
| 157 | + }) |
| 158 | + |
| 159 | + app.post('/foo', { |
| 160 | + schema: { |
| 161 | + response: { |
| 162 | + 200: { |
| 163 | + $id: 'urn:schema:response', |
| 164 | + type: 'object', |
| 165 | + properties: { |
| 166 | + hello: { $ref: 'urn:schema:foo#/properties/name' } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + }, () => { return { hello: 'world' } }) |
| 172 | + |
| 173 | + app.get('/bar', { |
| 174 | + schema: { |
| 175 | + response: { |
| 176 | + 200: { |
| 177 | + $id: 'urn:schema:response:bar', |
| 178 | + type: 'object', |
| 179 | + properties: { |
| 180 | + lang: { type: 'string', enum: ['it', 'en'] } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + }, () => { return { lang: 'en' } }) |
| 186 | + |
| 187 | + return app |
| 188 | +} |
0 commit comments