|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -/* xslint-disable @typescript-eslint/ban-ts-comment */ |
18 | | -/* xslint-disable no-undef */ |
19 | | - |
20 | 17 | import * as assert from 'assert'; |
21 | 18 | import {describe, it, afterEach, before} from 'mocha'; |
22 | 19 | import * as nodeFetch from 'node-fetch'; |
23 | 20 | import * as protobuf from 'protobufjs'; |
24 | 21 | import * as path from 'path'; |
25 | 22 | import * as sinon from 'sinon'; |
| 23 | +import * as stream from 'stream'; |
26 | 24 | import echoProtoJson = require('../fixtures/echo.json'); |
27 | 25 | import {GrpcClient} from '../../src/fallback'; |
28 | 26 | import * as transcoding from '../../src/transcoding'; |
29 | 27 | import {OAuth2Client} from 'google-auth-library'; |
30 | 28 | import {GrpcClientOptions} from '../../src'; |
| 29 | +import {StreamArrayParser} from '../../src/streamArrayParser'; |
31 | 30 |
|
32 | 31 | const authClient = { |
33 | 32 | async getRequestHeaders() { |
@@ -112,6 +111,75 @@ describe('REGAPIC', () => { |
112 | 111 | }); |
113 | 112 | }); |
114 | 113 |
|
| 114 | + it('should make a streaming request', done => { |
| 115 | + const requestObject = {content: 'test content'}; |
| 116 | + const responseObject = [{content: 'test'}, {content: 'content'}]; |
| 117 | + const responseObjectJson = JSON.stringify(responseObject, null, ' '); |
| 118 | + const responseStream = new stream.Readable(); |
| 119 | + responseStream.push(responseObjectJson.slice(0, 10)); |
| 120 | + responseStream.push(responseObjectJson.slice(10)); |
| 121 | + responseStream.push(null); |
| 122 | + // incomplete types for nodeFetch, so... |
| 123 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 124 | + sinon.stub(nodeFetch, 'Promise' as any).returns( |
| 125 | + Promise.resolve({ |
| 126 | + ok: true, |
| 127 | + body: responseStream, |
| 128 | + }) |
| 129 | + ); |
| 130 | + |
| 131 | + gaxGrpc.createStub(echoService, stubOptions).then(echoStub => { |
| 132 | + const stream = echoStub.expand( |
| 133 | + requestObject, |
| 134 | + {}, |
| 135 | + {}, |
| 136 | + () => {} |
| 137 | + ) as StreamArrayParser; |
| 138 | + const results: {}[] = []; |
| 139 | + stream.on('data', data => { |
| 140 | + results.push(data); |
| 141 | + }); |
| 142 | + stream.on('error', done); |
| 143 | + stream.on('end', () => { |
| 144 | + assert.deepStrictEqual(results, responseObject); |
| 145 | + done(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle fetch failure', done => { |
| 151 | + const requestObject = {content: 'test-content'}; |
| 152 | + sinon |
| 153 | + // incomplete types for nodeFetch, so... |
| 154 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 155 | + .stub(nodeFetch, 'Promise' as any) |
| 156 | + .returns(Promise.reject(new Error('Fetch error'))); |
| 157 | + |
| 158 | + gaxGrpc.createStub(echoService, stubOptions).then(echoStub => { |
| 159 | + echoStub.echo(requestObject, {}, {}, (err?: {}) => { |
| 160 | + assert.strictEqual((err as Error).message, 'Fetch error'); |
| 161 | + done(); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should handle streaming request failure', done => { |
| 167 | + const requestObject = {content: 'test content'}; |
| 168 | + sinon |
| 169 | + // incomplete types for nodeFetch, so... |
| 170 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 171 | + .stub(nodeFetch, 'Promise' as any) |
| 172 | + .returns(Promise.reject(new Error('Fetch error'))); |
| 173 | + |
| 174 | + gaxGrpc.createStub(echoService, stubOptions).then(echoStub => { |
| 175 | + const stream = echoStub.expand(requestObject) as StreamArrayParser; |
| 176 | + stream.on('error', err => { |
| 177 | + assert.strictEqual((err as Error).message, 'Fetch error'); |
| 178 | + done(); |
| 179 | + }); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
115 | 183 | describe('should support enum conversion in proto message', () => { |
116 | 184 | it('should support enum conversion in proto message response', done => { |
117 | 185 | const requestObject = {name: 'shelves/shelf-name'}; |
|
0 commit comments