Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delay to query parameters #206

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions public/guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ <h3 id="nestedresources">Nested resources</h3>
<li><a href="https://jsonplaceholder.typicode.com/users/1/todos">https://jsonplaceholder.typicode.com/users/1/todos</a></li>
<li><a href="https://jsonplaceholder.typicode.com/users/1/posts">https://jsonplaceholder.typicode.com/users/1/posts</a></li>
</ul>
<h3 id="delay">Delay</h3>
<p>With any request, add a <code style="padding: 0 !important;">delay=N</code> query string parameter to simulate a slow-running API response.</p>
<ul>
<li><a href="https://jsonplaceholder.typicode.com/posts/1/comments?delay=500">https://jsonplaceholder.typicode.com/posts/1/comments?delay=500</a></li>
<li><a href="https://jsonplaceholder.typicode.com/posts?userId=1&delay=1000">https://jsonplaceholder.typicode.com/posts?userId=1&delay=1000</a></li>
</ul>
<p></main></p>
</div>

Expand Down
8 changes: 8 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ <h2>Routes</h2>
<a href="/guide/">here</a>.
</p>

<!-- Delay -->
<h2>Simulate a delay</h2>

<p>
You can simulate a delay by adding a
<strong>delay</strong> query parameter (measured in milliseconds) to any route.
</p>

<!-- JSON Server -->
<h2>Use your own data</h2>
<!-- <p>
Expand Down
11 changes: 11 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ const data = require('../data.json')
const app = jsonServer.create()
const router = jsonServer.router(clone(data), { _isFake: true })

app.use((req, res, next) => {
if (req.query.delay) {
const delay = parseInt(req.query.delay, 10)
if (delay > 0) {
setTimeout(next, delay)
return
}
}
next()
})

app.use((req, res, next) => {
if (req.path === '/') return next()
router.db.setState(clone(data))
Expand Down
7 changes: 7 additions & 0 deletions templates/GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,11 @@ Available nested routes:
* https://jsonplaceholder.typicode.com/users/1/todos
* https://jsonplaceholder.typicode.com/users/1/posts

### Delay

With any request, add a <code style="padding: 0 !important;">delay=N</code> query string parameter to simulate a slow-running API response.

* https://jsonplaceholder.typicode.com/posts/1/comments?delay=500
* https://jsonplaceholder.typicode.com/posts?userId=1&delay=1000

</main>
10 changes: 9 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,15 @@ <h2>Routes</h2>

<p>
<strong>Note</strong>: you can view detailed examples
<a href="guide.html">here</a>.
<a href="/guide/">here</a>.
</p>

<!-- Delay -->
<h2>Simulate a delay</h2>

<p>
You can simulate a delay by adding a
<strong>delay</strong> query parameter (measured in milliseconds) to any route.
</p>

<!-- JSON Server -->
Expand Down
25 changes: 25 additions & 0 deletions test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ test('GET /', (t) => {
.expect(200, (err) => t.end(err))
})

test('GET /?delay=100', (t) => {
const start = Date.now()
request(app)
.get('/?delay=100')
.expect(200, (err) => {
const end = Date.now()
t.assert(end - start >= 100, `actual response time ${end - start}`)
t.assert(end - start < 200, `actual response time ${end - start}`)
t.end(err)
})
})

test('POST /', (t) => {
const max = 10
t.plan(max * 3)
Expand All @@ -34,3 +46,16 @@ test('POST /', (t) => {
})
}
})

test('POST /posts?delay=100', (t) => {
const start = Date.now()
request(app)
.post('/posts?delay=100')
.send({ body: 'foo' })
.expect(201, (err) => {
const end = Date.now()
t.assert(end - start >= 100, `actual response time ${end - start}`)
t.assert(end - start < 200, `actual response time ${end - start}`)
t.end(err)
})
})