express.post.js 3.6 KB

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