Skip to content

Commit

Permalink
fix: uppercase for header decorator (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jun 30, 2021
1 parent 300a3ec commit cfcfb1f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/util/webRouterParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const extractKoaLikeValue = (key, data) => {
case RouteParamTypes.QUERY:
return data ? ctx.query[data] : ctx.query;
case RouteParamTypes.HEADERS:
return data ? ctx.headers[data] : ctx.headers;
return data ? ctx.get(data) : ctx.headers;
case RouteParamTypes.SESSION:
return data ? ctx.session[data] : ctx.session;
case RouteParamTypes.FILESTREAM:
Expand Down Expand Up @@ -55,7 +55,7 @@ export const extractExpressLikeValue = (key, data) => {
case RouteParamTypes.QUERY:
return data ? req.query[data] : req.query;
case RouteParamTypes.HEADERS:
return data ? req.headers[data] : req.headers;
return data ? req.get(data) : req.headers;
case RouteParamTypes.SESSION:
return data ? req.session[data] : req.session;
case RouteParamTypes.FILESTREAM:
Expand Down
22 changes: 18 additions & 4 deletions packages/core/test/paramMapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('/test/web/paramMapping.test.ts', () => {
expect(meta.length).eq(9);
});

it('extract koa value shoule be ok', async () => {
it('extract koa value should be ok', async () => {
let fn = extractKoaLikeValue(RouteParamTypes.NEXT, {});
expect(await fn({}, 'next')).eq('next');

Expand All @@ -40,7 +40,13 @@ describe('/test/web/paramMapping.test.ts', () => {
fn = extractKoaLikeValue(RouteParamTypes.QUERY, null);
expect(await fn({query: { body : {aaa: 111}}}, 'next')).deep.eq({ body : {aaa: 111}});
fn = extractKoaLikeValue(RouteParamTypes.HEADERS, 'body');
expect(await fn({headers: { body : {aaa: 111}}}, 'next')).deep.eq({aaa: 111});
const ctx = {
headers: { body : {aaa: 111}},
get(key) {
return ctx.headers[key];
}
};
expect(await fn(ctx, 'next')).deep.eq({aaa: 111});
fn = extractKoaLikeValue(RouteParamTypes.HEADERS, null);
expect(await fn({headers: { body : {aaa: 111}}}, 'next')).deep.eq({ body : {aaa: 111}});
fn = extractKoaLikeValue(RouteParamTypes.SESSION, null);
Expand Down Expand Up @@ -68,7 +74,7 @@ describe('/test/web/paramMapping.test.ts', () => {
expect(await fn({ip: '127.0.0.1'}, 'next')).deep.eq('127.0.0.1');
});

it('extract express value shoule be ok', async () => {
it('extract express value should be ok', async () => {
let fn = extractExpressLikeValue(RouteParamTypes.NEXT, {});
expect(await fn({}, {}, 'next')).eq('next');

Expand All @@ -84,8 +90,16 @@ describe('/test/web/paramMapping.test.ts', () => {
expect(await fn({query: { body : {aaa: 111}}}, {}, 'next')).deep.eq({aaa: 111});
fn = extractExpressLikeValue(RouteParamTypes.QUERY, null);
expect(await fn({query: { body : {aaa: 111}}}, {}, 'next')).deep.eq({ body : {aaa: 111}});

const req = {
headers: { body : {aaa: 111}},
get(key) {
return req.headers[key];
}
};

fn = extractExpressLikeValue(RouteParamTypes.HEADERS, 'body');
expect(await fn({headers: { body : {aaa: 111}}}, {}, 'next')).deep.eq({aaa: 111});
expect(await fn(req, {}, 'next')).deep.eq({aaa: 111});
fn = extractExpressLikeValue(RouteParamTypes.HEADERS, null);
expect(await fn({headers: { body : {aaa: 111}}}, {}, 'next')).deep.eq({ body : {aaa: 111}});
fn = extractExpressLikeValue(RouteParamTypes.SESSION, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Redirect,
SetHeader,
Logger,
Headers,
} from '@midwayjs/decorator';
import { UserService } from '../service/user';
import { IMidwayExpressContext, IMidwayExpressRequest } from '../../../../../src';
Expand Down Expand Up @@ -71,4 +72,9 @@ export class APIController {
this.ctx.res.send('ctx-body');
}

@Get('/header-upper')
async getHeaderWithUppercase(@Headers('X-ABC') abc) {
return abc;
}

}
6 changes: 6 additions & 0 deletions packages/web-express/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe('/test/feature.test.ts', () => {
expect(result.headers['ccc']).toEqual('ddd');
});

it('should test get header with upper case', async () => {
const result = await createHttpRequest(app).get('/api/header-upper').set('x-abc', '321');
expect(result.status).toBe(200);
expect(result.text).toBe('321');
});

it('test get method with return value', async () => {
const result = await createHttpRequest(app).get('/api/').query({ name: 'harry' });
expect(result.status).toBe(201);
Expand Down
6 changes: 6 additions & 0 deletions packages/web-koa/test/fixtures/base-app/src/controller/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Redirect,
SetHeader,
Logger,
Headers,
} from '@midwayjs/decorator';
import { UserService } from '../service/user';
import { IMidwayKoaContext } from '../../../../../src';
Expand Down Expand Up @@ -61,4 +62,9 @@ export class APIController {
async getCtxBody() {
this.ctx.body = 'ctx-body';
}

@Get('/header-upper')
async getHeaderWithUppercase(@Headers('X-ABC') abc) {
return abc;
}
}
6 changes: 6 additions & 0 deletions packages/web-koa/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe('/test/feature.test.ts', () => {
expect(result.headers['ccc']).toEqual('ddd');
});

it('should test get header with upper case', async () => {
const result = await createHttpRequest(app).get('/header-upper').set('x-abc', '321');
expect(result.status).toBe(200);
expect(result.text).toBe('321');
});

it('test get method with return value', async () => {
const result = await createHttpRequest(app).get('/').query({ name: 'harry', age: 18 });
expect(result.status).toBe(201);
Expand Down

0 comments on commit cfcfb1f

Please sign in to comment.