Skip to content

Commit

Permalink
Merge pull request #2 from ismailmmd/issue-92-add-unit-test
Browse files Browse the repository at this point in the history
Issue 92 add unit test
  • Loading branch information
v 1 r t l committed Nov 9, 2022
2 parents d949703 + 593eaac commit 9cfc36c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
27 changes: 8 additions & 19 deletions tests/cookieParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ describe('when req.cookies exists', function (it) {

describe('when a secret is given', function (it) {
const val = signature.sign('foobarbaz', 'keyboard cat')
// TODO: "bar" fails...

it('should populate req.signedCookies with false for incorrect secret', async () => {
await makeFetch(createServer('keyboard new cat'))('/signed', {
headers: {
Cookie: 'foo=s:' + val
}
}).expect(200, '{"foo":false}')
})

it('should populate req.signedCookies', async () => {
await makeFetch(createServer('keyboard cat'))('/signed', {
Expand Down Expand Up @@ -117,21 +124,3 @@ describe('when multiple secrets are given', function (it) {
}).expect(200, '{"buzz":"foobar","fizz":"foobar"}')
})
})

/* describe('when no secret is given', function () {
let server: http.Server
beforeEach(() => (server = createServer()))
it('should populate req.cookies', async () => {
await makeFetch(server).get('/').set('Cookie', 'foo=bar; bar=baz').expect(200, '{"foo":"bar","bar":"baz"}', done)
})
it('should not populate req.signedCookies', async () => {
var val = signature.sign('foobarbaz', 'keyboard cat')
await makeFetch(server)
.get('/signed')
.set('Cookie', 'foo=s:' + val)
.expect(200, '{}', done)
})
}) */
4 changes: 4 additions & 0 deletions tests/signedCookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ it('should return false for tampered signed string', function () {
assert.equal(signedCookie('s:foobaz.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', 'keyboard cat'), false)
})

it('should return false when invalid secret is passed', function () {
assert.equal(signedCookie('s:foobaz.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', ''), false)
})

it('should return unsigned value for signed string', function () {
assert.equal(signedCookie('s:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', 'keyboard cat'), 'foobar')
})
Expand Down

0 comments on commit 9cfc36c

Please sign in to comment.