PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/silverlight/3.4/PubnubSilverlight.Example/UnitTest/WhenAMessageIsPublished.cs

https://github.com/dchenatl/c-sharp
C# | 739 lines | 650 code | 89 blank | 0 comment | 104 complexity | 1c09a8a89155028f7cf750080b363c04 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using System.ComponentModel;
  7. using System.Threading;
  8. using System.Collections;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using Microsoft.Silverlight.Testing;
  12. using PubNubMessaging.Core;
  13. using Newtonsoft.Json;
  14. using Newtonsoft.Json.Linq;
  15. namespace PubnubSilverlight.UnitTest
  16. {
  17. [TestClass]
  18. public class WhenAMessageIsPublished : SilverlightTest
  19. {
  20. bool isPublished2 = false;
  21. bool isPublished3 = false;
  22. bool isUnencryptPublished = false;
  23. bool isUnencryptObjectPublished = false;
  24. bool isEncryptObjectPublished = false;
  25. bool isUnencryptDH = false;
  26. bool isUnencryptObjectDH = false;
  27. bool isEncryptObjectDH = false;
  28. bool isEncryptPublished = false;
  29. bool isSecretEncryptPublished = false;
  30. bool isEncryptDH = false;
  31. bool isSecretEncryptDH = false;
  32. bool isComplexObjectPublished = false;
  33. bool isComplexObjectDetailedHistory = false;
  34. bool isSerializedObjectMessagePublished = false;
  35. bool isSerializedObjectMessageDetailedHistory = false;
  36. bool isLargeMessagePublished = false;
  37. long unEncryptPublishTimetoken = 0;
  38. long unEncryptObjectPublishTimetoken = 0;
  39. long encryptObjectPublishTimetoken = 0;
  40. long encryptPublishTimetoken = 0;
  41. long secretEncryptPublishTimetoken = 0;
  42. long complexObjectPublishTimetoken = 0;
  43. long serializedMessagePublishTimetoken = 0;
  44. const string messageForUnencryptPublish = "Pubnub Messaging API 1";
  45. const string messageForEncryptPublish = "漢語";
  46. const string messageForSecretEncryptPublish = "Pubnub Messaging API 2";
  47. const string messageLarge2K = "Numerous questions remain about the origins of the chemical and what impact its apparent use could have on the ongoing Syrian civil war and international involvement in it.When asked if the intelligence community's conclusion pushed the situation across President Barack Obama's \"red line\" that could potentially trigger more U.S. involvement in the Syrian civil war, Hagel said it's too soon to say.\"We need all the facts. We need all the information,\" he said. \"What I've just given you is what our intelligence community has said they know. As I also said, they are still assessing and they are still looking at what happened, who was responsible and the other specifics that we'll need.\" In a letter sent to lawmakers before Hagel's announcement, the White House said that intelligence analysts have concluded \"with varying degrees of confidence that the Syrian regime has used chemical weapons on a small scale in Syria, specifically the chemical agent sarin.\" In the letter, signed by White House legislative affairs office Director Miguel Rodriguez, the White House said the \"chain of custody\" of the chemicals was not clear and that intelligence analysts could not confirm the circumstances under which the sarin was used, including the role of Syrian President Bashar al-Assad's regime. Read Rodriguez's letter to Levin (PDF) But, the letter said, \"we do believe that any use of chemical weapons in Syria would very likely have originated with the Assad regime.\" The Syrian government has been battling a rebellion for more than two years, bringing international condemnation of the regime and pleas for greater international assistance. The United Nations estimated in February that more than 70,000 people had died since the conflict began. The administration is \"pressing for a comprehensive United Nations investigation that can credibly evaluate the evidence and establish what took place,\" the White House letter said. Sen. John McCain, one of the lawmakers who received the letter, said the use of";
  48. string messageObjectForUnencryptPublish = "";
  49. string messageObjectForEncryptPublish = "";
  50. string messageComplexObjectForPublish = "";
  51. string serializedObjectMessageForPublish;
  52. bool isCheck = false;
  53. bool isUnencryptCheck = false;
  54. bool isUnencryptObjectPubCheck = false;
  55. bool isUnencryptObjectDHCheck = false;
  56. bool isEncryptObjectPubCheck = false;
  57. bool isEncryptObjectDHCheck = false;
  58. bool isEncryptPubCheck = false;
  59. bool isEncryptDHCheck = false;
  60. bool isSecretEncryptPubCheck = false;
  61. bool isSecretEncryptDHCheck = false;
  62. bool isComplexObjectPublishCheck = false;
  63. bool isComplexObjectDetailedHistoryCheck = false;
  64. bool isSerializedObjectMessageCheck = false;
  65. bool isSerializedMessageDetailedHistoryCheck = false;
  66. bool isPublishMessageTooLargeCheck = false;
  67. bool isCheck2 = false;
  68. bool isCheck3 = false;
  69. [TestMethod]
  70. [Asynchronous]
  71. public void ThenUnencryptPublishShouldReturnSuccessCodeAndInfo()
  72. {
  73. isUnencryptPublished = false;
  74. Pubnub pubnub = new Pubnub("demo","demo","","",false);
  75. string channel = "my/channel";
  76. string message = messageForUnencryptPublish;
  77. PubnubUnitTest unitTest = new PubnubUnitTest();
  78. unitTest.TestClassName = "WhenAMessageIsPublished";
  79. unitTest.TestCaseName = "ThenUnencryptPublishShouldReturnSuccessCodeAndInfo";
  80. pubnub.PubnubUnitTest = unitTest;
  81. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessUnencryptPublishCodeCallback));
  82. EnqueueConditional(() => isCheck);
  83. EnqueueCallback(() =>
  84. {
  85. if (!isUnencryptPublished)
  86. {
  87. Assert.IsTrue(isUnencryptPublished, "Unencrypt Publish Failed");
  88. }
  89. else
  90. {
  91. EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, unEncryptPublishTimetoken, -1, false, CaptureUnencryptDetailedHistoryCallback));
  92. EnqueueConditional(() => isUnencryptCheck);
  93. EnqueueCallback(() => Assert.IsTrue(isUnencryptDH, "Unable to match the successful unencrypt Publish"));
  94. }
  95. });
  96. EnqueueTestComplete();
  97. }
  98. [Asynchronous]
  99. public void ReturnSuccessUnencryptPublishCodeCallback(string result)
  100. {
  101. if (!string.IsNullOrWhiteSpace(result))
  102. {
  103. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  104. if (receivedObj is object[])
  105. {
  106. long statusCode = Int64.Parse(receivedObj[0].ToString());
  107. string statusMsg = (string)receivedObj[1];
  108. if (statusCode == 1 && statusMsg.ToLower() == "sent")
  109. {
  110. isUnencryptPublished = true;
  111. unEncryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
  112. }
  113. }
  114. }
  115. isCheck = true;
  116. }
  117. [Asynchronous]
  118. public void CaptureUnencryptDetailedHistoryCallback(string result)
  119. {
  120. if (!string.IsNullOrWhiteSpace(result))
  121. {
  122. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  123. if (receivedObj is object[])
  124. {
  125. JArray jObj = JArray.Parse(receivedObj[0].ToString());
  126. if (jObj[0].ToString() == messageForUnencryptPublish)
  127. {
  128. isUnencryptDH = true;
  129. }
  130. }
  131. }
  132. isUnencryptCheck = true;
  133. }
  134. [TestMethod]
  135. [Asynchronous]
  136. public void ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo()
  137. {
  138. isUnencryptObjectPublished = false;
  139. Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
  140. string channel = "my/channel";
  141. object message = new CustomClass();
  142. messageObjectForUnencryptPublish = JsonConvert.SerializeObject(message);
  143. PubnubUnitTest unitTest = new PubnubUnitTest();
  144. unitTest.TestClassName = "WhenAMessageIsPublished";
  145. unitTest.TestCaseName = "ThenUnencryptObjectPublishShouldReturnSuccessCodeAndInfo";
  146. pubnub.PubnubUnitTest = unitTest;
  147. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessUnencryptObjectPublishCodeCallback));
  148. EnqueueConditional(() => isUnencryptObjectPubCheck);
  149. EnqueueCallback(() =>
  150. {
  151. if (!isUnencryptObjectPublished)
  152. {
  153. Assert.IsTrue(isUnencryptObjectPublished, "Unencrypt Publish Failed");
  154. }
  155. else
  156. {
  157. EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, unEncryptObjectPublishTimetoken, -1, false, CaptureUnencryptObjectDetailedHistoryCallback));
  158. EnqueueConditional(() => isUnencryptObjectDHCheck);
  159. EnqueueCallback(() => Assert.IsTrue(isUnencryptObjectDH, "Unable to match the successful unencrypt object Publish"));
  160. }
  161. });
  162. EnqueueTestComplete();
  163. }
  164. [Asynchronous]
  165. public void ReturnSuccessUnencryptObjectPublishCodeCallback(string result)
  166. {
  167. if (!string.IsNullOrWhiteSpace(result))
  168. {
  169. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  170. if (receivedObj is object[])
  171. {
  172. long statusCode = Int64.Parse(receivedObj[0].ToString());
  173. string statusMsg = (string)receivedObj[1];
  174. if (statusCode == 1 && statusMsg.ToLower() == "sent")
  175. {
  176. isUnencryptObjectPublished = true;
  177. unEncryptObjectPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
  178. }
  179. }
  180. }
  181. isUnencryptObjectPubCheck = true;
  182. }
  183. [Asynchronous]
  184. public void CaptureUnencryptObjectDetailedHistoryCallback(string result)
  185. {
  186. if (!string.IsNullOrWhiteSpace(result))
  187. {
  188. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  189. if (receivedObj is object[])
  190. {
  191. JArray jArr = JArray.Parse(receivedObj[0].ToString());
  192. if (jArr[0].ToString(Formatting.None) == messageObjectForUnencryptPublish)
  193. {
  194. isUnencryptObjectDH = true;
  195. }
  196. }
  197. }
  198. isUnencryptObjectDHCheck = true;
  199. }
  200. [TestMethod]
  201. [Asynchronous]
  202. public void ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo()
  203. {
  204. isEncryptObjectPublished = false;
  205. Pubnub pubnub = new Pubnub("demo", "demo", "", "enigma", false);
  206. string channel = "my/channel";
  207. object message = new SecretCustomClass();
  208. messageObjectForEncryptPublish = JsonConvert.SerializeObject(message);
  209. PubnubUnitTest unitTest = new PubnubUnitTest();
  210. unitTest.TestClassName = "WhenAMessageIsPublished";
  211. unitTest.TestCaseName = "ThenEncryptObjectPublishShouldReturnSuccessCodeAndInfo";
  212. pubnub.PubnubUnitTest = unitTest;
  213. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessEncryptObjectPublishCodeCallback));
  214. EnqueueConditional(() => isEncryptObjectPubCheck);
  215. EnqueueCallback(() =>
  216. {
  217. if (!isEncryptObjectPublished)
  218. {
  219. Assert.IsTrue(isEncryptObjectPublished, "Encrypt Object Publish Failed");
  220. }
  221. else
  222. {
  223. EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, encryptObjectPublishTimetoken, -1, false, CaptureEncryptObjectDetailedHistoryCallback));
  224. EnqueueConditional(() => isEncryptObjectDHCheck);
  225. EnqueueCallback(() => Assert.IsTrue(isEncryptObjectDH, "Unable to match the successful encrypt object Publish"));
  226. }
  227. });
  228. EnqueueTestComplete();
  229. }
  230. [Asynchronous]
  231. public void ReturnSuccessEncryptObjectPublishCodeCallback(string result)
  232. {
  233. if (!string.IsNullOrWhiteSpace(result))
  234. {
  235. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  236. if (receivedObj is object[])
  237. {
  238. long statusCode = Int64.Parse(receivedObj[0].ToString());
  239. string statusMsg = (string)receivedObj[1];
  240. if (statusCode == 1 && statusMsg.ToLower() == "sent")
  241. {
  242. isEncryptObjectPublished = true;
  243. encryptObjectPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
  244. }
  245. }
  246. }
  247. isEncryptObjectPubCheck = true;
  248. }
  249. [Asynchronous]
  250. public void CaptureEncryptObjectDetailedHistoryCallback(string result)
  251. {
  252. if (!string.IsNullOrWhiteSpace(result))
  253. {
  254. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  255. if (receivedObj is object[])
  256. {
  257. JArray jArr = JArray.Parse(receivedObj[0].ToString());
  258. if (jArr[0].ToString(Formatting.None) == messageObjectForEncryptPublish)
  259. {
  260. isEncryptObjectDH = true;
  261. }
  262. }
  263. }
  264. isEncryptObjectDHCheck = true;
  265. }
  266. [TestMethod]
  267. [Asynchronous]
  268. public void ThenEncryptPublishShouldReturnSuccessCodeAndInfo()
  269. {
  270. isEncryptPublished = false;
  271. Pubnub pubnub = new Pubnub("demo", "demo", "", "enigma", false);
  272. string channel = "my/channel";
  273. string message = messageForEncryptPublish;
  274. PubnubUnitTest unitTest = new PubnubUnitTest();
  275. unitTest.TestClassName = "WhenAMessageIsPublished";
  276. unitTest.TestCaseName = "ThenEncryptPublishShouldReturnSuccessCodeAndInfo";
  277. pubnub.PubnubUnitTest = unitTest;
  278. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessEncryptPublishCodeCallback));
  279. EnqueueConditional(() => isEncryptPubCheck);
  280. EnqueueCallback(() =>
  281. {
  282. if (!isEncryptPublished)
  283. {
  284. Assert.IsTrue(isEncryptPublished, "Encrypt Publish Failed");
  285. }
  286. else
  287. {
  288. EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, encryptPublishTimetoken, -1, false, CaptureEncryptDetailedHistoryCallback));
  289. EnqueueConditional(() => isEncryptDHCheck);
  290. EnqueueCallback(() => Assert.IsTrue(isEncryptDH, "Unable to decrypt the successful Publish"));
  291. }
  292. });
  293. EnqueueTestComplete();
  294. }
  295. [Asynchronous]
  296. public void ReturnSuccessEncryptPublishCodeCallback(string result)
  297. {
  298. if (!string.IsNullOrWhiteSpace(result))
  299. {
  300. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  301. if (receivedObj is object[])
  302. {
  303. long statusCode = Int64.Parse(receivedObj[0].ToString());
  304. string statusMsg = (string)receivedObj[1];
  305. if (statusCode == 1 && statusMsg.ToLower() == "sent")
  306. {
  307. isEncryptPublished = true;
  308. encryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
  309. }
  310. }
  311. }
  312. isEncryptPubCheck = true;
  313. }
  314. [Asynchronous]
  315. public void CaptureEncryptDetailedHistoryCallback(string result)
  316. {
  317. if (!string.IsNullOrWhiteSpace(result))
  318. {
  319. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  320. if (receivedObj is object[])
  321. {
  322. JArray jArr = JArray.Parse(receivedObj[0].ToString());
  323. if (jArr[0].ToString() == messageForEncryptPublish)
  324. {
  325. isEncryptDH = true;
  326. }
  327. }
  328. }
  329. isEncryptDHCheck = true;
  330. }
  331. [TestMethod]
  332. [Asynchronous]
  333. public void ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo()
  334. {
  335. isSecretEncryptPublished = false;
  336. Pubnub pubnub = new Pubnub("demo", "demo", "key", "enigma", false);
  337. string channel = "my/channel";
  338. string message = messageForSecretEncryptPublish;
  339. PubnubUnitTest unitTest = new PubnubUnitTest();
  340. unitTest.TestClassName = "WhenAMessageIsPublished";
  341. unitTest.TestCaseName = "ThenSecretKeyWithEncryptPublishShouldReturnSuccessCodeAndInfo";
  342. pubnub.PubnubUnitTest = unitTest;
  343. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessSecretEncryptPublishCodeCallback));
  344. EnqueueConditional(() => isSecretEncryptPubCheck);
  345. EnqueueCallback(() =>
  346. {
  347. if (!isSecretEncryptPublished)
  348. {
  349. Assert.IsTrue(isSecretEncryptPublished, "Secret Encrypt Publish Failed");
  350. }
  351. else
  352. {
  353. EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, secretEncryptPublishTimetoken, -1, false, CaptureSecretEncryptDetailedHistoryCallback));
  354. EnqueueConditional(() => isSecretEncryptDHCheck);
  355. EnqueueCallback(() => Assert.IsTrue(isSecretEncryptDH, "Unable to decrypt the successful Secret key Publish"));
  356. }
  357. });
  358. EnqueueTestComplete();
  359. }
  360. [Asynchronous]
  361. public void ReturnSuccessSecretEncryptPublishCodeCallback(string result)
  362. {
  363. if (!string.IsNullOrWhiteSpace(result))
  364. {
  365. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  366. if (receivedObj is object[])
  367. {
  368. long statusCode = Int64.Parse(receivedObj[0].ToString());
  369. string statusMsg = (string)receivedObj[1];
  370. if (statusCode == 1 && statusMsg.ToLower() == "sent")
  371. {
  372. isSecretEncryptPublished = true;
  373. secretEncryptPublishTimetoken = Convert.ToInt64(receivedObj[2].ToString());
  374. }
  375. }
  376. }
  377. isSecretEncryptPubCheck = true;
  378. }
  379. [Asynchronous]
  380. private void CaptureSecretEncryptDetailedHistoryCallback(string result)
  381. {
  382. if (!string.IsNullOrWhiteSpace(result))
  383. {
  384. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  385. if (receivedObj is object[])
  386. {
  387. JArray jArr = JArray.Parse(receivedObj[0].ToString());
  388. if (jArr[0].ToString() == messageForSecretEncryptPublish)
  389. {
  390. isSecretEncryptDH = true;
  391. }
  392. }
  393. }
  394. isSecretEncryptDHCheck = true;
  395. }
  396. [TestMethod]
  397. public void ThenPubnubShouldGenerateUniqueIdentifier()
  398. {
  399. Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
  400. Assert.IsNotNull(pubnub.GenerateGuid());
  401. }
  402. [TestMethod]
  403. [ExpectedException(typeof(MissingFieldException))]
  404. public void ThenPublishKeyShouldNotBeEmpty()
  405. {
  406. Pubnub pubnub = new Pubnub("", "demo", "", "", false);
  407. string channel = "my/channel";
  408. string message = "Pubnub API Usage Example";
  409. pubnub.Publish<string>(channel, message, null);
  410. }
  411. [TestMethod]
  412. [Asynchronous]
  413. public void ThenOptionalSecretKeyShouldBeProvidedInConstructor()
  414. {
  415. isPublished2 = false;
  416. Pubnub pubnub = new Pubnub("demo", "demo", "key");
  417. string channel = "my/channel";
  418. string message = "Pubnub API Usage Example";
  419. PubnubUnitTest unitTest = new PubnubUnitTest();
  420. unitTest.TestClassName = "WhenAMessageIsPublished";
  421. unitTest.TestCaseName = "ThenOptionalSecretKeyShouldBeProvidedInConstructor";
  422. pubnub.PubnubUnitTest = unitTest;
  423. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSecretKeyPublishCallback));
  424. EnqueueConditional(() => isCheck2);
  425. EnqueueCallback(() => Assert.IsTrue(isPublished2, "Publish Failed with secret key"));
  426. EnqueueTestComplete();
  427. }
  428. [Asynchronous]
  429. public void ReturnSecretKeyPublishCallback(string result)
  430. {
  431. if (!string.IsNullOrWhiteSpace(result))
  432. {
  433. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  434. if (receivedObj is object[])
  435. {
  436. long statusCode = Int64.Parse(receivedObj[0].ToString());
  437. string statusMsg = (string)receivedObj[1];
  438. if (statusCode == 1 && statusMsg.ToLower() == "sent")
  439. {
  440. isPublished2 = true;
  441. }
  442. }
  443. }
  444. isCheck2 = true;
  445. }
  446. [TestMethod]
  447. [Asynchronous]
  448. public void IfSSLNotProvidedThenDefaultShouldBeFalse()
  449. {
  450. isPublished3 = false;
  451. Pubnub pubnub = new Pubnub("demo", "demo", "");
  452. string channel = "my/channel";
  453. string message = "Pubnub API Usage Example";
  454. PubnubUnitTest unitTest = new PubnubUnitTest();
  455. unitTest.TestClassName = "WhenAMessageIsPublished";
  456. unitTest.TestCaseName = "IfSSLNotProvidedThenDefaultShouldBeFalse";
  457. pubnub.PubnubUnitTest = unitTest;
  458. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnNoSSLDefaultFalseCallback));
  459. EnqueueConditional(() => isCheck3);
  460. EnqueueCallback(() => Assert.IsTrue(isPublished3, "Publish Failed with no SSL"));
  461. EnqueueTestComplete();
  462. }
  463. [Asynchronous]
  464. public void ReturnNoSSLDefaultFalseCallback(string result)
  465. {
  466. if (!string.IsNullOrWhiteSpace(result))
  467. {
  468. object[] receivedObj = JsonConvert.DeserializeObject<object[]>(result);
  469. if (receivedObj is object[])
  470. {
  471. long statusCode = Int64.Parse(receivedObj[0].ToString());
  472. string statusMsg = (string)receivedObj[1];
  473. if (statusCode == 1 && statusMsg.ToLower() == "sent")
  474. {
  475. isPublished3 = true;
  476. }
  477. }
  478. }
  479. isCheck3 = true;
  480. }
  481. [TestMethod]
  482. [Asynchronous]
  483. public void ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo()
  484. {
  485. isComplexObjectPublished = false;
  486. Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
  487. PubnubUnitTest unitTest = new PubnubUnitTest();
  488. unitTest.TestClassName = "WhenAMessageIsPublished";
  489. unitTest.TestCaseName = "ThenComplexMessageObjectShouldReturnSuccessCodeAndInfo";
  490. pubnub.PubnubUnitTest = unitTest;
  491. string channel = "my/channel";
  492. object message = new PubnubDemoObject();
  493. messageComplexObjectForPublish = JsonConvert.SerializeObject(message);
  494. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessComplexObjectPublishCodeCallback));
  495. EnqueueConditional(() => isComplexObjectPublishCheck);
  496. EnqueueCallback(() =>
  497. {
  498. if (!isComplexObjectPublished)
  499. {
  500. Assert.IsTrue(isComplexObjectPublished, "Complex Object Publish Failed");
  501. }
  502. else
  503. {
  504. EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, complexObjectPublishTimetoken, -1, false, CaptureComplexObjectDetailedHistoryCallback));
  505. EnqueueConditional(() => isComplexObjectDetailedHistoryCheck);
  506. EnqueueCallback(() => Assert.IsTrue(isComplexObjectDetailedHistory, "Unable to match the successful unencrypt object Publish"));
  507. }
  508. });
  509. EnqueueTestComplete();
  510. }
  511. [Asynchronous]
  512. private void ReturnSuccessComplexObjectPublishCodeCallback(string result)
  513. {
  514. if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
  515. {
  516. object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
  517. if (deserializedMessage is object[])
  518. {
  519. long statusCode = Int64.Parse(deserializedMessage[0].ToString());
  520. string statusMessage = (string)deserializedMessage[1];
  521. if (statusCode == 1 && statusMessage.ToLower() == "sent")
  522. {
  523. isComplexObjectPublished = true;
  524. complexObjectPublishTimetoken = Convert.ToInt64(deserializedMessage[2].ToString());
  525. }
  526. }
  527. }
  528. isComplexObjectPublishCheck = true;
  529. }
  530. [Asynchronous]
  531. private void CaptureComplexObjectDetailedHistoryCallback(string result)
  532. {
  533. if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
  534. {
  535. object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
  536. if (deserializedMessage is object[])
  537. {
  538. JArray message = deserializedMessage[0] as JArray;
  539. if (message != null && message[0].ToString(Formatting.None) == messageComplexObjectForPublish)
  540. {
  541. isComplexObjectDetailedHistory = true;
  542. }
  543. }
  544. }
  545. isComplexObjectDetailedHistoryCheck = true;
  546. }
  547. [TestMethod]
  548. [Asynchronous]
  549. public void ThenDisableJsonEncodeShouldSendSerializedObjectMessage()
  550. {
  551. isSerializedObjectMessagePublished = false;
  552. Pubnub pubnub = new Pubnub("demo", "demo", "", "", false);
  553. pubnub.EnableJsonEncodingForPublish = false;
  554. PubnubUnitTest unitTest = new PubnubUnitTest();
  555. unitTest.TestClassName = "WhenAMessageIsPublished";
  556. unitTest.TestCaseName = "ThenDisableJsonEncodeShouldSendSerializedObjectMessage";
  557. pubnub.PubnubUnitTest = unitTest;
  558. string channel = "my/channel";
  559. object message = "{\"operation\":\"ReturnData\",\"channel\":\"Mobile1\",\"sequenceNumber\":0,\"data\":[\"ping 1.0.0.1\"]}";
  560. serializedObjectMessageForPublish = message.ToString();
  561. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnSuccessSerializedObjectMessageForPublishCallback));
  562. EnqueueConditional(() => isSerializedObjectMessageCheck);
  563. EnqueueCallback(() =>
  564. {
  565. if (!isSerializedObjectMessagePublished)
  566. {
  567. EnqueueCallback(() => Assert.IsTrue(isSerializedObjectMessagePublished, "Serialized Object Message Publish Failed"));
  568. }
  569. else
  570. {
  571. EnqueueCallback(() => pubnub.DetailedHistory<string>(channel, -1, serializedMessagePublishTimetoken, -1, false, CaptureSerializedMessagePublishDetailedHistoryCallback));
  572. EnqueueConditional(() => isSerializedMessageDetailedHistoryCheck);
  573. EnqueueCallback(() => Assert.IsTrue(isSerializedObjectMessageDetailedHistory, "Unable to match the successful serialized object message Publish"));
  574. }
  575. });
  576. EnqueueTestComplete();
  577. }
  578. [Asynchronous]
  579. private void ReturnSuccessSerializedObjectMessageForPublishCallback(string result)
  580. {
  581. if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
  582. {
  583. object[] deserializedResult = JsonConvert.DeserializeObject<object[]>(result);
  584. if (deserializedResult is object[])
  585. {
  586. long statusCode = Int64.Parse(deserializedResult[0].ToString());
  587. string statusMessage = (string)deserializedResult[1];
  588. if (statusCode == 1 && statusMessage.ToLower() == "sent")
  589. {
  590. isSerializedObjectMessagePublished = true;
  591. serializedMessagePublishTimetoken = Convert.ToInt64(deserializedResult[2].ToString());
  592. }
  593. }
  594. }
  595. isSerializedObjectMessageCheck = true;
  596. }
  597. [Asynchronous]
  598. private void CaptureSerializedMessagePublishDetailedHistoryCallback(string result)
  599. {
  600. if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
  601. {
  602. object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
  603. if (deserializedMessage is object[])
  604. {
  605. JArray message = deserializedMessage[0] as JArray;
  606. if (message != null && message[0].ToString(Formatting.None) == serializedObjectMessageForPublish)
  607. {
  608. isSerializedObjectMessageDetailedHistory = true;
  609. }
  610. }
  611. }
  612. isSerializedMessageDetailedHistoryCheck = true;
  613. }
  614. [TestMethod]
  615. [Asynchronous]
  616. public void ThenLargeMessageShoudFailWithMessageTooLargeInfo()
  617. {
  618. isLargeMessagePublished = false;
  619. Pubnub pubnub = new Pubnub("demo", "demo", "", "", true);
  620. PubnubUnitTest unitTest = new PubnubUnitTest();
  621. unitTest.TestClassName = "WhenAMessageIsPublished";
  622. unitTest.TestCaseName = "ThenLargeMessageShoudFailWithMessageTooLargeInfo";
  623. pubnub.PubnubUnitTest = unitTest;
  624. string channel = "my/channel";
  625. string message = messageLarge2K.Substring(0,1320);
  626. EnqueueCallback(() => pubnub.Publish<string>(channel, message, ReturnPublishMessageTooLargeInfoCallback));
  627. EnqueueConditional(() => isPublishMessageTooLargeCheck);
  628. EnqueueCallback(() => Assert.IsTrue(isLargeMessagePublished, "Message Too Large is not failing as expected."));
  629. EnqueueTestComplete();
  630. }
  631. [Asynchronous]
  632. private void ReturnPublishMessageTooLargeInfoCallback(string result)
  633. {
  634. if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
  635. {
  636. object[] deserializedMessage = JsonConvert.DeserializeObject<object[]>(result);
  637. if (deserializedMessage is object[])
  638. {
  639. long statusCode = Int64.Parse(deserializedMessage[0].ToString());
  640. string statusMessage = (string)deserializedMessage[1];
  641. if (statusCode == 0 && statusMessage.ToLower() == "message too large")
  642. {
  643. isLargeMessagePublished = true;
  644. }
  645. }
  646. }
  647. isPublishMessageTooLargeCheck = true;
  648. }
  649. }
  650. }