post.js 3.5 KB

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