/tests/Microsoft.AspNet.SignalR.Client.JS.Tests/Tests/FunctionalTests/Common/PingServerFacts.js

https://gitlab.com/scgitlab/dsdsds · JavaScript · 81 lines · 56 code · 11 blank · 14 comment · 2 complexity · 78c15e3b964ccf25bbaab45bf479b90f MD5 · raw file

  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. QUnit.module("Transports Common - Ping Server Facts");
  4. testUtilities.runWithAllTransports(function (transport) {
  5. QUnit.asyncTimeoutTest(transport + " transport can initiate Ping Server.", testUtilities.defaultTestTimeout, function (end, assert, testName) {
  6. var connection = testUtilities.createHubConnection(end, assert, testName),
  7. testPingServer = function () {
  8. $.signalR.transports._logic.pingServer(connection).done(function () {
  9. // Successful ping
  10. assert.ok(true, "Successful ping with " + transport);
  11. end();
  12. }).fail(function () {
  13. assert.ok(false, "Failed to ping server with " + transport);
  14. end();
  15. });
  16. };
  17. // Starting/Stopping a connection to have it instantiated with all the appropriate variables
  18. connection.start({ transport: transport }).done(function () {
  19. assert.ok(true, "Connected");
  20. testPingServer();
  21. });
  22. // Cleanup
  23. return function () {
  24. connection.stop();
  25. };
  26. });
  27. QUnit.asyncTimeoutTest(transport + " transport calls Ping Server with custom query string in url", testUtilities.defaultTestTimeout, function (end, assert, testName) {
  28. var connection = testUtilities.createHubConnection(end, assert, testName),
  29. expectedQs = window.encodeURIComponent(testName),
  30. savedAjax = $.ajax,
  31. testPingServer = function () {
  32. $.signalR.transports._logic.pingServer(connection).done(function () {
  33. // Successful ping
  34. assert.ok(true, "Successful ping with " + transport);
  35. }).fail(function () {
  36. assert.ok(false, "Failed to ping server with " + transport);
  37. end();
  38. });
  39. };
  40. // Verify that the query string parameter was set
  41. assert.ok(connection.qs.indexOf(expectedQs) >= 0, "Query string contains the test name prior to ping server.");
  42. // For long polling this ajax request will execute before testPingServer because longPolling
  43. // utilizes the pingServer method.
  44. function ajaxReplacement(url, settings) {
  45. if (!settings) {
  46. settings = url;
  47. url = settings.url;
  48. }
  49. // Check if it's the ping request;
  50. if (url.indexOf("/ping") >= 0) {
  51. // Verify that the query string parameter on the connection is passed via the ajax request
  52. assert.ok(url.indexOf(expectedQs) >= 0, "Query string parameters were passed in ping server");
  53. // Let the ajax request finish out
  54. setTimeout(end, 0);
  55. }
  56. // Persist the request through to the original ajax request
  57. return savedAjax.call(this, url, settings);
  58. };
  59. // Starting/Stopping a connection to have it instantiated with all the appropriate variables
  60. connection.start({ transport: transport }).done(function () {
  61. assert.ok(true, "Connected");
  62. $.ajax = ajaxReplacement;
  63. testPingServer();
  64. });
  65. return function () {
  66. $.ajax = savedAjax;
  67. connection.stop();
  68. };
  69. });
  70. });