get.js 3.1 KB

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