Skip to content

Commit 0a53ed4

Browse files
committed
add examples
1 parent 4219ffa commit 0a53ed4

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

examples/override-hook.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const fastifyOverride = require('../index')
2+
const t = require('tap')
3+
const Fastify = require('fastify')
4+
5+
const adminArea = async app => {
6+
app.addHook('onRequest', (req, reply) => {
7+
// Some auth strategy
8+
// ...
9+
10+
reply.status(401).send('You are not authenticated!')
11+
})
12+
13+
app.get('/health', async () => {
14+
return 'You are a admin!'
15+
})
16+
}
17+
18+
const server = async (app) => {
19+
app.register(adminArea, {
20+
prefix: 'admin'
21+
})
22+
}
23+
24+
t.test('test admin health endpoint', async t => {
25+
const app = Fastify()
26+
27+
await app.register(fastifyOverride, {
28+
override: {
29+
hooks: {
30+
// just bypass the authentication hook
31+
onRequest: async () => {}
32+
}
33+
}
34+
})
35+
36+
app.register(server)
37+
38+
const response = await app.inject({
39+
url: 'admin/health',
40+
method: 'GET'
41+
})
42+
43+
t.equal(response.statusCode, 200)
44+
t.equal(response.body, 'You are a admin!')
45+
})

examples/override-plugin.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const fastifyOverride = require('../index')
2+
const t = require('tap')
3+
const Fastify = require('fastify')
4+
const fp = require('fastify-plugin')
5+
6+
// This simulates the @fastify/postgres plugin https://github.com/fastify/fastify-postgres
7+
const fastifyPostgres = fp((app) => {}, {
8+
name: '@fastify/postgres'
9+
})
10+
11+
const users = async app => {
12+
app.get('/', {
13+
schema: {
14+
response: {
15+
200: {
16+
type: 'array',
17+
items: {
18+
type: 'object',
19+
properties: {
20+
id: {
21+
type: 'number'
22+
},
23+
name: {
24+
type: 'string'
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}, async function () {
32+
return (await this.pg.query('SELECT * from "users" LIMIT 10')).rows
33+
})
34+
}
35+
36+
const server = async (app) => {
37+
app.register(fastifyPostgres, {
38+
connectionString: 'postgres://postgres@localhost/postgres'
39+
})
40+
41+
app.register(users, {
42+
prefix: 'users'
43+
})
44+
}
45+
46+
t.test('GET /users - 200', async t => {
47+
const app = Fastify()
48+
49+
await app.register(fastifyOverride, {
50+
override: {
51+
plugins: {
52+
'@fastify/postgres': fp(async (instance) => {
53+
instance.decorate('pg', {
54+
query: async () => {
55+
return {
56+
rows: [
57+
{
58+
id: 1,
59+
name: 'Matthias',
60+
password: 'test123'
61+
},
62+
{
63+
id: 3,
64+
name: 'Max',
65+
password: 'alligator2'
66+
}
67+
]
68+
}
69+
}
70+
})
71+
})
72+
}
73+
}
74+
})
75+
76+
app.register(server)
77+
78+
const response = await app.inject({
79+
url: '/users',
80+
method: 'GET'
81+
})
82+
83+
const body = response.json()
84+
85+
t.equal(response.statusCode, 200)
86+
t.equal(body.length, 2)
87+
t.matchOnly(body, [
88+
{
89+
id: /\d+/,
90+
name: /.+/
91+
},
92+
{
93+
id: /\d+/,
94+
name: /.+/
95+
// password: /.*/ <-- this would fail
96+
}
97+
])
98+
})

0 commit comments

Comments
 (0)