crossdomainrpc_test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.net.CrossDomainRpcTest');
  15. goog.setTestOnly('goog.net.CrossDomainRpcTest');
  16. goog.require('goog.Promise');
  17. goog.require('goog.log');
  18. goog.require('goog.net.CrossDomainRpc');
  19. goog.require('goog.testing.TestCase');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.userAgent');
  22. goog.require('goog.userAgent.product');
  23. function setUpPage() {
  24. goog.testing.TestCase.getActiveTestCase().promiseTimeout = 20000; // 20s
  25. }
  26. function print(o) {
  27. if (Object.prototype.toSource) {
  28. return o.toSource();
  29. } else {
  30. var fragments = [];
  31. fragments.push('{');
  32. var first = true;
  33. for (var p in o) {
  34. if (!first) fragments.push(',');
  35. fragments.push(p);
  36. fragments.push(':"');
  37. fragments.push(o[p]);
  38. fragments.push('"');
  39. first = false;
  40. }
  41. return fragments.join('');
  42. }
  43. }
  44. function testNormalRequest() {
  45. var start = goog.now();
  46. return new goog
  47. .Promise(function(resolve, reject) {
  48. goog.net.CrossDomainRpc.send(
  49. 'crossdomainrpc_test_response.html', resolve, 'POST',
  50. {xyz: '01234567891123456789'});
  51. })
  52. .then(function(e) {
  53. if (e.target.status < 300) {
  54. var elapsed = goog.now() - start;
  55. var responseData = eval(e.target.responseText);
  56. goog.log.fine(
  57. goog.net.CrossDomainRpc.logger_, elapsed + 'ms: [' +
  58. responseData.result.length + '] ' + print(responseData));
  59. assertEquals(16 * 1024, responseData.result.length);
  60. assertEquals(123, e.target.status);
  61. assertEquals(1, e.target.responseHeaders.a);
  62. assertEquals('2', e.target.responseHeaders.b);
  63. } else {
  64. goog.log.fine(goog.net.CrossDomainRpc.logger_, print(e));
  65. fail();
  66. }
  67. });
  68. }
  69. function testErrorRequest() {
  70. // Firefox and Safari do not give a valid error event.
  71. if (goog.userAgent.GECKO || goog.userAgent.product.SAFARI) {
  72. return;
  73. }
  74. return new goog
  75. .Promise(function(resolve, reject) {
  76. goog.net.CrossDomainRpc.send(
  77. 'http://hoodjimcwaadji.google.com/index.html', resolve, 'POST',
  78. {xyz: '01234567891123456789'});
  79. setTimeout(function() {
  80. reject('CrossDomainRpc.send did not complete within 4000ms');
  81. }, 4000);
  82. })
  83. .then(function(e) {
  84. if (e.target.status < 300) {
  85. fail('should have failed requesting a non-existent URI');
  86. } else {
  87. goog.log.fine(
  88. goog.net.CrossDomainRpc.logger_,
  89. 'expected error seen; event=' + print(e));
  90. }
  91. });
  92. }
  93. function testGetDummyResourceUri() {
  94. var url = goog.net.CrossDomainRpc.getDummyResourceUri_();
  95. assertTrue('dummy resource URL should not contain "?"', url.indexOf('?') < 0);
  96. assertTrue('dummy resource URL should not contain "#"', url.indexOf('#') < 0);
  97. }
  98. function testRemoveHash() {
  99. assertEquals('abc', goog.net.CrossDomainRpc.removeHash_('abc#123'));
  100. assertEquals('abc', goog.net.CrossDomainRpc.removeHash_('abc#12#3'));
  101. }