Skip to content

Commit 90de84c

Browse files
author
Eric Koleda
committed
Add a test for passing additional parameters.
1 parent f9e29ef commit 90de84c

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,16 @@ method:
340340
341341
```js
342342
var authorizationUrl = getService().getAuthorizationUrl({
343+
// Pass the additional parameter "lang" with the value "fr".
343344
lang: 'fr'
344345
});
345346
```
346347
347348
These values will be stored along-side Apps Script's internal information in the
348-
cryptographically secure `state` token, which is passed in the authorization URL
349-
and passed back to the redirect URI. The `state` token is automatically
350-
decrypted in the callback function, and you can access your parameters using the
351-
same `request.parameter` field used in web apps:
349+
encypted `state` token, which is passed in the authorization URL and passed back
350+
to the redirect URI. The `state` token is automatically decrypted in the
351+
callback function and you can access your parameters using the same
352+
`request.parameter` field used in web apps:
352353
353354
```js
354355
function authCallback(request) {

src/Utilities.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function validate_(params) {
4242
Object.keys(params).forEach(function(name) {
4343
var value = params[name];
4444
if (!value) {
45-
throw Utilities.formatString('%s is required.', name);
45+
throw new Error(name + ' is required.');
4646
}
4747
});
4848
}

test/mocks/script.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var MockScriptApp = function() {};
2+
3+
MockScriptApp.prototype.getScriptId = function() {
4+
return Math.random().toString(36).substring(2);
5+
};
6+
7+
MockScriptApp.prototype.newStateToken = function() {
8+
return new MockStateTokenBuilder();
9+
};
10+
11+
var MockStateTokenBuilder = function() {
12+
this.arguments = {};
13+
};
14+
15+
MockStateTokenBuilder.prototype.withMethod = function(method) {
16+
this.method = method;
17+
return this;
18+
};
19+
20+
MockStateTokenBuilder.prototype.withArgument = function(key, value) {
21+
this.arguments[key] = value;
22+
return this;
23+
};
24+
25+
MockStateTokenBuilder.prototype.withTimeout = function(timeout) {
26+
this.timeout = timeout;
27+
return this;
28+
};
29+
30+
MockStateTokenBuilder.prototype.createToken = function() {
31+
return JSON.stringify(this);
32+
};
33+
34+
module.exports = MockScriptApp;

test/test.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ var MockUrlFetchApp = require('./mocks/urlfetchapp');
44
var MockProperties = require('./mocks/properties');
55
var MockCache = require('./mocks/cache');
66
var MockLock = require('./mocks/lock');
7+
var MockScriptApp = require('./mocks/script');
78
var Future = require('fibers/future');
89

910
var mocks = {
10-
ScriptApp: {
11-
getScriptId: function() {
12-
return '12345';
13-
}
14-
},
11+
ScriptApp: new MockScriptApp(),
1512
UrlFetchApp: new MockUrlFetchApp(),
1613
Utilities: {
1714
base64Encode: function(data) {
@@ -405,6 +402,32 @@ describe('Service', function() {
405402
service.exchangeGrant_();
406403
});
407404
});
405+
406+
describe('#getAuthorizationUrl()', function() {
407+
it('should add additional parameters to the state token', function() {
408+
var service = OAuth2.createService('test')
409+
.setAuthorizationBaseUrl('http://www.example.com')
410+
.setClientId('abc')
411+
.setClientSecret('def')
412+
.setCallbackFunction('authCallback');
413+
var authorizationUrl = service.getAuthorizationUrl({
414+
foo: 'bar'
415+
});
416+
417+
// Extract the state token from the URL and parse it. For example, the
418+
// URL http://www.example.com?state=%7B%22a%22%3A1%7D would produce
419+
// {a: 1}.
420+
var querystring = authorizationUrl.split('?')[1];
421+
var params = querystring.split('&').reduce(function(result, pair) {
422+
var parts = pair.split('=').map(decodeURIComponent);
423+
result[parts[0]] = parts[1];
424+
return result;
425+
}, {});
426+
var state = JSON.parse(params.state);
427+
428+
assert.equal(state.arguments.foo, 'bar');
429+
});
430+
});
408431
});
409432

410433
describe('Utilities', function() {

0 commit comments

Comments
 (0)