|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | + |
| 5 | +const axios = require('axios') |
| 6 | +const fastify = require('fastify') |
| 7 | +const BPromise = require('bluebird') |
| 8 | + |
| 9 | +const plugin = require('./index.js') |
| 10 | + |
| 11 | +test('should cache the cacheable request', (t) => { |
| 12 | + t.plan(6) |
| 13 | + const instance = fastify() |
| 14 | + instance.register(plugin, {ttl: 1000}) |
| 15 | + instance.get('/cache', (req, res) => { |
| 16 | + res.send({hello: 'world'}) |
| 17 | + }) |
| 18 | + instance.listen(0, async (err) => { |
| 19 | + if (err) t.threw(err) |
| 20 | + instance.server.unref() |
| 21 | + const portNum = instance.server.address().port |
| 22 | + const address = `http://127.0.0.1:${portNum}/cache` |
| 23 | + const [response1, response2] = await BPromise.all([ |
| 24 | + axios.get(address), |
| 25 | + axios.get(address), |
| 26 | + ]) |
| 27 | + t.is(response1.status, 200) |
| 28 | + t.is(response2.status, 200) |
| 29 | + t.is(response1.headers['x-response-cache'], 'miss') |
| 30 | + t.is(response2.headers['x-response-cache'], 'hit') |
| 31 | + t.deepEqual(response1.data, {hello: 'world'}) |
| 32 | + t.deepEqual(response2.data, {hello: 'world'}) |
| 33 | + }) |
| 34 | +}) |
| 35 | + |
| 36 | +test('should not cache the uncacheable request', (t) => { |
| 37 | + t.plan(6) |
| 38 | + const instance = fastify() |
| 39 | + instance.register(plugin, {ttl: 1000}) |
| 40 | + instance.post('/no-cache', (req, res) => { |
| 41 | + res.send({hello: 'world'}) |
| 42 | + }) |
| 43 | + instance.listen(0, async (err) => { |
| 44 | + if (err) t.threw(err) |
| 45 | + instance.server.unref() |
| 46 | + const portNum = instance.server.address().port |
| 47 | + const address = `http://127.0.0.1:${portNum}/no-cache` |
| 48 | + const [response1, response2] = await BPromise.all([ |
| 49 | + axios.post(address, {}), |
| 50 | + axios.post(address, {}), |
| 51 | + ]) |
| 52 | + t.is(response1.status, 200) |
| 53 | + t.is(response2.status, 200) |
| 54 | + t.notOk(response1.headers['x-response-cache']) |
| 55 | + t.notOk(response2.headers['x-response-cache']) |
| 56 | + t.deepEqual(response1.data, {hello: 'world'}) |
| 57 | + t.deepEqual(response2.data, {hello: 'world'}) |
| 58 | + }) |
| 59 | +}) |
| 60 | + |
| 61 | +test('should apply ttl config', (t) => { |
| 62 | + t.plan(9) |
| 63 | + const instance = fastify() |
| 64 | + instance.register(plugin, {ttl: 2000}) |
| 65 | + instance.get('/ttl', (req, res) => { |
| 66 | + res.send({hello: 'world'}) |
| 67 | + }) |
| 68 | + instance.listen(0, async (err) => { |
| 69 | + if (err) t.threw(err) |
| 70 | + instance.server.unref() |
| 71 | + const portNum = instance.server.address().port |
| 72 | + const address = `http://127.0.0.1:${portNum}/ttl` |
| 73 | + const [response1, response2] = await BPromise.all([ |
| 74 | + axios.get(address), |
| 75 | + axios.get(address), |
| 76 | + ]) |
| 77 | + await BPromise.delay(3000) |
| 78 | + const response3 = await axios.get(address) |
| 79 | + t.is(response1.status, 200) |
| 80 | + t.is(response2.status, 200) |
| 81 | + t.is(response3.status, 200) |
| 82 | + t.is(response1.headers['x-response-cache'], 'miss') |
| 83 | + t.is(response2.headers['x-response-cache'], 'hit') |
| 84 | + t.is(response3.headers['x-response-cache'], 'miss') |
| 85 | + t.deepEqual(response1.data, {hello: 'world'}) |
| 86 | + t.deepEqual(response2.data, {hello: 'world'}) |
| 87 | + t.deepEqual(response3.data, {hello: 'world'}) |
| 88 | + }) |
| 89 | +}) |
| 90 | + |
| 91 | +test('should apply additionalCondition config', (t) => { |
| 92 | + t.plan(12) |
| 93 | + const instance = fastify() |
| 94 | + instance.register(plugin, { |
| 95 | + additionalCondition: { |
| 96 | + headers: ['x-should-applied'], |
| 97 | + }, |
| 98 | + }) |
| 99 | + instance.get('/headers', (req, res) => { |
| 100 | + res.send({hello: 'world'}) |
| 101 | + }) |
| 102 | + instance.listen(0, async (err) => { |
| 103 | + if (err) t.threw(err) |
| 104 | + instance.server.unref() |
| 105 | + const portNum = instance.server.address().port |
| 106 | + const address = `http://127.0.0.1:${portNum}/headers` |
| 107 | + const [response1, response2, response3, response4] = await BPromise.all([ |
| 108 | + axios.get(address, { |
| 109 | + headers: {'x-should-applied': 'yes'}, |
| 110 | + }), |
| 111 | + axios.get(address, { |
| 112 | + headers: {'x-should-applied': 'yes'}, |
| 113 | + }), |
| 114 | + axios.get(address, { |
| 115 | + headers: {'x-should-applied': 'no'}, |
| 116 | + }), |
| 117 | + axios.get(address), |
| 118 | + ]) |
| 119 | + t.is(response1.status, 200) |
| 120 | + t.is(response2.status, 200) |
| 121 | + t.is(response3.status, 200) |
| 122 | + t.is(response4.status, 200) |
| 123 | + t.is(response1.headers['x-response-cache'], 'miss') |
| 124 | + t.is(response2.headers['x-response-cache'], 'hit') |
| 125 | + t.is(response3.headers['x-response-cache'], 'miss') |
| 126 | + t.is(response4.headers['x-response-cache'], 'miss') |
| 127 | + t.deepEqual(response1.data, {hello: 'world'}) |
| 128 | + t.deepEqual(response2.data, {hello: 'world'}) |
| 129 | + t.deepEqual(response3.data, {hello: 'world'}) |
| 130 | + t.deepEqual(response4.data, {hello: 'world'}) |
| 131 | + }) |
| 132 | +}) |
| 133 | + |
| 134 | +test('should waiting for cache if multiple same request come in', (t) => { |
| 135 | + t.plan(6) |
| 136 | + const instance = fastify() |
| 137 | + instance.register(plugin, {ttl: 5000}) |
| 138 | + instance.get('/waiting', async (req, res) => { |
| 139 | + await BPromise.delay(3000) |
| 140 | + res.send({hello: 'world'}) |
| 141 | + }) |
| 142 | + instance.listen(0, async (err) => { |
| 143 | + if (err) t.threw(err) |
| 144 | + instance.server.unref() |
| 145 | + const portNum = instance.server.address().port |
| 146 | + const address = `http://127.0.0.1:${portNum}/waiting` |
| 147 | + const [response1, response2] = await BPromise.all([ |
| 148 | + axios.get(address), |
| 149 | + axios.get(address), |
| 150 | + ]) |
| 151 | + t.is(response1.status, 200) |
| 152 | + t.is(response2.status, 200) |
| 153 | + t.is(response1.headers['x-response-cache'], 'miss') |
| 154 | + t.is(response2.headers['x-response-cache'], 'hit') |
| 155 | + t.deepEqual(response1.data, {hello: 'world'}) |
| 156 | + t.deepEqual(response2.data, {hello: 'world'}) |
| 157 | + }) |
| 158 | +}) |
| 159 | + |
| 160 | +test('should not waiting for cache due to timeout', (t) => { |
| 161 | + t.plan(6) |
| 162 | + const instance = fastify() |
| 163 | + instance.register(plugin) |
| 164 | + instance.get('/abort', async (req, res) => { |
| 165 | + await BPromise.delay(2000) |
| 166 | + res.send({hello: 'world'}) |
| 167 | + }) |
| 168 | + instance.listen(0, async (err) => { |
| 169 | + if (err) t.threw(err) |
| 170 | + instance.server.unref() |
| 171 | + const portNum = instance.server.address().port |
| 172 | + const address = `http://127.0.0.1:${portNum}/abort` |
| 173 | + const [response1, response2] = await BPromise.all([ |
| 174 | + axios.get(address), |
| 175 | + axios.get(address), |
| 176 | + ]) |
| 177 | + t.is(response1.status, 200) |
| 178 | + t.is(response2.status, 200) |
| 179 | + t.is(response1.headers['x-response-cache'], 'miss') |
| 180 | + t.is(response2.headers['x-response-cache'], 'miss') |
| 181 | + t.deepEqual(response1.data, {hello: 'world'}) |
| 182 | + t.deepEqual(response2.data, {hello: 'world'}) |
| 183 | + }) |
| 184 | +}) |
0 commit comments