| 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 |
|
| 15 |
goog.provide('goog.net.CrossDomainRpcTest'); |
| 16 |
goog.setTestOnly('goog.net.CrossDomainRpcTest'); |
| 17 |
|
| 18 |
goog.require('goog.Promise'); |
| 19 |
goog.require('goog.log'); |
| 20 |
goog.require('goog.net.CrossDomainRpc'); |
| 21 |
goog.require('goog.testing.TestCase'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.userAgent'); |
| 24 |
goog.require('goog.userAgent.product'); |
| 25 |
|
| 26 |
function setUpPage() { |
| 27 |
goog.testing.TestCase.getActiveTestCase().promiseTimeout = 20000; // 20s |
| 28 |
} |
| 29 |
|
| 30 |
function print(o) { |
| 31 |
if (Object.prototype.toSource) { |
| 32 |
return o.toSource(); |
| 33 |
} else { |
| 34 |
var fragments = []; |
| 35 |
fragments.push('{'); |
| 36 |
var first = true; |
| 37 |
for (var p in o) { |
| 38 |
if (!first) fragments.push(','); |
| 39 |
fragments.push(p); |
| 40 |
fragments.push(':"'); |
| 41 |
fragments.push(o[p]); |
| 42 |
fragments.push('"'); |
| 43 |
first = false; |
| 44 |
} |
| 45 |
return fragments.join(''); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
function testNormalRequest() { |
| 51 |
var start = goog.now(); |
| 52 |
return new goog |
| 53 |
.Promise(function(resolve, reject) { |
| 54 |
goog.net.CrossDomainRpc.send( |
| 55 |
'crossdomainrpc_test_response.html', resolve, 'POST', |
| 56 |
{xyz: '01234567891123456789'}); |
| 57 |
}) |
| 58 |
.then(function(e) { |
| 59 |
if (e.target.status < 300) { |
| 60 |
var elapsed = goog.now() - start; |
| 61 |
var responseData = eval(e.target.responseText); |
| 62 |
goog.log.fine( |
| 63 |
goog.net.CrossDomainRpc.logger_, elapsed + 'ms: [' + |
| 64 |
responseData.result.length + '] ' + print(responseData)); |
| 65 |
assertEquals(16 * 1024, responseData.result.length); |
| 66 |
assertEquals(123, e.target.status); |
| 67 |
assertEquals(1, e.target.responseHeaders.a); |
| 68 |
assertEquals('2', e.target.responseHeaders.b); |
| 69 |
} else { |
| 70 |
goog.log.fine(goog.net.CrossDomainRpc.logger_, print(e)); |
| 71 |
fail(); |
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
function testErrorRequest() { |
| 78 |
// Firefox and Safari do not give a valid error event. |
| 79 |
if (goog.userAgent.GECKO || goog.userAgent.product.SAFARI) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
return new goog |
| 84 |
.Promise(function(resolve, reject) { |
| 85 |
goog.net.CrossDomainRpc.send( |
| 86 |
'http://hoodjimcwaadji.google.com/index.html', resolve, 'POST', |
| 87 |
{xyz: '01234567891123456789'}); |
| 88 |
setTimeout(function() { |
| 89 |
reject('CrossDomainRpc.send did not complete within 4000ms'); |
| 90 |
}, 4000); |
| 91 |
}) |
| 92 |
.then(function(e) { |
| 93 |
if (e.target.status < 300) { |
| 94 |
fail('should have failed requesting a non-existent URI'); |
| 95 |
} else { |
| 96 |
goog.log.fine( |
| 97 |
goog.net.CrossDomainRpc.logger_, |
| 98 |
'expected error seen; event=' + print(e)); |
| 99 |
} |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
function testGetDummyResourceUri() { |
| 105 |
var url = goog.net.CrossDomainRpc.getDummyResourceUri_(); |
| 106 |
assertTrue('dummy resource URL should not contain "?"', url.indexOf('?') < 0); |
| 107 |
assertTrue('dummy resource URL should not contain "#"', url.indexOf('#') < 0); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
function testRemoveHash() { |
| 112 |
assertEquals('abc', goog.net.CrossDomainRpc.removeHash_('abc#123')); |
| 113 |
assertEquals('abc', goog.net.CrossDomainRpc.removeHash_('abc#12#3')); |
| 114 |
} |