Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/app/core/url-serializer/bitstream-url-serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { DefaultUrlSerializer, UrlTree } from '@angular/router';
describe('BitstreamUrlSerializer', () => {
let serializer: BitstreamUrlSerializer;

function pathSegments(tree: UrlTree): string[] {
return tree.root.children.primary.segments.map((segment) => segment.path);
}

beforeEach(() => {
TestBed.configureTestingModule({
providers: [BitstreamUrlSerializer]
Expand Down Expand Up @@ -51,4 +55,18 @@ describe('BitstreamUrlSerializer', () => {
const resultUrl = new DefaultUrlSerializer().serialize(result);
expect(resultUrl).toBe(expectedUrl);
});

it('should keep the whole query string when a query value contains a raw "?"', () => {
const result: UrlTree = serializer.parse('/bitstream/handle/123/456/file.pdf?isAllowed=y?x&sequenceId=2');

expect(pathSegments(result)).toEqual(['bitstream', 'handle', '123', '456', 'file.pdf']);
expect(result.queryParams).toEqual({ isAllowed: 'y?x', sequenceId: '2' });
});

it('should not encode a fragment into the filename', () => {
const result: UrlTree = serializer.parse('/bitstream/handle/123/456/file.pdf#page=2');

expect(pathSegments(result)).toEqual(['bitstream', 'handle', '123', '456', 'file.pdf']);
expect(result.fragment).toBe('page=2');
});
});
10 changes: 6 additions & 4 deletions src/app/core/url-serializer/bitstream-url-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export class BitstreamUrlSerializer extends DefaultUrlSerializer {
// Intercept parsing of every URL
parse(url: string): UrlTree {
if (url.startsWith('/bitstream/')) {
// Separate the path from the query string
const [path, query] = url.split('?');
// Separate the path from the query string and fragment (everything from the first raw '?' or '#')
const queryIndex = url.search(/[?#]/);
const path = queryIndex === -1 ? url : url.substring(0, queryIndex);
const queryAndFragment = queryIndex === -1 ? '' : url.substring(queryIndex);
Comment thread
milanmajchrak marked this conversation as resolved.

// Split the path to isolate the filename
const parts = path.split('/');
Expand All @@ -24,8 +26,8 @@ export class BitstreamUrlSerializer extends DefaultUrlSerializer {
// Reconstruct the path with the encoded filename
const newPath = [...parts.slice(0, this.FILENAME_INDEX), encodedFilename].join('/');

// Reattach query string if present
url = query ? `${newPath}?${query}` : newPath;
// Reattach the query string and fragment unchanged
url = newPath + queryAndFragment;
Comment thread
milanmajchrak marked this conversation as resolved.
}
}
return super.parse(url);
Expand Down
Loading