You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					160 lines
				
				3.4 KiB
			
		
		
			
		
	
	
					160 lines
				
				3.4 KiB
			| 
											3 years ago
										 | 'use strict' | ||
|  | 
 | ||
|  | const t = require('tap') | ||
|  | const test = t.test | ||
|  | const sget = require('simple-get').concat | ||
|  | const fastify = require('..')() | ||
|  | 
 | ||
|  | const opts = { | ||
|  |   schema: { | ||
|  |     response: { | ||
|  |       200: { | ||
|  |         type: 'object', | ||
|  |         properties: { | ||
|  |           hello: { | ||
|  |             type: 'string' | ||
|  |           } | ||
|  |         } | ||
|  |       }, | ||
|  |       '2xx': { | ||
|  |         type: 'object', | ||
|  |         properties: { | ||
|  |           hello: { | ||
|  |             type: 'number' | ||
|  |           } | ||
|  |         } | ||
|  |       } | ||
|  |     } | ||
|  |   } | ||
|  | } | ||
|  | 
 | ||
|  | test('shorthand - output string', t => { | ||
|  |   t.plan(1) | ||
|  |   try { | ||
|  |     fastify.get('/string', opts, function (req, reply) { | ||
|  |       reply.code(200).send({ hello: 'world' }) | ||
|  |     }) | ||
|  |     t.pass() | ||
|  |   } catch (e) { | ||
|  |     t.fail() | ||
|  |   } | ||
|  | }) | ||
|  | 
 | ||
|  | test('shorthand - output number', t => { | ||
|  |   t.plan(1) | ||
|  |   try { | ||
|  |     fastify.get('/number', opts, function (req, reply) { | ||
|  |       reply.code(201).send({ hello: 55 }) | ||
|  |     }) | ||
|  |     t.pass() | ||
|  |   } catch (e) { | ||
|  |     t.fail() | ||
|  |   } | ||
|  | }) | ||
|  | 
 | ||
|  | test('wrong object for schema - output', t => { | ||
|  |   t.plan(1) | ||
|  |   try { | ||
|  |     fastify.get('/wrong-object-for-schema', opts, function (req, reply) { | ||
|  |       // will send { }
 | ||
|  |       reply.code(201).send({ hello: 'world' }) | ||
|  |     }) | ||
|  |     t.pass() | ||
|  |   } catch (e) { | ||
|  |     t.fail() | ||
|  |   } | ||
|  | }) | ||
|  | 
 | ||
|  | test('empty response', t => { | ||
|  |   t.plan(1) | ||
|  |   try { | ||
|  |     // no checks
 | ||
|  |     fastify.get('/empty', opts, function (req, reply) { | ||
|  |       reply.code(204).send() | ||
|  |     }) | ||
|  |     t.pass() | ||
|  |   } catch (e) { | ||
|  |     t.fail() | ||
|  |   } | ||
|  | }) | ||
|  | 
 | ||
|  | test('unlisted response code', t => { | ||
|  |   t.plan(1) | ||
|  |   try { | ||
|  |     fastify.get('/400', opts, function (req, reply) { | ||
|  |       reply.code(400).send({ hello: 'DOOM' }) | ||
|  |     }) | ||
|  |     t.pass() | ||
|  |   } catch (e) { | ||
|  |     t.fail() | ||
|  |   } | ||
|  | }) | ||
|  | 
 | ||
|  | fastify.listen(0, err => { | ||
|  |   t.error(err) | ||
|  |   fastify.server.unref() | ||
|  | 
 | ||
|  |   test('shorthand - string get ok', t => { | ||
|  |     t.plan(4) | ||
|  |     sget({ | ||
|  |       method: 'GET', | ||
|  |       url: 'http://localhost:' + fastify.server.address().port + '/string' | ||
|  |     }, (err, response, body) => { | ||
|  |       t.error(err) | ||
|  |       t.equal(response.statusCode, 200) | ||
|  |       t.equal(response.headers['content-length'], '' + body.length) | ||
|  |       t.same(JSON.parse(body), { hello: 'world' }) | ||
|  |     }) | ||
|  |   }) | ||
|  | 
 | ||
|  |   test('shorthand - number get ok', t => { | ||
|  |     t.plan(4) | ||
|  |     sget({ | ||
|  |       method: 'GET', | ||
|  |       url: 'http://localhost:' + fastify.server.address().port + '/number' | ||
|  |     }, (err, response, body) => { | ||
|  |       t.error(err) | ||
|  |       t.equal(response.statusCode, 201) | ||
|  |       t.equal(response.headers['content-length'], '' + body.length) | ||
|  |       t.same(JSON.parse(body), { hello: 55 }) | ||
|  |     }) | ||
|  |   }) | ||
|  | 
 | ||
|  |   test('shorthand - wrong-object-for-schema', t => { | ||
|  |     t.plan(4) | ||
|  |     sget({ | ||
|  |       method: 'GET', | ||
|  |       url: 'http://localhost:' + fastify.server.address().port + '/wrong-object-for-schema' | ||
|  |     }, (err, response, body) => { | ||
|  |       t.error(err) | ||
|  |       t.equal(response.statusCode, 201) | ||
|  |       t.equal(response.headers['content-length'], '' + body.length) | ||
|  |       t.same(JSON.parse(body), {}) | ||
|  |     }) | ||
|  |   }) | ||
|  | 
 | ||
|  |   test('shorthand - empty', t => { | ||
|  |     t.plan(2) | ||
|  |     sget({ | ||
|  |       method: 'GET', | ||
|  |       url: 'http://localhost:' + fastify.server.address().port + '/empty' | ||
|  |     }, (err, response, body) => { | ||
|  |       t.error(err) | ||
|  |       t.equal(response.statusCode, 204) | ||
|  |     }) | ||
|  |   }) | ||
|  | 
 | ||
|  |   test('shorthand - 400', t => { | ||
|  |     t.plan(4) | ||
|  |     sget({ | ||
|  |       method: 'GET', | ||
|  |       url: 'http://localhost:' + fastify.server.address().port + '/400' | ||
|  |     }, (err, response, body) => { | ||
|  |       t.error(err) | ||
|  |       t.equal(response.statusCode, 400) | ||
|  |       t.equal(response.headers['content-length'], '' + body.length) | ||
|  |       t.same(JSON.parse(body), { hello: 'DOOM' }) | ||
|  |     }) | ||
|  |   }) | ||
|  | }) |