| 1 |
// Copyright 2015 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.streams.XhrStreamReaderTest'); |
| 16 |
goog.setTestOnly('goog.net.streams.XhrStreamReaderTest'); |
| 17 |
|
| 18 |
goog.require('goog.net.ErrorCode'); |
| 19 |
goog.require('goog.net.XmlHttp'); |
| 20 |
goog.require('goog.net.streams.Base64PbStreamParser'); |
| 21 |
goog.require('goog.net.streams.JsonStreamParser'); |
| 22 |
goog.require('goog.net.streams.PbJsonStreamParser'); |
| 23 |
goog.require('goog.net.streams.PbStreamParser'); |
| 24 |
goog.require('goog.net.streams.XhrStreamReader'); |
| 25 |
goog.require('goog.object'); |
| 26 |
goog.require('goog.testing.asserts'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
goog.require('goog.testing.net.XhrIo'); |
| 29 |
|
| 30 |
|
| 31 |
var Base64PbStreamParser = |
| 32 |
goog.module.get('goog.net.streams.Base64PbStreamParser'); |
| 33 |
var PbJsonStreamParser = goog.module.get('goog.net.streams.PbJsonStreamParser'); |
| 34 |
|
| 35 |
var xhrReader; |
| 36 |
var xhrIo; |
| 37 |
|
| 38 |
var Status = goog.net.streams.XhrStreamReader.Status; |
| 39 |
var ReadyState = goog.net.XmlHttp.ReadyState; |
| 40 |
|
| 41 |
var CONTENT_TYPE_HEADER = goog.net.XhrIo.CONTENT_TYPE_HEADER; |
| 42 |
var CONTENT_TRANSFER_ENCODING = goog.net.XhrIo.CONTENT_TRANSFER_ENCODING; |
| 43 |
|
| 44 |
|
| 45 |
function shouldRunTests() { |
| 46 |
return goog.net.streams.XhrStreamReader.isStreamingSupported(); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
function setUp() { |
| 51 |
xhrIo = new goog.testing.net.XhrIo(); |
| 52 |
xhrReader = new goog.net.streams.XhrStreamReader(xhrIo); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
function tearDown() { |
| 57 |
xhrIo = null; |
| 58 |
xhrReader = null; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
function testGetParserByResponseHeader() { |
| 63 |
xhrIo.getStreamingResponseHeader = function() { return null; }; |
| 64 |
assertNull(xhrReader.getParserByResponseHeader_()); |
| 65 |
|
| 66 |
xhrIo.getStreamingResponseHeader = function() { return ''; }; |
| 67 |
assertNull(xhrReader.getParserByResponseHeader_()); |
| 68 |
|
| 69 |
xhrIo.getStreamingResponseHeader = function() { return 'xxxxx'; }; |
| 70 |
assertNull(xhrReader.getParserByResponseHeader_()); |
| 71 |
|
| 72 |
xhrIo.getStreamingResponseHeader = function(key) { |
| 73 |
if (key == CONTENT_TYPE_HEADER) return 'application/json'; |
| 74 |
return null; |
| 75 |
}; |
| 76 |
assertTrue( |
| 77 |
xhrReader.getParserByResponseHeader_() instanceof |
| 78 |
goog.net.streams.JsonStreamParser); |
| 79 |
|
| 80 |
xhrIo.getStreamingResponseHeader = function(key) { |
| 81 |
if (key == CONTENT_TYPE_HEADER) return 'application/json; charset=utf-8'; |
| 82 |
return null; |
| 83 |
}; |
| 84 |
assertTrue( |
| 85 |
xhrReader.getParserByResponseHeader_() instanceof |
| 86 |
goog.net.streams.JsonStreamParser); |
| 87 |
|
| 88 |
xhrIo.getStreamingResponseHeader = function(key) { |
| 89 |
if (key == CONTENT_TYPE_HEADER) return 'application/x-protobuf'; |
| 90 |
return null; |
| 91 |
}; |
| 92 |
assertTrue( |
| 93 |
xhrReader.getParserByResponseHeader_() instanceof |
| 94 |
goog.net.streams.PbStreamParser); |
| 95 |
|
| 96 |
xhrIo.getStreamingResponseHeader = function(key) { |
| 97 |
if (key == CONTENT_TYPE_HEADER) return 'application/x-protobuf'; |
| 98 |
if (key == CONTENT_TRANSFER_ENCODING) return 'BASE64'; |
| 99 |
return null; |
| 100 |
}; |
| 101 |
assertTrue( |
| 102 |
xhrReader.getParserByResponseHeader_() instanceof Base64PbStreamParser); |
| 103 |
|
| 104 |
xhrIo.getStreamingResponseHeader = function(key) { |
| 105 |
if (key == CONTENT_TYPE_HEADER) return 'application/json+protobuf'; |
| 106 |
return null; |
| 107 |
}; |
| 108 |
assertTrue( |
| 109 |
xhrReader.getParserByResponseHeader_() instanceof PbJsonStreamParser); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
function testNoData() { |
| 114 |
xhrReader.setDataHandler(function(messages) { |
| 115 |
fail('Received unexpected messages: ' + messages); |
| 116 |
}); |
| 117 |
|
| 118 |
var streamStatus = []; |
| 119 |
var httpStatus = []; |
| 120 |
xhrReader.setStatusHandler(function() { |
| 121 |
streamStatus.push(xhrReader.getStatus()); |
| 122 |
httpStatus.push(xhrIo.getStatus()); |
| 123 |
}); |
| 124 |
|
| 125 |
var headers = {'Content-Type': 'application/json'}; |
| 126 |
xhrIo.send('/foo/bar'); |
| 127 |
xhrIo.simulateResponse(goog.net.HttpStatus.OK, '', headers); |
| 128 |
|
| 129 |
assertElementsEquals([Status.NO_DATA], streamStatus); |
| 130 |
assertElementsEquals([goog.net.HttpStatus.OK], httpStatus); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
function testRetrieveHttpStatusInStatusHandler() { |
| 135 |
var received = []; |
| 136 |
xhrReader.setDataHandler(function(messages) { |
| 137 |
received.push(messages); |
| 138 |
}); |
| 139 |
|
| 140 |
var streamStatus = []; |
| 141 |
var httpStatus = []; |
| 142 |
xhrReader.setStatusHandler(function() { |
| 143 |
console.log('in setStatusHandler'); |
| 144 |
streamStatus.push(xhrReader.getStatus()); |
| 145 |
httpStatus.push(xhrIo.getStatus()); |
| 146 |
}); |
| 147 |
|
| 148 |
var headers = {'Content-Type': 'application/json'}; |
| 149 |
xhrIo.send('/foo/bar'); |
| 150 |
xhrIo.simulateResponse(goog.net.HttpStatus.OK, '[{"1" : "b"}]', headers); |
| 151 |
|
| 152 |
assertEquals(1, received.length); |
| 153 |
assertEquals(1, received[0].length); |
| 154 |
assertElementsEquals(['1'], goog.object.getKeys(received[0][0])); |
| 155 |
assertElementsEquals('b', received[0][0][1]); |
| 156 |
|
| 157 |
assertElementsEquals([Status.ACTIVE, Status.SUCCESS], streamStatus); |
| 158 |
assertElementsEquals( |
| 159 |
[goog.net.HttpStatus.OK, goog.net.HttpStatus.OK], httpStatus); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
function testParsingSingleMessage() { |
| 164 |
var received = []; |
| 165 |
xhrReader.setDataHandler(function(messages) { received.push(messages); }); |
| 166 |
|
| 167 |
var body = 'CgX__gABdw=='; |
| 168 |
var headers = { |
| 169 |
'Content-Type': 'application/x-protobuf', |
| 170 |
'Content-Transfer-Encoding': 'BASE64' |
| 171 |
}; |
| 172 |
xhrIo.send('/foo/bar'); |
| 173 |
xhrIo.simulateResponse(goog.net.HttpStatus.OK, body, headers); |
| 174 |
|
| 175 |
assertEquals(1, received.length); |
| 176 |
assertEquals(1, received[0].length); |
| 177 |
assertElementsEquals(['1'], goog.object.getKeys(received[0][0])); |
| 178 |
assertElementsEquals([0xFF, 0xFE, 0x00, 0x01, 0x77], received[0][0][1]); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
function testParsingMultipleMessages() { |
| 183 |
/** |
| 184 |
* Pass the following protobuf messages |
| 185 |
* 0x0a, 0x03, 0x61, 0x62, 0x63, |
| 186 |
* 0x0a, 0x03, 0x64, 0x65, 0x66, |
| 187 |
* 0x12, 0x03, 0x67, 0x68, 0x69, |
| 188 |
* 0x0a, 0x03, 0x6a, 0x6b, 0x6c, |
| 189 |
*/ |
| 190 |
|
| 191 |
var chunk1 = 'CgNh'; |
| 192 |
var chunk2 = 'YmMKA'; |
| 193 |
var chunk3 = '2RlZhIDZ2hpCg'; |
| 194 |
var chunk4 = 'Nqa2w='; |
| 195 |
|
| 196 |
var received = []; |
| 197 |
xhrReader.setDataHandler(function(messages) { received.push(messages); }); |
| 198 |
|
| 199 |
var headers = { |
| 200 |
'Content-Type': 'application/x-protobuf', |
| 201 |
'Content-Transfer-Encoding': 'BASE64' |
| 202 |
}; |
| 203 |
|
| 204 |
xhrIo.send('/foo/bar'); |
| 205 |
|
| 206 |
xhrIo.simulatePartialResponse(chunk1, headers); |
| 207 |
assertEquals(0, received.length); |
| 208 |
|
| 209 |
xhrIo.simulatePartialResponse(chunk2); |
| 210 |
assertEquals(1, received.length); |
| 211 |
assertEquals(1, received[0].length); |
| 212 |
assertElementsEquals(['1'], goog.object.getKeys(received[0][0])); |
| 213 |
assertElementsEquals([0x61, 0x62, 0x63], received[0][0][1]); |
| 214 |
|
| 215 |
received = []; |
| 216 |
xhrIo.simulatePartialResponse(chunk3); |
| 217 |
assertEquals(1, received.length); |
| 218 |
assertEquals(2, received[0].length); |
| 219 |
assertElementsEquals(['1'], goog.object.getKeys(received[0][0])); |
| 220 |
assertElementsEquals([0x64, 0x65, 0x66], received[0][0][1]); |
| 221 |
assertElementsEquals(['2'], goog.object.getKeys(received[0][1])); |
| 222 |
assertElementsEquals([0x67, 0x68, 0x69], received[0][1][2]); |
| 223 |
|
| 224 |
received = []; |
| 225 |
xhrIo.simulatePartialResponse(chunk4); |
| 226 |
assertEquals(1, received.length); |
| 227 |
assertEquals(1, received[0].length); |
| 228 |
assertElementsEquals(['1'], goog.object.getKeys(received[0][0])); |
| 229 |
assertElementsEquals([0x6a, 0x6b, 0x6c], received[0][0][1]); |
| 230 |
|
| 231 |
xhrIo.simulateReadyStateChange(ReadyState.COMPLETE); |
| 232 |
assertEquals(Status.SUCCESS, xhrReader.getStatus()); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
function testXhrTimeout() { |
| 237 |
xhrIo.send('/test', null, 'GET', null, null, 1000, false); |
| 238 |
|
| 239 |
xhrIo.simulatePartialResponse('', {'Content-Type': 'application/x-protobuf'}); |
| 240 |
xhrIo.getLastErrorCode = function() { return goog.net.ErrorCode.TIMEOUT; }; |
| 241 |
xhrIo.simulateReadyStateChange(ReadyState.COMPLETE); |
| 242 |
|
| 243 |
assertEquals(Status.TIMEOUT, xhrReader.getStatus()); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
// TODO: more mocked/networked tests, testing.xhrio not useful for streaming |
| 248 |
// states |