-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a setting to use a different REST url during SSR execution #3358
Open
atarix83
wants to merge
12
commits into
DSpace:main
Choose a base branch
from
4Science:task/main/DURACOM-288
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8ec8eed
[DURACOM-288] Provide a setting to use a different REST url during SS…
atarix83 268cf64
[DURACOM-288] Additional test for thumbnail component
atarix83 a1cbe3b
Merge branch 'main' into task/main/DURACOM-288
atarix83 a9223ea
[DURACOM-288] Fix after merge
atarix83 ed4e794
119799: Prevent submission from updating the dropdown values when hit…
alexandrevryghem c12a3e6
Merge remote-tracking branch 'alex/w2p-122005_fixed-dropdown-values-r…
alexandrevryghem c8694e1
119799: Prevent the lookup/lookup-name fields from resetting when hit…
alexandrevryghem 428f859
Merge branch 'main' into task/main/DURACOM-288
atarix83 dae1e68
Add configuration to toggle replace/transfer
ybnd 1ee754e
[DURACOM-288] Refactoring configuration to transfer state
atarix83 6b9bc66
[DURACOM-288] Add missing settings to config.example.yml
atarix83 c35964a
[DURACOM-288] Add unit test to test SSR url replace
atarix83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
src/app/core/dspace-rest/dspace-rest.interceptor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { | ||
HTTP_INTERCEPTORS, | ||
HttpClient, | ||
} from '@angular/common/http'; | ||
import { | ||
HttpClientTestingModule, | ||
HttpTestingController, | ||
} from '@angular/common/http/testing'; | ||
import { PLATFORM_ID } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { | ||
APP_CONFIG, | ||
AppConfig, | ||
} from '../../../config/app-config.interface'; | ||
import { DspaceRestInterceptor } from './dspace-rest.interceptor'; | ||
import { DspaceRestService } from './dspace-rest.service'; | ||
|
||
describe('DspaceRestInterceptor', () => { | ||
let httpMock: HttpTestingController; | ||
let httpClient: HttpClient; | ||
const appConfig: Partial<AppConfig> = { | ||
rest: { | ||
ssl: false, | ||
host: 'localhost', | ||
port: 8080, | ||
nameSpace: '/server', | ||
baseUrl: 'http://api.example.com/server', | ||
}, | ||
}; | ||
const appConfigWithSSR: Partial<AppConfig> = { | ||
rest: { | ||
ssl: false, | ||
host: 'localhost', | ||
port: 8080, | ||
nameSpace: '/server', | ||
baseUrl: 'http://api.example.com/server', | ||
ssrBaseUrl: 'http://ssr.example.com/server', | ||
}, | ||
}; | ||
|
||
describe('When SSR base URL is not set ', () => { | ||
describe('and it\'s in the browser', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [ | ||
DspaceRestService, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: DspaceRestInterceptor, | ||
multi: true, | ||
}, | ||
{ provide: APP_CONFIG, useValue: appConfig }, | ||
{ provide: PLATFORM_ID, useValue: 'browser' }, | ||
], | ||
}); | ||
|
||
httpMock = TestBed.inject(HttpTestingController); | ||
httpClient = TestBed.inject(HttpClient); | ||
}); | ||
|
||
it('should not modify the request', () => { | ||
const url = 'http://api.example.com/server/items'; | ||
httpClient.get(url).subscribe((response) => { | ||
expect(response).toBeTruthy(); | ||
}); | ||
|
||
const req = httpMock.expectOne(url); | ||
expect(req.request.url).toBe(url); | ||
req.flush({}); | ||
httpMock.verify(); | ||
}); | ||
}); | ||
|
||
describe('and it\'s in SSR mode', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [ | ||
DspaceRestService, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: DspaceRestInterceptor, | ||
multi: true, | ||
}, | ||
{ provide: APP_CONFIG, useValue: appConfig }, | ||
{ provide: PLATFORM_ID, useValue: 'server' }, | ||
], | ||
}); | ||
|
||
httpMock = TestBed.inject(HttpTestingController); | ||
httpClient = TestBed.inject(HttpClient); | ||
}); | ||
|
||
it('should not replace the base URL', () => { | ||
const url = 'http://api.example.com/server/items'; | ||
|
||
httpClient.get(url).subscribe((response) => { | ||
expect(response).toBeTruthy(); | ||
}); | ||
|
||
const req = httpMock.expectOne(url); | ||
expect(req.request.url).toBe(url); | ||
req.flush({}); | ||
httpMock.verify(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('When SSR base URL is set ', () => { | ||
describe('and it\'s in the browser', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [ | ||
DspaceRestService, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: DspaceRestInterceptor, | ||
multi: true, | ||
}, | ||
{ provide: APP_CONFIG, useValue: appConfigWithSSR }, | ||
{ provide: PLATFORM_ID, useValue: 'browser' }, | ||
], | ||
}); | ||
|
||
httpMock = TestBed.inject(HttpTestingController); | ||
httpClient = TestBed.inject(HttpClient); | ||
}); | ||
|
||
it('should not modify the request', () => { | ||
const url = 'http://api.example.com/server/items'; | ||
httpClient.get(url).subscribe((response) => { | ||
expect(response).toBeTruthy(); | ||
}); | ||
|
||
const req = httpMock.expectOne(url); | ||
expect(req.request.url).toBe(url); | ||
req.flush({}); | ||
httpMock.verify(); | ||
}); | ||
}); | ||
|
||
describe('and it\'s in SSR mode', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [ | ||
DspaceRestService, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: DspaceRestInterceptor, | ||
multi: true, | ||
}, | ||
{ provide: APP_CONFIG, useValue: appConfigWithSSR }, | ||
{ provide: PLATFORM_ID, useValue: 'server' }, | ||
], | ||
}); | ||
|
||
httpMock = TestBed.inject(HttpTestingController); | ||
httpClient = TestBed.inject(HttpClient); | ||
}); | ||
|
||
it('should replace the base URL', () => { | ||
const url = 'http://api.example.com/server/items'; | ||
const ssrBaseUrl = appConfigWithSSR.rest.ssrBaseUrl; | ||
|
||
httpClient.get(url).subscribe((response) => { | ||
expect(response).toBeTruthy(); | ||
}); | ||
|
||
const req = httpMock.expectOne(ssrBaseUrl + '/items'); | ||
expect(req.request.url).toBe(ssrBaseUrl + '/items'); | ||
req.flush({}); | ||
httpMock.verify(); | ||
}); | ||
|
||
it('should not replace any query param containing the base URL', () => { | ||
const url = 'http://api.example.com/server/items?url=http://api.example.com/server/item/1'; | ||
const ssrBaseUrl = appConfigWithSSR.rest.ssrBaseUrl; | ||
|
||
httpClient.get(url).subscribe((response) => { | ||
expect(response).toBeTruthy(); | ||
}); | ||
|
||
const req = httpMock.expectOne(ssrBaseUrl + '/items?url=http://api.example.com/server/item/1'); | ||
expect(req.request.url).toBe(ssrBaseUrl + '/items?url=http://api.example.com/server/item/1'); | ||
req.flush({}); | ||
httpMock.verify(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { isPlatformBrowser } from '@angular/common'; | ||
import { | ||
HttpEvent, | ||
HttpHandler, | ||
HttpInterceptor, | ||
HttpRequest, | ||
} from '@angular/common/http'; | ||
import { | ||
Inject, | ||
Injectable, | ||
PLATFORM_ID, | ||
} from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { | ||
APP_CONFIG, | ||
AppConfig, | ||
} from '../../../config/app-config.interface'; | ||
import { isEmpty } from '../../shared/empty.util'; | ||
|
||
@Injectable() | ||
/** | ||
* This Interceptor is used to use the configured base URL for the request made during SSR execution | ||
*/ | ||
export class DspaceRestInterceptor implements HttpInterceptor { | ||
|
||
/** | ||
* Contains the configured application base URL | ||
* @protected | ||
*/ | ||
protected baseUrl: string; | ||
protected ssrBaseUrl: string; | ||
|
||
constructor( | ||
@Inject(APP_CONFIG) protected appConfig: AppConfig, | ||
@Inject(PLATFORM_ID) private platformId: string, | ||
) { | ||
this.baseUrl = this.appConfig.rest.baseUrl; | ||
this.ssrBaseUrl = this.appConfig.rest.ssrBaseUrl; | ||
} | ||
|
||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | ||
if (isPlatformBrowser(this.platformId) || isEmpty(this.ssrBaseUrl) || this.baseUrl === this.ssrBaseUrl) { | ||
return next.handle(request); | ||
} | ||
|
||
// Different SSR Base URL specified so replace it in the current request url | ||
const url = request.url.replace(this.baseUrl, this.ssrBaseUrl); | ||
const newRequest: HttpRequest<any> = request.clone({ url }); | ||
return next.handle(newRequest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a basic test to
server-hard-redirect.service.spec.ts
to prove this replacement is working properly? This code appears to be missing specs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done