Skip to content

Commit 68082f4

Browse files
[IIIF-188] refactor to use param in method
1 parent 9a58f96 commit 68082f4

5 files changed

Lines changed: 12 additions & 9 deletions

File tree

src/app/bitstream-page/bitstream-download-redirect.guard.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('BitstreamDownloadRedirectGuard', () => {
169169
it('should redirect to the content link', waitForAsync(() => {
170170
TestBed.runInInjectionContext(() => {
171171
resolver(route, state).subscribe(() => {
172-
expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link');
172+
expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link', null, true);
173173
},
174174
);
175175
});
@@ -183,7 +183,7 @@ describe('BitstreamDownloadRedirectGuard', () => {
183183
it('should redirect to an updated content link', waitForAsync(() => {
184184
TestBed.runInInjectionContext(() => {
185185
resolver(route, state).subscribe(() => {
186-
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers');
186+
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers', null, true);
187187
});
188188
});
189189
}));

src/app/bitstream-page/bitstream-download-redirect.guard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ export const bitstreamDownloadRedirectGuard: CanActivateFn = (
7878
}),
7979
map(([isAuthorized, isLoggedIn, bitstream, fileLink]: [boolean, boolean, Bitstream, string]) => {
8080
if (isAuthorized && isLoggedIn && isNotEmpty(fileLink)) {
81-
hardRedirectService.redirect(fileLink);
81+
hardRedirectService.redirect(fileLink, null, true);
8282
return false;
8383
} else if (isAuthorized && !isLoggedIn && !hasValue(accessToken)) {
84-
hardRedirectService.redirect(bitstream._links.content.href);
84+
hardRedirectService.redirect(bitstream._links.content.href, null, true);
8585
return false;
8686
} else if (!isAuthorized) {
8787
// Either we have an access token, or we are logged in, or we are not logged in.
8888
// For now, the access token does not care if we are logged in or not.
8989
if (hasValue(accessToken)) {
90-
hardRedirectService.redirect(bitstream._links.content.href + '?accessToken=' + accessToken);
90+
hardRedirectService.redirect(bitstream._links.content.href + '?accessToken=' + accessToken, null, true);
9191
return false;
9292
} else if (isLoggedIn) {
9393
return router.createUrlTree([getForbiddenRoute()]);

src/app/core/services/hard-redirect.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export abstract class HardRedirectService {
1313
* the page to redirect to
1414
* @param statusCode
1515
* optional HTTP status code to use for redirect (default = 302, which is a temporary redirect)
16+
* @param shouldSetCorsHeader
17+
* optional to prevent CORS error on redirect
1618
*/
17-
abstract redirect(url: string, statusCode?: number);
19+
abstract redirect(url: string, statusCode?: number, shouldSetCorsHeader?: boolean);
1820

1921
/**
2022
* Get the current route, with query params included

src/app/core/services/server-hard-redirect.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('ServerHardRedirectService', () => {
102102
service = new ServerHardRedirectService(environmentWithSSRUrl, mockRequest, mockResponse, serverResponseService);
103103

104104
beforeEach(() => {
105-
service.redirect(redirect);
105+
service.redirect(redirect, null, true);
106106
});
107107

108108
it('should set header', () => {

src/app/core/services/server-hard-redirect.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ export class ServerHardRedirectService extends HardRedirectService {
4141
* the page to redirect to
4242
* @param statusCode
4343
* optional HTTP status code to use for redirect (default = 302, which is a temporary redirect)
44+
* @param shouldSetCorsHeader
4445
*/
45-
redirect(url: string, statusCode?: number) {
46+
redirect(url: string, statusCode?: number, shouldSetCorsHeader?: boolean) {
4647
if (url === this.req.url) {
4748
return;
4849
}
@@ -72,7 +73,7 @@ export class ServerHardRedirectService extends HardRedirectService {
7273
status = 302;
7374
}
7475

75-
if (this.req.path.endsWith('download')) {
76+
if (shouldSetCorsHeader) {
7677
this.setCorsHeader();
7778
}
7879

0 commit comments

Comments
 (0)