Skip to content

Commit

Permalink
fix: fix host header
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiming committed Mar 17, 2018
1 parent 6a152e0 commit 0f987bc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/middlewares/hostInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import { BadRequestError } from 'routing-controllers';
*/
export default function hostInject() {
return async (ctx: Context, next: () => Promise<{}>) => {
const fromHost = ctx.headers['t-host'];
// host validate
const cacheHostId = await Cache.Instance.get(`HOST:${ctx.host}`);
const cacheHostId = await Cache.Instance.get(`HOST:${fromHost}`);
if (!cacheHostId) {
const host = await getCustomRepository(HostRepository).findOne({
website: ctx.host
website: fromHost
});
if (!host) {
throw new BadRequestError('Request Host Not Found');
} else {
await Cache.Instance.set(`HOST:${ctx.host}`, host.id);
await Cache.Instance.set(`HOST:${fromHost}`, host.id);
ctx.state.hostId = host.id;
}
} else {
Expand Down
24 changes: 12 additions & 12 deletions test/controllers/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('LogController', () => {
it('建立会话,无 TOKEN', async () => {
const res = await request(Test.Instance.app)
.post('/log/initialize')
.set('Host', host.website)
.set('t-host', host.website)
.send({
referrer: 'https://ruiming.me',
lang: 'zh-cn',
Expand All @@ -51,8 +51,8 @@ describe('LogController', () => {
it('建立会话,复用 TOKEN', async () => {
const res = await request(Test.Instance.app)
.post('/log/initialize')
.set('Authorization', token)
.set('Host', host.website)
.set('authorization', token)
.set('t-host', host.website)
.send({
referrer: 'https://ruiming.me',
lang: 'zh-cn',
Expand All @@ -69,7 +69,7 @@ describe('LogController', () => {
it('发送页面数据,没有 TOKEN', async () => {
const res = await request(Test.Instance.app)
.post('/log/page')
.set('Host', host.website)
.set('t-host', host.website)
.send(Test.Instance.mockPage());

assert(res.status === 400);
Expand All @@ -78,8 +78,8 @@ describe('LogController', () => {
it('正确发送页面数据', async () => {
const res = await request(Test.Instance.app)
.post('/log/page')
.set('Authorization', token)
.set('Host', host.website)
.set('authorization', token)
.set('t-host', host.website)
.send(
Test.Instance.mockPage({
url: 'https://ruiming.me'
Expand All @@ -95,8 +95,8 @@ describe('LogController', () => {
const [name1, name2] = [faker.lorem.word(), faker.lorem.word()];
const res = await request(Test.Instance.app)
.post('/log/assets')
.set('Authorization', token)
.set('Host', host.website)
.set('authorization', token)
.set('t-host', host.website)
.send({
pageId,
assets: [Test.Instance.mockAsset({ name: name1 }), Test.Instance.mockAsset({ name: name2 })]
Expand All @@ -114,8 +114,8 @@ describe('LogController', () => {
it('跳转下一个页面', async () => {
const res = await request(Test.Instance.app)
.post('/log/page')
.set('Authorization', token)
.set('Host', host.website)
.set('authorization', token)
.set('t-host', host.website)
.send(
Test.Instance.mockPage({
url: 'https://ruiming.me',
Expand All @@ -131,8 +131,8 @@ describe('LogController', () => {
it('网页退出', async () => {
const res = await request(Test.Instance.app)
.post('/log/exit')
.set('Authorization', token)
.set('Host', host.website)
.set('authorization', token)
.set('t-host', host.website)
.send({
pageId: nextPageId,
exitTime: new Date()
Expand Down

0 comments on commit 0f987bc

Please sign in to comment.