PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/simulator/node_modules/pusher/tests/integration/pusher/trigger.js

https://gitlab.com/silentx09/opiniothon
JavaScript | 298 lines | 265 code | 32 blank | 1 comment | 0 complexity | 615d90c42b6105d592a402d6baec593c MD5 | raw file
  1. var expect = require("expect.js");
  2. var nock = require("nock");
  3. var Pusher = require("../../../lib/pusher");
  4. describe("Pusher", function() {
  5. var pusher;
  6. beforeEach(function() {
  7. pusher = new Pusher({ appId: 1234, key: "f00d", secret: "beef" });
  8. nock.disableNetConnect();
  9. });
  10. afterEach(function() {
  11. nock.cleanAll();
  12. nock.enableNetConnect();
  13. });
  14. describe("#trigger", function() {
  15. it("should send the event when no callback is given", function(done) {
  16. var mock = nock("http://api.pusherapp.com")
  17. .filteringPath(function(path) {
  18. return path
  19. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  20. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  21. })
  22. .post(
  23. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=cf87d666b4a829a54fc44b313584b2d7&auth_signature=Y",
  24. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["test_channel"] }
  25. )
  26. .reply(200, "{}");
  27. pusher.trigger("test_channel", "my_event", { some: "data "});
  28. setTimeout(function() {
  29. // we have no callback in this test, so we wait until the next tick
  30. expect(mock.isDone()).to.be(true);
  31. done();
  32. }, 0);
  33. });
  34. it("should send the event to a single channel", function(done) {
  35. var mock = nock("http://api.pusherapp.com")
  36. .filteringPath(function(path) {
  37. return path
  38. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  39. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  40. })
  41. .post(
  42. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=e95168baf497b2e54b2c6cadd41a6a3f&auth_signature=Y",
  43. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["one"] }
  44. )
  45. .reply(200, "{}");
  46. pusher.trigger("one", "my_event", { some: "data "}, null, done);
  47. });
  48. it("should send the event to multiple channels", function(done) {
  49. var mock = nock("http://api.pusherapp.com")
  50. .filteringPath(function(path) {
  51. return path
  52. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  53. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  54. })
  55. .post(
  56. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=530dac0aa045e5f8e51c470aed0ce325&auth_signature=Y",
  57. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["one", "two", "three"] }
  58. )
  59. .reply(200, "{}");
  60. pusher.trigger(["one", "two", "three"], "my_event", { some: "data "}, null, done);
  61. });
  62. it("should serialize arrays into JSON", function(done) {
  63. var mock = nock("http://api.pusherapp.com")
  64. .filteringPath(function(path) {
  65. return path
  66. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  67. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  68. })
  69. .post(
  70. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=18e64b2fed38726915d79ebb4f8feb5b&auth_signature=Y",
  71. { name: "my_event", data: "[1,2,4]", channels: ["one"] }
  72. )
  73. .reply(200, "{}");
  74. pusher.trigger("one", "my_event", [1,2,4], null, done);
  75. });
  76. it("should not serialize strings into JSON", function(done) {
  77. var mock = nock("http://api.pusherapp.com")
  78. .filteringPath(function(path) {
  79. return path
  80. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  81. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  82. })
  83. .post(
  84. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=f358a562d00e1bfe1859132d932cd706&auth_signature=Y",
  85. { name: "test_event", data: "test string", channels: ["test"] }
  86. )
  87. .reply(200, "{}");
  88. pusher.trigger("test", "test_event", "test string", null, done);
  89. });
  90. it("should add socket_id to the request body", function(done) {
  91. var mock = nock("http://api.pusherapp.com")
  92. .filteringPath(function(path) {
  93. return path
  94. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  95. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  96. })
  97. .post(
  98. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=0478e1ed73804ae1be97cfa6554cf039&auth_signature=Y",
  99. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["test_channel"], socket_id: "123.567" }
  100. )
  101. .reply(200, "{}");
  102. pusher.trigger("test_channel", "my_event", { some: "data "}, "123.567", done);
  103. });
  104. it("should call back with the result", function(done) {
  105. var mock = nock("http://api.pusherapp.com")
  106. .filteringPath(function(path) {
  107. return path
  108. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  109. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  110. })
  111. .post(
  112. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=cf87d666b4a829a54fc44b313584b2d7&auth_signature=Y",
  113. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["test_channel"] }
  114. )
  115. .reply(200, "OK");
  116. pusher.trigger("test_channel", "my_event", { some: "data "}, null, function(error, request, response) {
  117. expect(error).to.be(null);
  118. expect(response.statusCode).to.equal(200);
  119. done();
  120. });
  121. });
  122. it("should call back with a RequestError if Pusher responds with 4xx", function(done) {
  123. var mock = nock("http://api.pusherapp.com")
  124. .filteringPath(function(path) {
  125. return path
  126. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  127. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  128. })
  129. .post(
  130. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=cf87d666b4a829a54fc44b313584b2d7&auth_signature=Y",
  131. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["test_channel"] }
  132. )
  133. .reply(400, "Error");
  134. pusher.trigger("test_channel", "my_event", { some: "data "}, null, function(error, request, response) {
  135. expect(error).to.be.a(Pusher.RequestError);
  136. expect(error.message).to.equal("Unexpected status code 400");
  137. expect(error.url).to.match(
  138. /^http:\/\/api.pusherapp.com\/apps\/1234\/events\?auth_key=f00d&auth_timestamp=[0-9]+&auth_version=1\.0&body_md5=cf87d666b4a829a54fc44b313584b2d7&auth_signature=[a-f0-9]+$/
  139. );
  140. expect(error.statusCode).to.equal(400);
  141. expect(error.body).to.equal("Error");
  142. done();
  143. });
  144. });
  145. it("should allow channel names with special characters: _ - = @ , . ;", function(done) {
  146. var mock = nock("http://api.pusherapp.com")
  147. .filteringPath(function(path) {
  148. return path
  149. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  150. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  151. })
  152. .post(
  153. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=024f0f297e27e131c8ec2c8817d153f4&auth_signature=Y",
  154. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["test_-=@,.;channel"] }
  155. )
  156. .reply(200, "OK");
  157. pusher.trigger("test_-=@,.;channel", "my_event", { some: "data "}, null, function(error, request, response) {
  158. expect(error).to.be(null);
  159. expect(response.statusCode).to.equal(200);
  160. done();
  161. });
  162. });
  163. it("should throw an error if called with more than 10 channels", function() {
  164. expect(function() {
  165. pusher.trigger(
  166. ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"], "x", {}
  167. );
  168. }).to.throwError(function(e) {
  169. expect(e).to.be.an(Error);
  170. expect(e.message).to.equal(
  171. "Can't trigger a message to more than 10 channels"
  172. );
  173. });
  174. });
  175. it("should throw an error if channel name is empty", function() {
  176. expect(function() {
  177. pusher.trigger("", "test");
  178. }).to.throwError(function(e) {
  179. expect(e).to.be.an(Error);
  180. expect(e.message).to.equal("Invalid channel name: ''");
  181. });
  182. });
  183. it("should throw an error if channel name is invalid", function() {
  184. expect(function() {
  185. pusher.trigger("abc$", "test");
  186. }).to.throwError(function(e) {
  187. expect(e).to.be.an(Error);
  188. expect(e.message).to.equal("Invalid channel name: 'abc$'");
  189. });
  190. });
  191. it("should throw an error if channel name is longer than 200 characters", function() {
  192. var channel = new Array(202).join("x"); // 201 characters
  193. expect(function() {
  194. pusher.trigger(channel, "test");
  195. }).to.throwError(function(e) {
  196. expect(e).to.be.an(Error);
  197. expect(e.message).to.equal("Channel name too long: '" + channel + "'");
  198. });
  199. });
  200. it("should throw an error if event name is longer than 200 characters", function() {
  201. var event = new Array(202).join("x"); // 201 characters
  202. expect(function() {
  203. pusher.trigger("test", event);
  204. }).to.throwError(function(e) {
  205. expect(e).to.be.an(Error);
  206. expect(e.message).to.equal("Too long event name: '" + event + "'");
  207. });
  208. });
  209. it("should respect the encryption, host and port config", function(done) {
  210. var pusher = new Pusher({
  211. appId: 1234,
  212. key: "f00d",
  213. secret: "beef",
  214. encrypted: true,
  215. host: "example.com",
  216. port: 1234
  217. });
  218. var mock = nock("https://example.com:1234")
  219. .filteringPath(function(path) {
  220. return path
  221. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  222. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  223. })
  224. .post(
  225. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=0478e1ed73804ae1be97cfa6554cf039&auth_signature=Y",
  226. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["test_channel"], socket_id: "123.567" }
  227. )
  228. .reply(200, "{}");
  229. pusher.trigger("test_channel", "my_event", { some: "data "}, "123.567", done);
  230. });
  231. it("should respect the timeout when specified", function(done) {
  232. var pusher = new Pusher({
  233. appId: 1234,
  234. key: "f00d",
  235. secret: "beef",
  236. timeout: 200
  237. });
  238. var mock = nock("http://api.pusherapp.com")
  239. .filteringPath(function(path) {
  240. return path
  241. .replace(/auth_timestamp=[0-9]+/, "auth_timestamp=X")
  242. .replace(/auth_signature=[0-9a-f]{64}/, "auth_signature=Y");
  243. })
  244. .post(
  245. "/apps/1234/events?auth_key=f00d&auth_timestamp=X&auth_version=1.0&body_md5=0478e1ed73804ae1be97cfa6554cf039&auth_signature=Y",
  246. { name: "my_event", data: "{\"some\":\"data \"}", channels: ["test_channel"], socket_id: "123.567" }
  247. )
  248. .delayConnection(200)
  249. .reply(200);
  250. pusher.trigger("test_channel", "my_event", { some: "data "}, "123.567", function(error, request, response) {
  251. var expectedError = new Error("ETIMEDOUT");
  252. expectedError.code = "ETIMEDOUT";
  253. expectedError.connect = undefined;
  254. expect(error).to.be.a(Pusher.RequestError);
  255. expect(error.message).to.equal("Request failed with an error");
  256. expect(error.error).to.eql(expectedError);
  257. expect(error.url).to.match(
  258. /^http:\/\/api.pusherapp.com\/apps\/1234\/events\?auth_key=f00d&auth_timestamp=[0-9]+&auth_version=1\.0&body_md5=0478e1ed73804ae1be97cfa6554cf039&auth_signature=[a-f0-9]+$/
  259. );
  260. expect(error.statusCode).to.equal(null);
  261. expect(error.body).to.equal(null);
  262. done();
  263. });
  264. });
  265. });
  266. });