|
| 1 | +var Pubnub = require('cloud/pubnub'), |
| 2 | + assert = require('assert'), |
| 3 | + Sandbox = require('cloud/utils/sandbox'), |
| 4 | + TestSuite = require('cloud/utils/testSuite'); |
| 5 | + |
| 6 | +var test = new TestSuite("Error callback"), |
| 7 | + pubnub, |
| 8 | + sandbox; |
| 9 | + |
| 10 | +test.before(function () { |
| 11 | + pubnub = Pubnub.init({ |
| 12 | + pubilsh_key: 'pub-c-a82961b5-d8ee-462c-bd16-0cd53edbbe09', |
| 13 | + subscribe_key: 'sub-c-f5674c7e-7d52-11e3-a993-02ee2ddab7fe', |
| 14 | + heartbeat_interval: false |
| 15 | + }); |
| 16 | +}); |
| 17 | + |
| 18 | +test.beforeEach(function () { |
| 19 | + sandbox = Sandbox.create(); |
| 20 | +}); |
| 21 | + |
| 22 | +test.it("should be invoked on JSON response without error description", function (done) { |
| 23 | + sandbox.stub(Parse.Cloud, 'httpRequest', function () { |
| 24 | + var deferred = new Parse.Promise(); |
| 25 | + deferred.resolve("{}"); |
| 26 | + return deferred; |
| 27 | + }, "request"); |
| 28 | + |
| 29 | + pubnub.publish({ |
| 30 | + channel: "blah", |
| 31 | + message: "hey", |
| 32 | + callback: function () { |
| 33 | + done(new Error("Success callback invoked while shouldn't")) |
| 34 | + }, |
| 35 | + error: function (error) { |
| 36 | + assert.equal(error.message, "Bad JSON response"); |
| 37 | + done(); |
| 38 | + } |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +test.it("should be invoked on JSON error without error description", function (done) { |
| 43 | + sandbox.stub(Parse.Cloud, 'httpRequest', function () { |
| 44 | + var deferred = new Parse.Promise(); |
| 45 | + deferred.reject("{}"); |
| 46 | + return deferred; |
| 47 | + }, "request"); |
| 48 | + |
| 49 | + pubnub.publish({ |
| 50 | + channel: "blah", |
| 51 | + message: "hey", |
| 52 | + callback: function () { |
| 53 | + done(new Error("Success callback invoked while shouldn't")) |
| 54 | + }, |
| 55 | + error: function (error) { |
| 56 | + assert.equal(error.message, "Network error"); |
| 57 | + done(); |
| 58 | + } |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +test.it("should be invoked on non-JSON response", function (done) { |
| 63 | + sandbox.stub(Parse.Cloud, 'httpRequest', function (options) { |
| 64 | + options.url = 'http://google.com'; |
| 65 | + return sandbox.invokeOriginal("request", [options]); |
| 66 | + }, "request"); |
| 67 | + |
| 68 | + pubnub.publish({ |
| 69 | + channel: "blah", |
| 70 | + message: "hey", |
| 71 | + callback: function () { |
| 72 | + done(new Error("Success callback invoked while shouldn't")) |
| 73 | + }, |
| 74 | + error: function (error) { |
| 75 | + assert.equal(error.message, "Bad JSON response"); |
| 76 | + done(); |
| 77 | + } |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +test.it("should be invoked on non-JSON error", function (done) { |
| 82 | + sandbox.stub(Parse.Cloud, 'httpRequest', function (options) { |
| 83 | + options.url = 'http://blah.hey'; |
| 84 | + return sandbox.invokeOriginal("request", [options]); |
| 85 | + }, "request"); |
| 86 | + |
| 87 | + pubnub.publish({ |
| 88 | + channel: "blah", |
| 89 | + message: "hey", |
| 90 | + callback: function () { |
| 91 | + done(new Error("Success callback invoked while shouldn't")) |
| 92 | + }, |
| 93 | + error: function (error) { |
| 94 | + assert.equal(error.message, "Network error"); |
| 95 | + done(); |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +test.it("should be invoked on any #subscribe() invocation", function (done) { |
| 101 | + assert.throws(function () { |
| 102 | + pubnub.subscribe({}); |
| 103 | + }); |
| 104 | + done(); |
| 105 | +}); |
| 106 | + |
| 107 | +test.afterEach(function () { |
| 108 | + sandbox.restore(); |
| 109 | +}); |
| 110 | + |
| 111 | +module.exports = test; |
0 commit comments