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