Skip to content

Commit f5fd222

Browse files
Ryan Zectypicode
Ryan Zec
authored andcommitted
added support for _delay in query string to delay response of request on per request basis (typicode#673)
* added support for passing _delay as query parameter to delay response by X milliseconds * added tests / fixed linting error
1 parent dfbfd56 commit f5fd222

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

src/server/router/delay.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const pause = require('connect-pause')
2+
3+
module.exports = function delay(req, res, next) {
4+
// NOTE: for some reason unknown to me, if the default is 0, the tests seems to add 2 seconds
5+
// NOTE: to each test, a default value of 1 does not seem to be effected by that issue
6+
const _delay = !isNaN(parseFloat(req.query._delay))
7+
? parseFloat(req.query._delay)
8+
: 1
9+
delete req.query._delay
10+
pause(_delay)(req, res, next)
11+
}

src/server/router/nested.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const express = require('express')
22
const pluralize = require('pluralize')
3+
const delay = require('./delay')
34

45
module.exports = opts => {
56
const router = express.Router()
7+
router.use(delay)
68

79
// Rewrite URL (/:resource/:id/:nested -> /:nested) and request query
810
function get(req, res, next) {

src/server/router/plural.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const pluralize = require('pluralize')
44
const write = require('./write')
55
const getFullURL = require('./get-full-url')
66
const utils = require('../utils')
7+
const delay = require('./delay')
78

89
module.exports = (db, name, opts) => {
910
// Create router
1011
const router = express.Router()
12+
router.use(delay)
1113

1214
// Embed function used in GET /name and GET /name/id
1315
function embed(resource, e) {

src/server/router/singular.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const express = require('express')
22
const write = require('./write')
33
const getFullURL = require('./get-full-url')
4+
const delay = require('./delay')
45

56
module.exports = (db, name) => {
67
const router = express.Router()
8+
router.use(delay)
79

810
function show(req, res, next) {
911
res.locals.data = db.get(name).value()

test/server/plural.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,18 @@ describe('Server', () => {
501501
})
502502
})
503503

504+
describe('GET /:resource>_delay=', () => {
505+
it('should delay response', done => {
506+
const start = new Date()
507+
request(server)
508+
.get('/posts?_delay=1100')
509+
.expect(200, function(err) {
510+
const end = new Date()
511+
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
512+
})
513+
})
514+
})
515+
504516
describe('POST /:resource', () => {
505517
it('should respond with json, create a resource and increment id', async () => {
506518
await request(server)
@@ -546,6 +558,19 @@ describe('Server', () => {
546558
.expect(201))
547559
})
548560

561+
describe('POST /:resource?_delay=', () => {
562+
it('should delay response', done => {
563+
const start = new Date()
564+
request(server)
565+
.post('/posts?_delay=1100')
566+
.send({ body: 'foo', booleanValue: true, integerValue: 1 })
567+
.expect(201, function(err) {
568+
const end = new Date()
569+
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
570+
})
571+
})
572+
})
573+
549574
describe('PUT /:resource/:id', () => {
550575
it('should respond with json and replace resource', async () => {
551576
const post = { id: 1, booleanValue: true, integerValue: 1 }
@@ -573,6 +598,20 @@ describe('Server', () => {
573598
.expect(404))
574599
})
575600

601+
describe('PUT /:resource:id?_delay=', () => {
602+
it('should delay response', done => {
603+
const start = new Date()
604+
request(server)
605+
.put('/posts/1?_delay=1100')
606+
.set('Accept', 'application/json')
607+
.send({ id: 1, booleanValue: true, integerValue: 1 })
608+
.expect(200, function(err) {
609+
const end = new Date()
610+
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
611+
})
612+
})
613+
})
614+
576615
describe('PATCH /:resource/:id', () => {
577616
it('should respond with json and update resource', async () => {
578617
const partial = { body: 'bar' }
@@ -597,6 +636,20 @@ describe('Server', () => {
597636
.expect(404))
598637
})
599638

639+
describe('PATCH /:resource:id?_delay=', () => {
640+
it('should delay response', done => {
641+
const start = new Date()
642+
request(server)
643+
.patch('/posts/1?_delay=1100')
644+
.send({ body: 'bar' })
645+
.send({ id: 1, booleanValue: true, integerValue: 1 })
646+
.expect(200, function(err) {
647+
const end = new Date()
648+
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
649+
})
650+
})
651+
})
652+
600653
describe('DELETE /:resource/:id', () => {
601654
it('should respond with empty data, destroy resource and dependent resources', async () => {
602655
await request(server)
@@ -615,6 +668,19 @@ describe('Server', () => {
615668
.expect(404))
616669
})
617670

671+
describe('DELETE /:resource:id?_delay=', () => {
672+
it('should delay response', done => {
673+
const start = new Date()
674+
request(server)
675+
.del('/posts/1?_delay=1100')
676+
.send({ id: 1, booleanValue: true, integerValue: 1 })
677+
.expect(200, function(err) {
678+
const end = new Date()
679+
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
680+
})
681+
})
682+
})
683+
618684
describe('Static routes', () => {
619685
describe('GET /', () => {
620686
it('should respond with html', () =>

0 commit comments

Comments
 (0)