Skip to content

Commit

Permalink
add test for opts support + koa 2 💉 ..
Browse files Browse the repository at this point in the history
  • Loading branch information
3imed-jaberi committed May 27, 2020
1 parent 3b4c591 commit 8bafa18
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,51 @@ describe('Koa Querystring', function () {
})
})
})

describe('custom qs options', function () {
it('should correctly parse numbers and booleans', function (done) {
let app = qs(new Koa(), null, {
decoder: function (str) {
switch (str) {
case 'true': return true;
case 'false': return false;
case 'null': return null;
}

if (/^[\d.]+$/.test(str)) {
return Number(str);
}

return str;
}
})

app.use(convert(function* (next) {
try {
yield* next
} catch (err) {
console.error(err.stack)
}
}))

app.use(convert(function* () {
this.body = this.query;
}))

request(app.listen())
.get('/?a=str&b=1&c=3.1415&d=true&e=false&f=null&g=2.7182e')
.expect(function (res) {
res.body.should.eql({
a: 'str',
b: 1,
c: 3.1415,
d: true,
e: false,
f: null,
g: '2.7182e',
})
})
.expect(200, done);
})
})
})

0 comments on commit 8bafa18

Please sign in to comment.