|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
| 3 | +import { DspaceRestInterceptor } from './dspace-rest.interceptor'; |
| 4 | +import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http'; |
| 5 | +import { DspaceRestService } from './dspace-rest.service'; |
| 6 | +import { APP_CONFIG, AppConfig } from '../../../config/app-config.interface'; |
| 7 | +import { PLATFORM_ID } from '@angular/core'; |
| 8 | + |
| 9 | +describe('DspaceRestInterceptor', () => { |
| 10 | + let httpMock: HttpTestingController; |
| 11 | + let httpClient: HttpClient; |
| 12 | + const appConfig: Partial<AppConfig> = { |
| 13 | + rest: { |
| 14 | + ssl: false, |
| 15 | + host: 'localhost', |
| 16 | + port: 8080, |
| 17 | + nameSpace: '/server', |
| 18 | + baseUrl: 'http://api.example.com/server', |
| 19 | + } |
| 20 | + }; |
| 21 | + const appConfigWithSSR: Partial<AppConfig> = { |
| 22 | + rest: { |
| 23 | + ssl: false, |
| 24 | + host: 'localhost', |
| 25 | + port: 8080, |
| 26 | + nameSpace: '/server', |
| 27 | + baseUrl: 'http://api.example.com/server', |
| 28 | + ssrBaseUrl: 'http://ssr.example.com/server', |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + describe('When SSR base URL is not set ', () => { |
| 33 | + describe('and it\'s in the browser', () => { |
| 34 | + beforeEach(() => { |
| 35 | + TestBed.configureTestingModule({ |
| 36 | + imports: [HttpClientTestingModule], |
| 37 | + providers: [ |
| 38 | + DspaceRestService, |
| 39 | + { |
| 40 | + provide: HTTP_INTERCEPTORS, |
| 41 | + useClass: DspaceRestInterceptor, |
| 42 | + multi: true, |
| 43 | + }, |
| 44 | + { provide: APP_CONFIG, useValue: appConfig }, |
| 45 | + { provide: PLATFORM_ID, useValue: 'browser' } |
| 46 | + ], |
| 47 | + }); |
| 48 | + |
| 49 | + httpMock = TestBed.inject(HttpTestingController); |
| 50 | + httpClient = TestBed.inject(HttpClient); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not modify the request', () => { |
| 54 | + const url = 'http://api.example.com/server/items'; |
| 55 | + httpClient.get(url).subscribe((response) => { |
| 56 | + expect(response).toBeTruthy(); |
| 57 | + }); |
| 58 | + |
| 59 | + const req = httpMock.expectOne(url); |
| 60 | + expect(req.request.url).toBe(url); |
| 61 | + req.flush({}); |
| 62 | + httpMock.verify(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('and it\'s in SSR mode', () => { |
| 67 | + beforeEach(() => { |
| 68 | + TestBed.configureTestingModule({ |
| 69 | + imports: [HttpClientTestingModule], |
| 70 | + providers: [ |
| 71 | + DspaceRestService, |
| 72 | + { |
| 73 | + provide: HTTP_INTERCEPTORS, |
| 74 | + useClass: DspaceRestInterceptor, |
| 75 | + multi: true, |
| 76 | + }, |
| 77 | + { provide: APP_CONFIG, useValue: appConfig }, |
| 78 | + { provide: PLATFORM_ID, useValue: 'server' } |
| 79 | + ], |
| 80 | + }); |
| 81 | + |
| 82 | + httpMock = TestBed.inject(HttpTestingController); |
| 83 | + httpClient = TestBed.inject(HttpClient); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should not replace the base URL', () => { |
| 87 | + const url = 'http://api.example.com/server/items'; |
| 88 | + |
| 89 | + httpClient.get(url).subscribe((response) => { |
| 90 | + expect(response).toBeTruthy(); |
| 91 | + }); |
| 92 | + |
| 93 | + const req = httpMock.expectOne(url); |
| 94 | + expect(req.request.url).toBe(url); |
| 95 | + req.flush({}); |
| 96 | + httpMock.verify(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('When SSR base URL is set ', () => { |
| 102 | + describe('and it\'s in the browser', () => { |
| 103 | + beforeEach(() => { |
| 104 | + TestBed.configureTestingModule({ |
| 105 | + imports: [HttpClientTestingModule], |
| 106 | + providers: [ |
| 107 | + DspaceRestService, |
| 108 | + { |
| 109 | + provide: HTTP_INTERCEPTORS, |
| 110 | + useClass: DspaceRestInterceptor, |
| 111 | + multi: true, |
| 112 | + }, |
| 113 | + { provide: APP_CONFIG, useValue: appConfigWithSSR }, |
| 114 | + { provide: PLATFORM_ID, useValue: 'browser' } |
| 115 | + ], |
| 116 | + }); |
| 117 | + |
| 118 | + httpMock = TestBed.inject(HttpTestingController); |
| 119 | + httpClient = TestBed.inject(HttpClient); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should not modify the request', () => { |
| 123 | + const url = 'http://api.example.com/server/items'; |
| 124 | + httpClient.get(url).subscribe((response) => { |
| 125 | + expect(response).toBeTruthy(); |
| 126 | + }); |
| 127 | + |
| 128 | + const req = httpMock.expectOne(url); |
| 129 | + expect(req.request.url).toBe(url); |
| 130 | + req.flush({}); |
| 131 | + httpMock.verify(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('and it\'s in SSR mode', () => { |
| 136 | + beforeEach(() => { |
| 137 | + TestBed.configureTestingModule({ |
| 138 | + imports: [HttpClientTestingModule], |
| 139 | + providers: [ |
| 140 | + DspaceRestService, |
| 141 | + { |
| 142 | + provide: HTTP_INTERCEPTORS, |
| 143 | + useClass: DspaceRestInterceptor, |
| 144 | + multi: true, |
| 145 | + }, |
| 146 | + { provide: APP_CONFIG, useValue: appConfigWithSSR }, |
| 147 | + { provide: PLATFORM_ID, useValue: 'server' } |
| 148 | + ], |
| 149 | + }); |
| 150 | + |
| 151 | + httpMock = TestBed.inject(HttpTestingController); |
| 152 | + httpClient = TestBed.inject(HttpClient); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should replace the base URL', () => { |
| 156 | + const url = 'http://api.example.com/server/items'; |
| 157 | + const ssrBaseUrl = appConfigWithSSR.rest.ssrBaseUrl; |
| 158 | + |
| 159 | + httpClient.get(url).subscribe((response) => { |
| 160 | + expect(response).toBeTruthy(); |
| 161 | + }); |
| 162 | + |
| 163 | + const req = httpMock.expectOne(ssrBaseUrl + '/items'); |
| 164 | + expect(req.request.url).toBe(ssrBaseUrl + '/items'); |
| 165 | + req.flush({}); |
| 166 | + httpMock.verify(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should not replace any query param containing the base URL', () => { |
| 170 | + const url = 'http://api.example.com/server/items?url=http://api.example.com/server/item/1'; |
| 171 | + const ssrBaseUrl = appConfigWithSSR.rest.ssrBaseUrl; |
| 172 | + |
| 173 | + httpClient.get(url).subscribe((response) => { |
| 174 | + expect(response).toBeTruthy(); |
| 175 | + }); |
| 176 | + |
| 177 | + const req = httpMock.expectOne(ssrBaseUrl + '/items?url=http://api.example.com/server/item/1'); |
| 178 | + expect(req.request.url).toBe(ssrBaseUrl + '/items?url=http://api.example.com/server/item/1'); |
| 179 | + req.flush({}); |
| 180 | + httpMock.verify(); |
| 181 | + }); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments