koajs / koa

Expressive middleware for node.js using ES2017 async functions
https://koajs.com
MIT License
35.25k stars 3.23k forks source link

is `ctx.is` bug? #1081

Closed Matrixbirds closed 7 years ago

Matrixbirds commented 7 years ago
node: 8.4.0
koa: 2.3.0

koa/lib/request.js image

my app code

image

my test code

// mocha + co-mocha
context('POST /api/balabala', function () {
        let resp;
        function fetch(params) {
            return request.get('/api/balabala')
               .query(params);
        }
        it.only("should response 200 ", function *() {
            resp = yield fetch({
                balbalbalabl: 234234234234
            });
            console.log(resp.body);
            console.log(resp.status);
            expect(resp.status).to.equal(200);
        });
});

image

ctx.is("application/json") return null; but actual its header contains{ 'content-type': 'application/json'}

Runrioter commented 7 years ago

Because you have no request content, so it's no point in geting content type. https://github.com/koajs/koa/blob/43a1df8200dad6860f688713a112785535a3ed3d/lib/request.js#L565

Matrixbirds commented 7 years ago

oh i got it, thx

wszgxa commented 6 years ago

I have a question about this. If the request method is get, so there is no request content. In this situation, should we return null ?