-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest.path.ts
More file actions
238 lines (221 loc) · 7.08 KB
/
test.path.ts
File metadata and controls
238 lines (221 loc) · 7.08 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
// Copyright 2014 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, before, beforeEach, after} from 'mocha';
import {GaxiosResponse} from 'gaxios';
import {APIEndpoint, GaxiosResponseWithHTTP2} from 'googleapis-common';
import * as nock from 'nock';
import {GoogleApis} from '../src';
import {Utils} from './utils';
describe('Path params', () => {
let localDrive: APIEndpoint;
let remoteDrive: APIEndpoint;
before(async () => {
nock.cleanAll();
const google = new GoogleApis();
remoteDrive = await Utils.loadApi(google, 'drive', 'v2');
nock.disableNetConnect();
});
beforeEach(() => {
nock.cleanAll();
nock.disableNetConnect();
const google = new GoogleApis();
localDrive = google.drive('v2');
});
it('should not throw error if not included and required', done => {
assert.doesNotThrow(() => {
localDrive.files.get({}, Utils.noop);
remoteDrive.files.get({}, Utils.noop);
done();
});
});
it('should return an err object if not included and required', done => {
localDrive.files.get({}, (err: Error) => {
assert.notStrictEqual(err, null);
remoteDrive.files.get({}, (e: Error) => {
assert.notStrictEqual(e, null);
done();
});
});
});
it('should be mentioned in err.message when missing', done => {
localDrive.files.get({}, (err: Error) => {
assert.notStrictEqual(
err.message.indexOf('fileId'),
-1,
'Missing param not mentioned in error',
);
remoteDrive.files.get({}, (e: Error) => {
assert.notStrictEqual(
e.message.indexOf('fileId'),
-1,
'Missing param not mentioned in error',
);
done();
});
});
});
it('should return null response object if not included and required', done => {
localDrive.files.get({}, (err: Error, resp: {}) => {
assert(err);
assert.strictEqual(resp, undefined);
remoteDrive.files.get({}, (e: Error, resp2: {}) => {
assert(e);
assert.strictEqual(resp2, undefined);
done();
});
});
});
it('should return null request object if not included and required', () => {
let req = localDrive.files.get({}, Utils.noop);
assert.strictEqual(req, undefined);
req = remoteDrive.files.get({}, Utils.noop);
assert.strictEqual(req, undefined);
});
it('should return null request object if not included and required and no callback', () => {
let req = localDrive.files.get({}, Utils.noop);
assert.strictEqual(req, undefined);
req = remoteDrive.files.get({}, Utils.noop);
assert.strictEqual(req, undefined);
});
it('should not be modifiable directly', () => {
const options = {fileId: '123'};
assert.doesNotThrow(() => {
// should not modify options object
localDrive.files.get(options, Utils.noop);
localDrive.files.get(options, Utils.noop);
remoteDrive.files.get(options, Utils.noop);
remoteDrive.files.get(options, Utils.noop);
});
});
it('should be put in URL of path', done => {
const p = '/drive/v2/files/abc123';
nock(Utils.baseUrl).get(p).reply(200);
localDrive.files.get(
{fileId: 'abc123'},
(err: Error, res: GaxiosResponse) => {
if (err) {
return done(err);
}
assert.strictEqual(res.config.url, Utils.baseUrl + p);
nock(Utils.baseUrl).get(p).reply(200);
remoteDrive.files.get(
{fileId: 'abc123'},
(err2: Error, res2: GaxiosResponse) => {
if (err2) {
return done(err2);
}
assert.strictEqual(res2.config.url, Utils.baseUrl + p);
done();
},
);
},
);
done();
});
it('should be put in URL of pathname', done => {
const p = '/drive/v2/files/123abc';
nock(Utils.baseUrl).get(p).reply(200);
localDrive.files.get(
{fileId: '123abc'},
(err: Error, res: GaxiosResponse) => {
if (err) {
return done(err);
}
assert.strictEqual(Utils.getPath(res), p);
nock(Utils.baseUrl).get(p).reply(200);
remoteDrive.files.get({fileId: '123abc'}, (err2: Error) => {
if (err2) {
return done(err2);
}
assert.strictEqual(Utils.getPath(res), p);
done();
});
},
);
});
it('should be urlencoded', done => {
const p = `/drive/v2/files/${encodeURIComponent('p@ram')}`;
nock(Utils.baseUrl).get(p).reply(200);
localDrive.files.get(
{fileId: 'p@ram'},
(err: Error, res: GaxiosResponse) => {
if (err) {
return done(err);
}
const parm = Utils.getPath(res).split('/').pop();
assert.strictEqual(decodeURIComponent(parm!), 'p@ram');
nock(Utils.baseUrl).get(p).reply(200);
remoteDrive.files.get({fileId: 'p@ram'}, (err2: Error) => {
if (err2) {
return done(err2);
}
const parm = Utils.getPath(res).split('/').pop();
assert.strictEqual(decodeURIComponent(parm!), 'p@ram');
done();
});
},
);
});
it('should keep query params null if only path params', done => {
const p = '/drive/v2/files/123abc';
nock(Utils.baseUrl).get(p).reply(200);
localDrive.files.get(
{fileId: '123abc'},
(err: Error, res: GaxiosResponseWithHTTP2) => {
if (err) {
return done(err);
}
assert.strictEqual(Utils.getQs(res), null);
nock(Utils.baseUrl).get(p).reply(200);
remoteDrive.files.get(
{fileId: '123abc'},
(err2: Error, res2: GaxiosResponseWithHTTP2) => {
if (err2) {
return done(err2);
}
assert.strictEqual(Utils.getQs(res2), null);
done();
},
);
},
);
});
it('should keep query params as is', done => {
const p = '/drive/v2/files/123abc?hello=world';
nock(Utils.baseUrl).get(p).reply(200);
localDrive.files.get(
{fileId: '123abc', hello: 'world'},
(err: Error, res: GaxiosResponseWithHTTP2) => {
if (err) {
return done(err);
}
assert.strictEqual(Utils.getQs(res), 'hello=world');
nock(Utils.baseUrl).get(p).reply(200);
remoteDrive.files.get(
{fileId: '123abc', hello: 'world'},
(err2: Error) => {
if (err2) {
return done(err2);
}
assert.strictEqual(Utils.getQs(res), 'hello=world');
done();
},
);
},
);
});
after(() => {
nock.cleanAll();
});
});