supertest.js 880 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const methods = require('methods');
  3. const EggTest = require('./http_test');
  4. const mockHttpServer = require('./mock_http_server');
  5. // patch from https://github.com/visionmedia/supertest/blob/199506d8dbfe0bb1434fc07c38cdcd1ab4c7c926/index.js#L19
  6. /**
  7. * Test against the given `app`,
  8. * returning a new `Test`.
  9. *
  10. * @param {Application} app
  11. * @return {Test}
  12. * @api public
  13. */
  14. module.exports = app => {
  15. const server = mockHttpServer(app);
  16. const obj = {};
  17. for (const method of methods) {
  18. obj[method] = url => {
  19. // support pathFor(url)
  20. if (url[0] !== '/') {
  21. const realUrl = app.router.pathFor(url);
  22. if (!realUrl) throw new Error(`Can\'t find router:${url}, please check your \'app/router.js\'`);
  23. url = realUrl;
  24. }
  25. return new EggTest(server, method, url);
  26. };
  27. }
  28. obj.del = obj.delete;
  29. return obj;
  30. };