Skip to content

Commit 4880d8a

Browse files
[IIIF-188] refactor to use param in method
1 parent 2aee6d2 commit 4880d8a

5 files changed

Lines changed: 13 additions & 10 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ describe('BitstreamDownloadRedirectGuard', () => {
162162
it('should redirect to the content link', waitForAsync(() => {
163163
TestBed.runInInjectionContext(() => {
164164
resolver(route, state).subscribe(() => {
165-
expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link');
166-
}
165+
expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link', null, true);
166+
},
167167
);
168168
});
169169
}));
@@ -176,7 +176,7 @@ describe('BitstreamDownloadRedirectGuard', () => {
176176
it('should redirect to an updated content link', waitForAsync(() => {
177177
TestBed.runInInjectionContext(() => {
178178
resolver(route, state).subscribe(() => {
179-
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers');
179+
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers', null, true);
180180
});
181181
});
182182
}));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ export const bitstreamDownloadRedirectGuard: CanActivateFn = (
6666
}),
6767
map(([isAuthorized, isLoggedIn, bitstream, fileLink]: [boolean, boolean, Bitstream, string]) => {
6868
if (isAuthorized && isLoggedIn && isNotEmpty(fileLink)) {
69-
hardRedirectService.redirect(fileLink);
69+
hardRedirectService.redirect(fileLink, null, true);
7070
return false;
7171
} else if (isAuthorized && !isLoggedIn && !hasValue(accessToken)) {
72-
hardRedirectService.redirect(bitstream._links.content.href);
72+
hardRedirectService.redirect(bitstream._links.content.href, null, true);
7373
return false;
7474
} else if (!isAuthorized) {
7575
// Either we have an access token, or we are logged in, or we are not logged in.
7676
// For now, the access token does not care if we are logged in or not.
7777
if (hasValue(accessToken)) {
78-
hardRedirectService.redirect(bitstream._links.content.href + '?accessToken=' + accessToken);
78+
hardRedirectService.redirect(bitstream._links.content.href + '?accessToken=' + accessToken, null, true);
7979
return false;
8080
} else if (isLoggedIn) {
8181
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
@@ -28,8 +28,9 @@ export class ServerHardRedirectService extends HardRedirectService {
2828
* the page to redirect to
2929
* @param statusCode
3030
* optional HTTP status code to use for redirect (default = 302, which is a temporary redirect)
31+
* @param shouldSetCorsHeader
3132
*/
32-
redirect(url: string, statusCode?: number) {
33+
redirect(url: string, statusCode?: number, shouldSetCorsHeader?: boolean) {
3334
if (url === this.req.url) {
3435
return;
3536
}
@@ -59,7 +60,7 @@ export class ServerHardRedirectService extends HardRedirectService {
5960
status = 302;
6061
}
6162

62-
if (this.req.path.endsWith('download')) {
63+
if (shouldSetCorsHeader) {
6364
this.setCorsHeader();
6465
}
6566

0 commit comments

Comments
 (0)