post.cookies.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 === "/cookies") {
  11. response.writeHead(200, { 'Set-Cookie': request.headers.cookie || "" });
  12. response.end();
  13. return;
  14. }
  15. if (request.url === "/cookies-redirect") {
  16. response.writeHead(302, { 'Location': 'http://localhost:8000/cookies' });
  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-Cookies', function() {
  25. before(function() {
  26. server.listen(8000);
  27. });
  28. describe('/cookies', function() {
  29. it("should send me back the cookies", function(done) {
  30. request.request({ url: "http://localhost:8000/cookies",
  31. method: 'POST',
  32. Cookies: { john: "doe", human: true },
  33. }
  34. , function(err, data, status, headers) {
  35. assert.equal(headers['set-cookie'], 'john=doe; human=true');
  36. assert.equal(200, status);
  37. done();
  38. });
  39. });
  40. it("should not send me back the cookies", function(done) {
  41. request.request({ url: "http://localhost:8000/cookies-redirect",
  42. method: 'POST',
  43. Cookies: { john: "doe", human: true },
  44. requestOptions: { trustRedirect: false },
  45. }
  46. , function(err, data, status, headers) {
  47. assert.equal(headers['set-cookie'], "");
  48. assert.equal(200, status);
  49. done();
  50. });
  51. });
  52. });
  53. after(function() {
  54. server.close();
  55. });
  56. });