-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest.transporters.ts
More file actions
302 lines (275 loc) · 9.3 KB
/
test.transporters.ts
File metadata and controls
302 lines (275 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as assert from 'assert';
import {describe, it, after, before, beforeEach} from 'mocha';
import {APIEndpoint} from 'googleapis-common';
import * as nock from 'nock';
import {AuthPlus} from '../src/googleapis';
import {GoogleApis} from '../src';
import {Utils} from './utils';
async function testHeaders(drive: APIEndpoint) {
const req = nock(Utils.baseUrl)
.post('/drive/v2/files/a/comments')
.reply(200, function () {
const headers = new Headers(this.req.headers);
// ensure that the x-goog-user-project is loaded from default credentials:
assert.strictEqual(
headers.get('x-goog-user-project'),
'my-quota-project',
);
// ensure that the x-goog-api-client header is populated by
// googleapis-common:
assert.ok(
/gdcl\/[0-9]+\.[\w-.]+ gl-node\/[0-9]+\.[\w-.]+/.test(
headers.get('x-goog-api-client')!,
),
);
});
const auth = getAuthClientMock();
const res = await drive.comments.insert({
fileId: 'a',
headers: new Headers({'If-None-Match': '12345'}),
auth: await auth.getClient(),
});
req.done();
auth.done();
assert.strictEqual(res.config.headers.get('If-None-Match'), '12345');
}
// Returns an auth client that fakes loading application default credentials
// from a fixtures directory:
function getAuthClientMock() {
// mock environment variables such that default credentials are loaded.
const projectOriginal = process.env.GCLOUD_PROJECT;
process.env.GCLOUD_PROJECT = 'my-fake-project';
const homeOriginal = process.env.HOME;
process.env.HOME = './test/fixtures/';
const appdataOriginal = process.env.APPDATA;
process.env.APPDATA = './test/fixtures/.config';
// an attempt will be made to fetch access token on first request.
const req = nock('https://oauth2.googleapis.com')
.post('/token')
.reply(200, {});
// An "AuthPlus" client with an added "done" method for resetting mocks.
class AuthMock extends AuthPlus {
done() {
process.env.GCLOUD_PROJECT = projectOriginal;
process.env.HOME = homeOriginal;
process.env.APPDATA = appdataOriginal;
req.done();
}
}
const auth = new AuthMock({
// Scopes can be specified either as an array or as a single, space-delimited string.
scopes: 'https://www.googleapis.com/auth/drive',
});
return auth;
}
async function testContentType(drive: APIEndpoint) {
nock(Utils.baseUrl).post('/drive/v2/files/a/comments').reply(200);
const res = await drive.comments.insert({
fileId: 'a',
resource: {content: 'hello '},
});
assert(
res.config.headers
.get('Content-Type')
.toString()
.indexOf('application/json') === 0,
);
}
async function testGzip(drive: APIEndpoint) {
nock(Utils.baseUrl)
.get('/drive/v2/files', undefined, {
reqheaders: {'Accept-Encoding': 'gzip'},
})
.reply(200, {});
const res = await drive.files.list();
assert.deepStrictEqual(res.data, {});
// note: axios strips the `content-encoding` header from the response,
// so that cannot be checked here.
}
async function testBody(drive: APIEndpoint) {
const scope = nock(Utils.baseUrl).get('/drive/v2/files').reply(200);
const res = await drive.files.list();
scope.done();
assert.strictEqual(res.config.headers['content-type'], undefined);
assert.strictEqual(res.config.body, undefined);
}
async function testBodyDelete(drive: APIEndpoint) {
const scope = nock(Utils.baseUrl).delete('/drive/v2/files/test').reply(200);
const res = await drive.files.delete({fileId: 'test'});
scope.done();
assert.strictEqual(res.config.headers['content-type'], undefined);
assert.strictEqual(res.config.body, undefined);
}
async function testResponseError(drive: APIEndpoint) {
await assert.rejects(
drive.files.list({q: 'hello'}),
(err: NodeJS.ErrnoException) => {
assert(err instanceof Error);
assert.strictEqual(err.message, 'Error!');
assert.strictEqual(err.code, 400);
return true;
},
);
}
async function testNotObjectError(oauth2: APIEndpoint) {
await assert.rejects(
oauth2.tokeninfo({access_token: 'hello'}),
(err: NodeJS.ErrnoException) => {
assert(err instanceof Error);
assert.strictEqual(err.message, 'invalid_grant');
assert.strictEqual((err as any).status, 400);
return true;
},
);
}
async function testBackendError(blogger: APIEndpoint) {
const obj = {longUrl: 'http://google.com/'};
await assert.rejects(
blogger.posts.publish({
resource: obj,
blogId: 'abc123',
postId: 'abc123',
}),
(err: NodeJS.ErrnoException) => {
assert(err instanceof Error);
console.log(err);
assert.strictEqual(Number((err as any).status), 500);
assert.strictEqual(err.message, 'There was an error!');
return true;
},
);
}
describe('Transporters', () => {
let localDrive: APIEndpoint;
let remoteDrive: APIEndpoint;
let localOauth2: APIEndpoint;
let remoteOauth2: APIEndpoint;
let localBlogger: APIEndpoint;
let remoteBlogger: APIEndpoint;
before(async () => {
nock.cleanAll();
const google = new GoogleApis();
[remoteDrive, remoteOauth2, remoteBlogger] = await Promise.all([
Utils.loadApi(google, 'drive', 'v2'),
Utils.loadApi(google, 'oauth2', 'v2'),
Utils.loadApi(google, 'blogger', 'v3'),
]);
nock.disableNetConnect();
});
after(() => {
nock.cleanAll();
});
beforeEach(() => {
nock.cleanAll();
nock.disableNetConnect();
const google = new GoogleApis();
localDrive = google.drive('v2');
localOauth2 = google.oauth2('v2');
localBlogger = google.blogger('v3');
});
it('should add headers to the request from params', async () => {
await testHeaders(localDrive);
await testHeaders(remoteDrive);
});
it('should automatically add content-type for POST requests', async () => {
await testContentType(localDrive);
await testContentType(remoteDrive);
});
it('should add the proper gzip headers', async () => {
await testGzip(localDrive);
await testGzip(remoteDrive);
});
it('should not add body for GET requests', async () => {
await testBody(localDrive);
await testBody(remoteDrive);
});
it('should not add body for DELETE requests', async () => {
await testBodyDelete(localDrive);
await testBodyDelete(remoteDrive);
});
it('should return errors within response body as instances of Error', async () => {
const scope = nock(Utils.baseUrl)
.get('/drive/v2/files?q=hello')
.times(2)
// Simulate an error returned via response body from
// Google's API endpoint
.reply(400, {error: {code: 400, message: 'Error!'}});
await testResponseError(localDrive);
await testResponseError(remoteDrive);
scope.done();
});
it('should return error message correctly when error is not an object', async () => {
const scope = nock(Utils.baseUrl)
.post('/oauth2/v2/tokeninfo?access_token=hello')
.times(2)
// Simulate an error returned via response body from
// Google's tokeninfo endpoint
.reply(400, {
error: 'invalid_grant',
error_description: 'Code was already redeemed.',
});
await testNotObjectError(localOauth2);
await testNotObjectError(remoteOauth2);
scope.done();
});
it('should return 5xx responses as errors', async () => {
const scope = nock('https://blogger.googleapis.com')
.post('/v3/blogs/abc123/posts/abc123/publish')
.times(2)
.reply(500, 'There was an error!');
await testBackendError(localBlogger);
await testBackendError(remoteBlogger);
scope.done();
});
it('should return 304 responses as success', async () => {
const scope = nock(Utils.baseUrl).get('/drive/v2/files').reply(304);
const res = await localDrive.files.list();
assert.strictEqual(res.status, 304);
scope.done();
});
it('should handle 5xx responses that include errors', async () => {
const scope = nock('https://blogger.googleapis.com')
.post('/v3/blogs/abc123/posts/abc123/publish')
.times(2)
.reply(500, {
error: {message: 'There was an error!'},
});
await testBackendError(localBlogger);
await testBackendError(remoteBlogger);
scope.done();
});
it('should handle a Backend Error', async () => {
const scope = nock('https://blogger.googleapis.com')
.post('/v3/blogs/abc123/posts/abc123/publish')
.times(2)
.reply(500, {
error: {
errors: [
{
domain: 'global',
reason: 'backendError',
message: 'There was an error!',
},
],
code: 500,
message: 'There was an error!',
},
});
await testBackendError(localBlogger);
await testBackendError(remoteBlogger);
scope.done();
});
});