1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const sget = require('simple-get').concat
|
6 | const fastify = require('..')()
|
7 |
|
8 | const opts = {
|
9 | schema: {
|
10 | response: {
|
11 | '2xx': {
|
12 | type: 'object',
|
13 | properties: {
|
14 | hello: {
|
15 | type: 'string'
|
16 | }
|
17 | }
|
18 | }
|
19 | }
|
20 | }
|
21 | }
|
22 |
|
23 | fastify.get('/return', opts, function (req, reply) {
|
24 | const promise = new Promise((resolve, reject) => {
|
25 | resolve({ hello: 'world' })
|
26 | })
|
27 | return promise
|
28 | })
|
29 |
|
30 | fastify.get('/return-error', opts, function (req, reply) {
|
31 | const promise = new Promise((resolve, reject) => {
|
32 | reject(new Error('some error'))
|
33 | })
|
34 | return promise
|
35 | })
|
36 |
|
37 | fastify.get('/double', function (req, reply) {
|
38 | setTimeout(function () {
|
39 |
|
40 | reply.send({ hello: 'world' })
|
41 | }, 20)
|
42 | return Promise.resolve({ hello: '42' })
|
43 | })
|
44 |
|
45 | fastify.get('/thenable', opts, function (req, reply) {
|
46 | setImmediate(function () {
|
47 | reply.send({ hello: 'world' })
|
48 | })
|
49 | return reply
|
50 | })
|
51 |
|
52 | fastify.get('/thenable-error', opts, function (req, reply) {
|
53 | setImmediate(function () {
|
54 | reply.send(new Error('kaboom'))
|
55 | })
|
56 | return reply
|
57 | })
|
58 |
|
59 | fastify.get('/return-reply', opts, function (req, reply) {
|
60 | return reply.send({ hello: 'world' })
|
61 | })
|
62 |
|
63 | fastify.listen(0, err => {
|
64 | t.error(err)
|
65 | fastify.server.unref()
|
66 |
|
67 | test('shorthand - sget return promise es6 get', t => {
|
68 | t.plan(4)
|
69 | sget({
|
70 | method: 'GET',
|
71 | url: 'http://localhost:' + fastify.server.address().port + '/return'
|
72 | }, (err, response, body) => {
|
73 | t.error(err)
|
74 | t.strictEqual(response.statusCode, 200)
|
75 | t.strictEqual(response.headers['content-length'], '' + body.length)
|
76 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
77 | })
|
78 | })
|
79 |
|
80 | test('shorthand - sget promise es6 get return error', t => {
|
81 | t.plan(2)
|
82 | sget({
|
83 | method: 'GET',
|
84 | url: 'http://localhost:' + fastify.server.address().port + '/return-error'
|
85 | }, (err, response, body) => {
|
86 | t.error(err)
|
87 | t.strictEqual(response.statusCode, 500)
|
88 | })
|
89 | })
|
90 |
|
91 | test('sget promise double send', t => {
|
92 | t.plan(3)
|
93 |
|
94 | sget({
|
95 | method: 'GET',
|
96 | url: 'http://localhost:' + fastify.server.address().port + '/double'
|
97 | }, (err, response, body) => {
|
98 | t.error(err)
|
99 | t.strictEqual(response.statusCode, 200)
|
100 | t.deepEqual(JSON.parse(body), { hello: '42' })
|
101 | })
|
102 | })
|
103 |
|
104 | test('thenable', t => {
|
105 | t.plan(4)
|
106 | sget({
|
107 | method: 'GET',
|
108 | url: 'http://localhost:' + fastify.server.address().port + '/thenable'
|
109 | }, (err, response, body) => {
|
110 | t.error(err)
|
111 | t.strictEqual(response.statusCode, 200)
|
112 | t.strictEqual(response.headers['content-length'], '' + body.length)
|
113 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
114 | })
|
115 | })
|
116 |
|
117 | test('thenable (error)', t => {
|
118 | t.plan(2)
|
119 | sget({
|
120 | method: 'GET',
|
121 | url: 'http://localhost:' + fastify.server.address().port + '/thenable-error'
|
122 | }, (err, response, body) => {
|
123 | t.error(err)
|
124 | t.strictEqual(response.statusCode, 500)
|
125 | })
|
126 | })
|
127 |
|
128 | test('return-reply', t => {
|
129 | t.plan(4)
|
130 | sget({
|
131 | method: 'GET',
|
132 | url: 'http://localhost:' + fastify.server.address().port + '/return-reply'
|
133 | }, (err, response, body) => {
|
134 | t.error(err)
|
135 | t.strictEqual(response.statusCode, 200)
|
136 | t.strictEqual(response.headers['content-length'], '' + body.length)
|
137 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
138 | })
|
139 | })
|
140 | })
|