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.

32 lines
1.1 KiB

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