http_test.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. const Test = require('supertest').Test;
  3. class EggTest extends Test {
  4. /**
  5. * Unexpectations:
  6. *
  7. * .unexpectHeader('Content-Type')
  8. * .unexpectHeader('Content-Type', fn)
  9. *
  10. * @return {EggTest}
  11. * @api public
  12. */
  13. unexpectHeader(name, b) {
  14. if (typeof b === 'function') {
  15. this.end(b);
  16. }
  17. // header
  18. if (typeof name === 'string') {
  19. this._asserts.push(this._unexpectHeader.bind(this, name));
  20. }
  21. return this;
  22. }
  23. /**
  24. * Expectations:
  25. *
  26. * .expectHeader('Content-Type')
  27. * .expectHeader('Content-Type', fn)
  28. *
  29. * @return {EggTest}
  30. * @api public
  31. */
  32. expectHeader(name, b) {
  33. if (typeof b === 'function') {
  34. this.end(b);
  35. }
  36. // header
  37. if (typeof name === 'string') {
  38. this._asserts.push(this._expectHeader.bind(this, name));
  39. }
  40. return this;
  41. }
  42. _unexpectHeader(name, res) {
  43. const actual = res.headers[name.toLowerCase()];
  44. if (actual) {
  45. return new Error('unexpected "' + name + '" header field, got "' + actual + '"');
  46. }
  47. }
  48. _expectHeader(name, res) {
  49. const actual = res.headers[name.toLowerCase()];
  50. if (!actual) {
  51. return new Error('expected "' + name + '" header field');
  52. }
  53. }
  54. }
  55. module.exports = EggTest;