OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 --> |
| 6 |
| 7 <!doctype html> |
| 8 <title>rpc-client</title> |
| 9 |
| 10 <link rel="import" href="../rpc-client.html"> |
| 11 <script src="../../bower_components/web-component-tester/browser.js"></script> |
| 12 |
| 13 <test-fixture id="client"> |
| 14 <template> |
| 15 <rpc-client service="service" method="ok"></rpc-client> |
| 16 </template> |
| 17 </test-fixture> |
| 18 <script> |
| 19 'use strict'; |
| 20 |
| 21 suite('<rpc-client>', function() { |
| 22 var client; |
| 23 var server; |
| 24 |
| 25 setup(function() { |
| 26 var prefix = ')]}\'\n'; |
| 27 |
| 28 server = sinon.fakeServer.create(); |
| 29 var okResponseHeaders = { |
| 30 'Content-Type': 'application/json', |
| 31 'X-Prpc-Grpc-Code': '' + luci.rpc.Code.OK |
| 32 } |
| 33 server.respondWith('POST', '/prpc/service/ok', [ |
| 34 200, |
| 35 okResponseHeaders, |
| 36 prefix + '{"secure": false}' |
| 37 ]); |
| 38 server.respondWith( |
| 39 'POST', |
| 40 'https://' + document.location.host + '/prpc/service/ok', |
| 41 [ |
| 42 200, |
| 43 okResponseHeaders, |
| 44 prefix + '{"secure": true}' |
| 45 ]) |
| 46 |
| 47 server.respondWith('POST', '/prpc/service/notFound', [ |
| 48 404, |
| 49 { |
| 50 'Content-Type': 'text/plain', |
| 51 'X-Prpc-Grpc-Code': '' + luci.rpc.Code.NOT_FOUND |
| 52 }, |
| 53 'Not found' |
| 54 ]); |
| 55 |
| 56 server.respondWith('POST', '/prpc/service/noCode', [ |
| 57 200, |
| 58 { |
| 59 'Content-Type': 'text/json', |
| 60 }, |
| 61 '{"blah": "bleh"}' |
| 62 ]); |
| 63 |
| 64 server.respondWith('POST', '/prpc/service/invalidCode', [ |
| 65 200, |
| 66 { |
| 67 'Content-Type': 'text/json', |
| 68 'X-Prpc-Grpc-Code': 'abc' |
| 69 }, |
| 70 '{"blah": "bleh"}' |
| 71 ]); |
| 72 |
| 73 server.respondWith('POST', '/prpc/service/malformedResponse', [ |
| 74 200, |
| 75 { |
| 76 'Content-Type': 'text/json', |
| 77 'X-Prpc-Grpc-Code': '' + luci.rpc.Code.OK |
| 78 }, |
| 79 '}}}' |
| 80 ]); |
| 81 |
| 82 client = fixture('client'); |
| 83 }); |
| 84 |
| 85 teardown(function() { |
| 86 server.restore(); |
| 87 }); |
| 88 |
| 89 function expectResponse(fn) { |
| 90 var call = client.call(); |
| 91 server.respond(); |
| 92 return call.completes.then(function () { |
| 93 expect(client.lastError).not.to.be.error; |
| 94 expect(client.lastResponse).to.equal(call.response); |
| 95 fn(call); |
| 96 }); |
| 97 } |
| 98 |
| 99 function expectError(fn) { |
| 100 var call = client.call(); |
| 101 server.respond(); |
| 102 return call.completes.then(function() { |
| 103 assert.fail('no error', 'error'); |
| 104 }).catch(function (error) { |
| 105 expect(error).not.to.be.null; |
| 106 expect(client.lastResponse).to.be.null; |
| 107 expect(client.lastError).to.be.equal(error); |
| 108 fn(error); |
| 109 }); |
| 110 } |
| 111 |
| 112 test('by default host is current', function() { |
| 113 expect(client.host).to.be.equal(document.location.host); |
| 114 }); |
| 115 |
| 116 test('with a request', function() { |
| 117 client.request = { msg: 'hi' }; |
| 118 var call = client.call(); |
| 119 expect(call.xhr.requestBody).to.equal('{"msg":"hi"}'); |
| 120 }); |
| 121 |
| 122 test('loading property changes', function() { |
| 123 expect(client.loading).to.be.false; |
| 124 |
| 125 var call = client.call(); |
| 126 expect(client.loading).to.be.true; |
| 127 |
| 128 server.respond(); |
| 129 return call.completes.then(function() { |
| 130 expect(client.loading).to.be.false; |
| 131 }); |
| 132 }); |
| 133 |
| 134 test('reads response', function() { |
| 135 return expectResponse(function(call) { |
| 136 expect(call.response).to.be.deep.equal({ secure: false }); |
| 137 expect(call.code).to.equal(luci.rpc.Code.OK); |
| 138 }); |
| 139 }); |
| 140 |
| 141 test('sets code', function() { |
| 142 return expectResponse(function(call) { |
| 143 expect(call.code).to.equal(luci.rpc.Code.OK); |
| 144 expect(client.lastCode).to.equal(call.code); |
| 145 }); |
| 146 }); |
| 147 |
| 148 test('secure', function() { |
| 149 client.insecure = false; |
| 150 return expectResponse(function(call) { |
| 151 expect(call.response).to.be.deep.equal({ secure: true }); |
| 152 }); |
| 153 }); |
| 154 |
| 155 test('with timeout', function() { |
| 156 client.timeout = 1000; |
| 157 var call = client.call(); |
| 158 var timeoutHeader = call.xhr.requestHeaders['x-prpc-timeout']; |
| 159 expect(timeoutHeader).to.be.equal('1000m'); |
| 160 }); |
| 161 |
| 162 test('error code', function() { |
| 163 client.method = 'notFound'; |
| 164 return expectError(function(e) { |
| 165 expect(e).to.be.an.instanceof(luci.rpc.GrpcError); |
| 166 expect(e.code).to.equal(luci.rpc.Code.NOT_FOUND); |
| 167 expect(e.description).to.equal('Not found'); |
| 168 expect(client.lastCall.code).to.equal(luci.rpc.Code.NOT_FOUND); |
| 169 expect(client.lastCode).to.equal(luci.rpc.Code.NOT_FOUND); |
| 170 }); |
| 171 }); |
| 172 |
| 173 test('no code', function() { |
| 174 client.method = 'noCode'; |
| 175 return expectError(function(e) { |
| 176 expect(e.message).to.contain('no X-Prpc-Grpc-Code'); |
| 177 }); |
| 178 }); |
| 179 |
| 180 test('malformed response', function() { |
| 181 client.method = 'malformedResponse'; |
| 182 return expectError(function(e) { |
| 183 expect(e.message).to.contain('could not parse response'); |
| 184 }); |
| 185 }); |
| 186 |
| 187 test('invalid code', function() { |
| 188 client.method = 'invalidCode'; |
| 189 return expectError(function(e) { |
| 190 expect(e.message).to.contain('Invalid X-Prpc-Grpc-Code'); |
| 191 }); |
| 192 }); |
| 193 |
| 194 test('request event', function() { |
| 195 client.request = { msg: 'hi' }; |
| 196 var event = null; |
| 197 client.addEventListener('request', function(e) { |
| 198 event = e; |
| 199 }); |
| 200 expect(event).to.be.null; |
| 201 var call = client.call(); |
| 202 expect(event).to.be.ok |
| 203 expect(event.detail.call).to.equal(call); |
| 204 expect(client.lastCall).to.equal(call); |
| 205 expect(call.host).to.equal(document.location.host); |
| 206 expect(call.service).to.equal('service'); |
| 207 expect(call.method).to.equal('ok'); |
| 208 expect(call.request).to.equal(client.request); |
| 209 }); |
| 210 |
| 211 test('response event', function() { |
| 212 var event = null; |
| 213 client.addEventListener('response', function(e) { |
| 214 event = e; |
| 215 }); |
| 216 expect(event).to.be.null; |
| 217 return expectResponse(function(call) { |
| 218 expect(event).to.be.ok |
| 219 expect(event.detail.call).to.equal(call); |
| 220 expect(call.host).to.equal(document.location.host); |
| 221 expect(call.service).to.equal('service'); |
| 222 expect(call.method).to.equal('ok'); |
| 223 expect(call.request).to.equal(client.request); |
| 224 |
| 225 expect(call.response).to.deep.equal({secure: false}); |
| 226 expect(call.error).to.be.null; |
| 227 }); |
| 228 }); |
| 229 |
| 230 test('error event', function() { |
| 231 client.method = 'notFound'; |
| 232 var event = null; |
| 233 client.addEventListener('error', function(e) { |
| 234 event = e; |
| 235 }); |
| 236 expect(event).to.be.null; |
| 237 var call = client.call(); |
| 238 expect(event).to.be.null; |
| 239 |
| 240 server.respond(); |
| 241 |
| 242 return expectError(function(call) { |
| 243 var call = client.lastCall; |
| 244 expect(event).to.be.ok |
| 245 expect(event.detail.call).to.equal(call); |
| 246 expect(call.host).to.equal(document.location.host); |
| 247 expect(call.service).to.equal('service'); |
| 248 expect(call.method).to.equal('notFound'); |
| 249 expect(call.request).to.equal(client.request); |
| 250 |
| 251 expect(call.response).to.be.null; |
| 252 expect(call.error).to.be.an.instanceof(luci.rpc.GrpcError); |
| 253 }); |
| 254 }); |
| 255 }); |
| 256 </script> |
OLD | NEW |