express.get.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. let Express = require('express')
  2. let request = require('../index');
  3. let assert = require('assert');
  4. const app = Express();
  5. const port = 3005;
  6. let server;
  7. describe('/GET-Express', function() {
  8. before(function() {
  9. server = app.listen(port, () => {
  10. ''
  11. console.log("Test are running on port : " + port);
  12. app.get("/", function(req, res) {
  13. res.append('Content-Type', 'text/plain');
  14. res.append('authorization', req.headers.authorization || "");
  15. res.send("Hello, world!\n")
  16. })
  17. app.get('/redirect', function(req, res) {
  18. res.redirect('/')
  19. })
  20. app.get('/redirect1', function(req, res) {
  21. res.redirect('/redirect')
  22. })
  23. });
  24. });
  25. describe('/', function() {
  26. it('should return 200', function(done) {
  27. request.get('http://localhost:3005/?hey=d', function(err, data, status, headers) {
  28. assert.ifError(err);
  29. assert.equal(200, status);
  30. done();
  31. });
  32. });
  33. it('should say "Hello, world!"', function(done) {
  34. request.get("http://localhost:3005", function(err, data, status, headers) {
  35. assert.ifError(err);
  36. assert.equal('Hello, world!\n', data);
  37. done();
  38. });
  39. });
  40. it("should have content-type to 'text/plain'", function(done) {
  41. request.get("http://localhost:3005", function(err, data, status, headers) {
  42. assert.ifError(err);
  43. assert.equal('text/plain; charset=utf-8', headers['content-type']);
  44. done();
  45. });
  46. });
  47. });
  48. describe('/redirect', function() {
  49. it("should throw an error (no redirect allowed)'", function(done) {
  50. request.request({ url: "http://localhost:3005/redirect",
  51. method: 'GET',
  52. requestOptions: { followRedirect: false }
  53. }
  54. , function(err, data, status, headers) {
  55. assert.equal(JSON.parse(err).code, 0)
  56. assert.equal(302, status);
  57. done();
  58. });
  59. });
  60. it("should not throw an error'", function(done) {
  61. request.request({ url: "http://localhost:3005/redirect",
  62. method: 'GET',
  63. requestOptions: { followRedirect: true }
  64. }
  65. , function(err, data, status, headers) {
  66. console.log(err, data);
  67. assert.equal(200, status);
  68. done();
  69. });
  70. });
  71. it("should throw an error'", function(done) {
  72. request.request({ url: "http://localhost:3005/redirect1",
  73. method: 'GET',
  74. requestOptions: { maxRedirect: 1 }
  75. }
  76. , function(err, data, status, headers) {
  77. assert.equal(JSON.parse(err).code, 1)
  78. assert.equal(302, status);
  79. done();
  80. });
  81. });
  82. it("should keep headers", function(done) {
  83. request.request({ url: "http://localhost:3005/redirect",
  84. method: 'GET',
  85. requestOptions: { },
  86. headers: {
  87. authorization: "Yo Zaral !"
  88. }
  89. }
  90. , function(err, data, status, headers) {
  91. assert.equal(headers.authorization, "Yo Zaral !")
  92. assert.equal(200, status);
  93. done();
  94. });
  95. });
  96. it("should not keep headers", function(done) {
  97. request.request({ url: "http://localhost:3005/redirect",
  98. method: 'GET',
  99. requestOptions: { trustRedirect: false },
  100. headers: {
  101. authorization: "Yo Zaral !"
  102. }
  103. }
  104. , function(err, data, status, headers) {
  105. assert.equal(headers.authorization, "")
  106. assert.equal(200, status);
  107. done();
  108. });
  109. });
  110. });
  111. after(function () {
  112. server.close();
  113. });
  114. });