post.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. response.writeHead(200, {'Content-Type': 'text/html'})
  11. response.end(body)
  12. })
  13. });
  14. describe('/POST', function () {
  15. before(function () {
  16. server.listen(8000);
  17. });
  18. describe('/', function () {
  19. it('should return 200', function (done) {
  20. request.post('http://localhost:8000/',function(err, data, status) {
  21. assert.ifError(err);
  22. assert.equal(200, status);
  23. done();
  24. });
  25. });
  26. it('should say "Hello, world!" inside a JSON object', function (done) {
  27. request.post("http://localhost:8000", {hello: 'Hello, world!'}, function(err, data) {
  28. assert.ifError(err);
  29. assert.deepEqual({hello: 'Hello, world!'}, JSON.parse(data));
  30. done();
  31. });
  32. });
  33. it("should have content-type to 'text/html'", function (done) {
  34. request.post("http://localhost:8000", function(err, data, status, headers) {
  35. assert.ifError(err);
  36. assert.equal('text/html' , headers['content-type']);
  37. done();
  38. });
  39. });
  40. });
  41. after(function () {
  42. server.close();
  43. });
  44. });