From e223ccbd02175d1e366e677365445d9d5fbdb4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=89=B2?= Date: Tue, 18 Dec 2018 23:24:50 +0800 Subject: [PATCH] fix(module:auth): fix unrecognized full URL anonymous key (#328) --- .../auth/src/token/base.interceptor.spec.ts | 59 +++++++------------ packages/auth/src/token/base.interceptor.ts | 3 +- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/packages/auth/src/token/base.interceptor.spec.ts b/packages/auth/src/token/base.interceptor.spec.ts index 46252b77c..fde6eca96 100644 --- a/packages/auth/src/token/base.interceptor.spec.ts +++ b/packages/auth/src/token/base.interceptor.spec.ts @@ -101,9 +101,7 @@ describe('auth: base.interceptor', () => { it(`should be ignore /login`, (done: () => void) => { genModule({ ignores: [/assets\//, /\/login/] }, basicModel); - http.get('/login', { responseType: 'text' }).subscribe(value => { - done(); - }); + http.get('/login', { responseType: 'text' }).subscribe(done); const req = httpBed.expectOne('/login') as TestRequest; expect(req.request.headers.get('token')).toBeNull(); req.flush('ok!'); @@ -111,9 +109,7 @@ describe('auth: base.interceptor', () => { it('should be non-ignore', (done: () => void) => { genModule({ ignores: null }, basicModel); - http.get('/login', { responseType: 'text' }).subscribe(value => { - done(); - }); + http.get('/login', { responseType: 'text' }).subscribe(done); const req = httpBed.expectOne('/login') as TestRequest; expect(req.request.headers.get('token')).toBe('123'); req.flush('ok!'); @@ -123,32 +119,29 @@ describe('auth: base.interceptor', () => { describe('#with allow_anonymous_key', () => { it(`in params`, (done: () => void) => { genModule({}, genModel(SimpleTokenModel, null)); - http - .get('/user', { - responseType: 'text', - params: { _allow_anonymous: '' }, - }) - .subscribe(value => { - done(); - }); - const ret = httpBed.expectOne( - req => req.method === 'GET' && req.url === '/user', - ) as TestRequest; + http.get('/user', { responseType: 'text', params: { _allow_anonymous: '' } }).subscribe(done); + const ret = httpBed.expectOne(() => true); + expect(ret.request.headers.get('Authorization')).toBeNull(); + ret.flush('ok!'); + }); + it(`in params (full url)`, (done: () => void) => { + genModule({}, genModel(SimpleTokenModel, null)); + http.get('https://ng-alain.com/api/user', { responseType: 'text', params: { _allow_anonymous: '' } }).subscribe(done); + const ret = httpBed.expectOne(() => true); expect(ret.request.headers.get('Authorization')).toBeNull(); ret.flush('ok!'); }); it(`in url`, (done: () => void) => { genModule({}, genModel(SimpleTokenModel, null)); - http - .get('/user?_allow_anonymous=1', { - responseType: 'text', - }) - .subscribe(value => { - done(); - }); - const ret = httpBed.expectOne( - req => req.method === 'GET', - ) as TestRequest; + http.get('/user?_allow_anonymous=1', { responseType: 'text' }).subscribe(done); + const ret = httpBed.expectOne(() => true); + expect(ret.request.headers.get('Authorization')).toBeNull(); + ret.flush('ok!'); + }); + it(`in url (full url)`, (done: () => void) => { + genModule({}, genModel(SimpleTokenModel, null)); + http.get('https://ng-alain.com/api/user?_allow_anonymous=1', { responseType: 'text' }).subscribe(done); + const ret = httpBed.expectOne(() => true); expect(ret.request.headers.get('Authorization')).toBeNull(); ret.flush('ok!'); }); @@ -175,12 +168,7 @@ describe('auth: base.interceptor', () => { }); it('with location', (done: () => void) => { const login_url = 'https://ng-alain.com/login'; - genModule( - { - login_url, - }, - genModel(SimpleTokenModel, null), - ); + genModule({ login_url }, genModel(SimpleTokenModel, null)); http.get('/test', { responseType: 'text' }).subscribe( () => { expect(false).toBe(true); @@ -198,10 +186,7 @@ describe('auth: base.interceptor', () => { }); it('should be not navigate to login when token_invalid_redirect: false', (done: () => void) => { - genModule( - { token_invalid_redirect: false }, - genModel(SimpleTokenModel, null), - ); + genModule({ token_invalid_redirect: false }, genModel(SimpleTokenModel, null)); http.get('/test', { responseType: 'text' }).subscribe( () => { expect(false).toBe(true); diff --git a/packages/auth/src/token/base.interceptor.ts b/packages/auth/src/token/base.interceptor.ts index cd9ba3665..11d90e5f6 100644 --- a/packages/auth/src/token/base.interceptor.ts +++ b/packages/auth/src/token/base.interceptor.ts @@ -7,7 +7,6 @@ import { HttpRequest, } from '@angular/common/http'; import { Injector, Optional } from '@angular/core'; -import { Router } from '@angular/router'; import { Observable, Observer } from 'rxjs'; import { _HttpClient } from '@delon/theme'; @@ -35,7 +34,7 @@ export abstract class BaseInterceptor implements HttpInterceptor { if ( options.allow_anonymous_key && - (req.params.has(options.allow_anonymous_key) || this.injector.get(Router).parseUrl(req.urlWithParams).queryParamMap.has(options.allow_anonymous_key)) + (req.params.has(options.allow_anonymous_key) || new RegExp(`[\?|&]${options.allow_anonymous_key}=[^&]+`).test(req.urlWithParams)) ) { return next.handle(req); }