You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.0 KiB

  1. const users = require('./users.json')
  2. /* responses for /users */
  3. const mockResponses = [
  4. { request: { method: 'PUT' }, response: { status: 400 } },
  5. { request: { method: 'DELETE' }, response: { status: 400 } },
  6. {
  7. /* for GET requests, return a subset of data filtered on 'minAge' and 'nationality' */
  8. request: { method: 'GET' },
  9. response: function (ctx) {
  10. ctx.body = users.filter(user => {
  11. const meetsMinAge = (user.age || 1000) >= (Number(ctx.query.minAge) || 0)
  12. const requiredNationality = user.nationality === (ctx.query.nationality || user.nationality)
  13. return meetsMinAge && requiredNationality
  14. })
  15. }
  16. },
  17. {
  18. /* for POST requests, create a new user with the request data (a JSON user) */
  19. request: { method: 'POST' },
  20. response: function (ctx) {
  21. const newUser = ctx.request.body
  22. users.push(newUser)
  23. newUser.id = users.length
  24. ctx.status = 201
  25. ctx.response.set('Location', `/users/${newUser.id}`)
  26. }
  27. }
  28. ]
  29. module.exports = mockResponses