UNPKG

bixi

Version:

企业级中后台前端解决方案

81 lines (74 loc) 2.6 kB
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http'; import { HttpClientTestingModule, HttpTestingController, TestRequest } from '@angular/common/http/testing'; import { Component, Type } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { of } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { BixiAuthModule } from '../../auth.module'; import { BIXI_AUTH_CONFIG, BixiAuthConfig } from '../../config.type'; import { BIXI_SERVICE_TOKEN } from '../interface'; import { JWTInterceptor } from './jwt.interceptor'; import { JWTTokenModel } from './jwt.model'; function genModel( token: | string | null = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwibmFtZSI6ImNpcGNoayIsImFkbWluIjp0cnVlLCJleHAiOjQ2NzA0MDk2MDB9.IINuMTwqwCQP63fSQ-ZPgOEaE8lilrUceUX9Wy47PBk` ) { const model = new JWTTokenModel(); // from: https://jwt.io/ model.token = token; return model; } describe('auth: jwt.interceptor', () => { let http: HttpClient; let httpBed: HttpTestingController; function genModule(options: BixiAuthConfig, tokenData?: JWTTokenModel) { TestBed.configureTestingModule({ declarations: [MockComponent], imports: [ HttpClientTestingModule, RouterTestingModule.withRoutes([ { path: 'login', component: MockComponent } ]), BixiAuthModule ], providers: [ { provide: BIXI_AUTH_CONFIG, useValue: { ...options } }, { provide: HTTP_INTERCEPTORS, useClass: JWTInterceptor, multi: true } ] }); if (tokenData) TestBed.inject(BIXI_SERVICE_TOKEN).set(tokenData); http = TestBed.inject<HttpClient>(HttpClient); httpBed = TestBed.inject(HttpTestingController as Type<HttpTestingController>); } it('should be add token', (done: () => void) => { const basicModel = genModel(); genModule({}, basicModel); http.get('/test', { responseType: 'text' }).subscribe(() => { done(); }); const req = httpBed.expectOne('/test') as TestRequest; expect(req.request.headers.get('Authorization')).toBe(`Bearer ${ basicModel.token }`); req.flush('ok!'); }); it('should be invalid token', (done: () => void) => { genModule({}, genModel(null)); http .get('/test') .pipe( catchError(err => { expect(err.status).toBe(401); done(); return of(err); }) ) .subscribe(); }); }); @Component({ template: '' }) class MockComponent { }