Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

feat(assert): assert cookie exists #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,34 @@ module.exports = function (Config) {
return this
}

/**
* Asserts the cookie exists.
*
* @method assertCookieExists
*
* @param {String} key
*
* @return {this}
*/
assertCookieExists (key) {
this._assert.exists(this.cookies[key])
return this
}

/**
* Asserts the plain cookie exists
*
* @method assertPlainCookieExists
*
* @param {String} key
*
* @return {this}
*/
assertPlainCookieExists (key) {
this._assert.exists(this.plainCookies[key])
return this
}

/**
* Assert header value
*
Expand Down
34 changes: 34 additions & 0 deletions test/unit/api-response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ test.group('Api Response', (group) => {
server.close()
})

test('assert response plain cookies exists', async (assert) => {
assert.plan(1)
const server = http.createServer((req, res) => {
nodeCookie.create(res, 'total', 20)
res.end()
}).listen(PORT)

const config = new Config()
const BaseResponse = BaseResponseManager(config)
const BaseRequest = BaseRequestManager(config)
const api = new ApiClient(BaseRequest, BaseResponse, assert)
const response = await api.get('/').end()
response.assertPlainCookieExists('total')
server.close()
})

test('assert response encrypted cookies', async (assert) => {
assert.plan(1)
const config = new Config()
Expand All @@ -155,6 +171,24 @@ test.group('Api Response', (group) => {
server.close()
})

test('assert response encrypted cookies exists', async (assert) => {
assert.plan(1)
const config = new Config()
config.set('app.appKey', 'averylongrandomkey')

const server = http.createServer((req, res) => {
nodeCookie.create(res, 'total', 20, {}, config.get('app.appKey'), true)
res.end()
}).listen(PORT)

const BaseResponse = BaseResponseManager(config)
const BaseRequest = BaseRequestManager(config)
const api = new ApiClient(BaseRequest, BaseResponse, assert)
const response = await api.get('/').end()
response.assertCookieExists('total')
server.close()
})

test('assert response error', async (assert) => {
assert.plan(3)
const config = new Config()
Expand Down