|
| 1 | +var httpRequest = require("../src/httpRequest"), |
| 2 | + bodyParser = require('body-parser'), |
| 3 | + express = require("express"); |
| 4 | + |
| 5 | +var port = 13371; |
| 6 | +var httpRequestServer = "http://localhost:"+port; |
| 7 | + |
| 8 | +var app = express(); |
| 9 | +app.use(bodyParser.json({ 'type': '*/*' })); |
| 10 | +app.get("/hello", function(req, res){ |
| 11 | + res.json({response: "OK"}); |
| 12 | +}); |
| 13 | + |
| 14 | +app.get("/404", function(req, res){ |
| 15 | + res.status(404); |
| 16 | + res.send("NO"); |
| 17 | +}); |
| 18 | + |
| 19 | +app.get("/301", function(req, res){ |
| 20 | + res.status(301); |
| 21 | + res.location("/hello"); |
| 22 | + res.send(); |
| 23 | +}); |
| 24 | + |
| 25 | +app.post('/echo', function(req, res){ |
| 26 | + res.json(req.body); |
| 27 | +}) |
| 28 | + |
| 29 | +app.listen(13371); |
| 30 | + |
| 31 | + |
| 32 | +describe("httpRequest", () => { |
| 33 | + |
| 34 | + it("should do /hello", (done) => { |
| 35 | + httpRequest({ |
| 36 | + url: httpRequestServer+"/hello" |
| 37 | + }).then(function(httpResponse){ |
| 38 | + expect(httpResponse.status).toBe(200); |
| 39 | + expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}')); |
| 40 | + expect(httpResponse.text).toEqual('{"response":"OK"}'); |
| 41 | + expect(httpResponse.data.response).toEqual("OK"); |
| 42 | + done(); |
| 43 | + }, function(){ |
| 44 | + fail("should not fail"); |
| 45 | + done(); |
| 46 | + }) |
| 47 | + }); |
| 48 | + |
| 49 | + it("should do /hello with callback and promises", (done) => { |
| 50 | + var calls = 0; |
| 51 | + httpRequest({ |
| 52 | + url: httpRequestServer+"/hello", |
| 53 | + success: function() { calls++; }, |
| 54 | + error: function() { calls++; } |
| 55 | + }).then(function(httpResponse){ |
| 56 | + expect(calls).toBe(1); |
| 57 | + expect(httpResponse.status).toBe(200); |
| 58 | + expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}')); |
| 59 | + expect(httpResponse.text).toEqual('{"response":"OK"}'); |
| 60 | + expect(httpResponse.data.response).toEqual("OK"); |
| 61 | + done(); |
| 62 | + }, function(){ |
| 63 | + fail("should not fail"); |
| 64 | + done(); |
| 65 | + }) |
| 66 | + }); |
| 67 | + |
| 68 | + it("should do not follow redirects by default", (done) => { |
| 69 | + |
| 70 | + httpRequest({ |
| 71 | + url: httpRequestServer+"/301" |
| 72 | + }).then(function(httpResponse){ |
| 73 | + expect(httpResponse.status).toBe(301); |
| 74 | + done(); |
| 75 | + }, function(){ |
| 76 | + fail("should not fail"); |
| 77 | + done(); |
| 78 | + }) |
| 79 | + }); |
| 80 | + |
| 81 | + it("should follow redirects when set", (done) => { |
| 82 | + |
| 83 | + httpRequest({ |
| 84 | + url: httpRequestServer+"/301", |
| 85 | + followRedirects: true |
| 86 | + }).then(function(httpResponse){ |
| 87 | + expect(httpResponse.status).toBe(200); |
| 88 | + expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}')); |
| 89 | + expect(httpResponse.text).toEqual('{"response":"OK"}'); |
| 90 | + expect(httpResponse.data.response).toEqual("OK"); |
| 91 | + done(); |
| 92 | + }, function(){ |
| 93 | + fail("should not fail"); |
| 94 | + done(); |
| 95 | + }) |
| 96 | + }); |
| 97 | + |
| 98 | + it("should fail on 404", (done) => { |
| 99 | + var calls = 0; |
| 100 | + httpRequest({ |
| 101 | + url: httpRequestServer+"/404", |
| 102 | + success: function() { |
| 103 | + calls++; |
| 104 | + fail("should not succeed"); |
| 105 | + done(); |
| 106 | + }, |
| 107 | + error: function(httpResponse) { |
| 108 | + calls++; |
| 109 | + expect(calls).toBe(1); |
| 110 | + expect(httpResponse.status).toBe(404); |
| 111 | + expect(httpResponse.buffer).toEqual(new Buffer('NO')); |
| 112 | + expect(httpResponse.text).toEqual('NO'); |
| 113 | + expect(httpResponse.data).toBe(undefined); |
| 114 | + done(); |
| 115 | + } |
| 116 | + }); |
| 117 | + }) |
| 118 | + |
| 119 | + it("should fail on 404", (done) => { |
| 120 | + httpRequest({ |
| 121 | + url: httpRequestServer+"/404", |
| 122 | + }).then(function(httpResponse){ |
| 123 | + fail("should not succeed"); |
| 124 | + done(); |
| 125 | + }, function(httpResponse){ |
| 126 | + expect(httpResponse.status).toBe(404); |
| 127 | + expect(httpResponse.buffer).toEqual(new Buffer('NO')); |
| 128 | + expect(httpResponse.text).toEqual('NO'); |
| 129 | + expect(httpResponse.data).toBe(undefined); |
| 130 | + done(); |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + it("should post on echo", (done) => { |
| 135 | + var calls = 0; |
| 136 | + httpRequest({ |
| 137 | + method: "POST", |
| 138 | + url: httpRequestServer+"/echo", |
| 139 | + body: { |
| 140 | + foo: "bar" |
| 141 | + }, |
| 142 | + success: function() { calls++; }, |
| 143 | + error: function() { calls++; } |
| 144 | + }).then(function(httpResponse){ |
| 145 | + expect(calls).toBe(1); |
| 146 | + expect(httpResponse.status).toBe(200); |
| 147 | + expect(httpResponse.data).toEqual({foo: "bar"}); |
| 148 | + done(); |
| 149 | + }, function(httpResponse){ |
| 150 | + fail("should not fail"); |
| 151 | + done(); |
| 152 | + }) |
| 153 | + }) |
| 154 | +}); |
0 commit comments