diff --git a/docs/api/response.md b/docs/api/response.md index e0b04020..0608c423 100644 --- a/docs/api/response.md +++ b/docs/api/response.md @@ -8,23 +8,25 @@ response signature. **Kind**: global class * [Response](#Response) - * [new Response(headers, method, [body], ...otherOptions)](#new_Response_new) + * [new Response(headers, [body], ...otherOptions)](#new_Response_new) * [.get(field)](#Response+get) ⇒ string \| undefined * [.redirect(url)](#Response+redirect) * [.set(field, value)](#Response+set) -### new Response(headers, method, [body], ...otherOptions) +### new Response(headers, [body], ...otherOptions) Create a new Response instance. +The `status` always starts as `200` and is set by the handlers (for example +`302` for a redirect, or the error's code), so it cannot be passed in. + | Param | Type | Description | | --- | --- | --- | | headers | object | key-value object of headers | -| method | string | the HTTP method | | [body] | object | optional key-value object of body parameters | -| ...otherOptions | object | any other properties that should be assigned to the request by your webserver | +| ...otherOptions | object | any other properties that should be assigned to the response by your webserver; properties the response already has (such as `status` or its methods) are not overwritten | **Example** ```js diff --git a/lib/response.js b/lib/response.js index 41a47136..cc494ea0 100644 --- a/lib/response.js +++ b/lib/response.js @@ -14,11 +14,13 @@ class Response { /** * Create a new Response instance. + * + * The `status` always starts as `200` and is set by the handlers (for example + * `302` for a redirect, or the error's code), so it cannot be passed in. * @constructor * @param headers {object} key-value object of headers - * @param method {string} the HTTP method * @param body {object=} optional key-value object of body parameters - * @param otherOptions {...object} any other properties that should be assigned to the request by your webserver + * @param otherOptions {...object} any other properties that should be assigned to the response by your webserver; properties the response already has (such as `status` or its methods) are not overwritten */ constructor({ headers = {}, body = {}, ...otherOptions } = {}) { this.status = 200; diff --git a/test/compliance/client-credential-workflow_test.js b/test/compliance/client-credential-workflow_test.js index df5865f8..e15139a8 100644 --- a/test/compliance/client-credential-workflow_test.js +++ b/test/compliance/client-credential-workflow_test.js @@ -106,6 +106,38 @@ describe('ClientCredentials Workflow Compliance (4.4)', function () { db.refreshTokens.has(token.refreshToken).should.equal(false); }); + /** + * 5.1. Successful Response + * + * The authorization server issues an access token and optional + * refresh token, and constructs the response by adding the following + * parameters to the entity-body of the HTTP response with a 200 (OK) + * status code + */ + it('returns status 200 for a successful token response even if the initial status is 404', async function () { + const response = new Response({ status: 404 }); + const request = createRequest({ + body: { + grant_type: 'client_credentials', + scope: enabledScope, + }, + headers: { + authorization: 'Basic ' + Buffer.from(clientDoc.id + ':' + clientDoc.secret).toString('base64'), + 'content-type': 'application/x-www-form-urlencoded', + }, + method: 'POST', + }); + + const token = await oAuth2Server.token(request, response); + + response.body.access_token.should.equal(token.accessToken); + response.body.token_type.should.equal('Bearer'); + db.accessTokens.has(token.accessToken).should.equal(true); + + // RFC 6749 section 5.1 specifies status 200 for a successful token response. + response.status.should.equal(200); + }); + /** * 7. Accessing Protected Resources * diff --git a/test/integration/response_test.js b/test/integration/response_test.js index cbaf529f..589b1eea 100644 --- a/test/integration/response_test.js +++ b/test/integration/response_test.js @@ -32,6 +32,12 @@ describe('Response integration', function () { response.status.should.equal(200); }); + + it('should not set the `status` from the options', function () { + const response = new Response({ body: {}, headers: {}, status: 404 }); + + response.status.should.equal(200); + }); }); describe('get()', function () {