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.

34 lines
1.1 KiB

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