From e00f6fa60debeeef7c6ee5766c116bcd3fb35370 Mon Sep 17 00:00:00 2001 From: Amad Ul Hassan Date: Thu, 22 Jan 2026 15:43:56 +0100 Subject: [PATCH] Rewrite OAI links in static page HTML with rest.baseUrl (ufal/dspace-angular#90) * Rewrite OAI links in static page HTML with rest.baseUrl Updated StaticPageComponent to rewrite OAI links in loaded HTML content to use the configured rest.baseUrl, ensuring correct API endpoint references. Added comprehensive tests to verify link rewriting, handling of missing baseUrl, avoidance of double slashes, and cases with no OAI links. * Remove unused ComponentFixture import in test Cleaned up the static-page.component.spec.ts file by removing the unused ComponentFixture import to improve code clarity. * Fix OAI URL construction and improve test coverage Corrects the construction of the OAI URL in StaticPageComponent to avoid double slashes by removing the extra slash in the base URL. Also updates the unit test to properly instantiate the component and check its creation. (cherry picked from commit cb86d0778d8b5808d75b95358380a0012e79af4f) --- .../static-page/static-page.component.spec.ts | 90 ++++++++++++++----- src/app/static-page/static-page.component.ts | 6 +- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/src/app/static-page/static-page.component.spec.ts b/src/app/static-page/static-page.component.spec.ts index 1ad4e607c3c..0c461042381 100644 --- a/src/app/static-page/static-page.component.spec.ts +++ b/src/app/static-page/static-page.component.spec.ts @@ -1,4 +1,4 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { TestBed } from '@angular/core/testing'; import { StaticPageComponent } from './static-page.component'; import { HtmlContentService } from '../shared/html-content.service'; @@ -11,27 +11,25 @@ import { environment } from '../../environments/environment'; import { ClarinSafeHtmlPipe } from '../shared/utils/clarin-safehtml.pipe'; describe('StaticPageComponent', () => { - let component: StaticPageComponent; - let fixture: ComponentFixture; - - let htmlContentService: HtmlContentService; - let appConfig: any; - - const htmlContent = '
TEST MESSAGE
'; - - beforeEach(async () => { - htmlContentService = jasmine.createSpyObj('htmlContentService', { - fetchHtmlContent: of(htmlContent), - getHmtlContentByPathAndLocale: Promise.resolve(htmlContent) + async function setupTest(html: string, restBase?: string) { + const htmlContentService = jasmine.createSpyObj('htmlContentService', { + fetchHtmlContent: of(html), + getHmtlContentByPathAndLocale: Promise.resolve(html) }); - appConfig = Object.assign(environment, { + const appConfig = { + ...environment, ui: { + ...(environment as any).ui, namespace: 'testNamespace' + }, + rest: { + ...(environment as any).rest, + baseUrl: restBase } - }); + }; - TestBed.configureTestingModule({ + await TestBed.configureTestingModule({ declarations: [ StaticPageComponent, ClarinSafeHtmlPipe ], imports: [ TranslateModule.forRoot() @@ -41,22 +39,66 @@ describe('StaticPageComponent', () => { { provide: Router, useValue: new RouterMock() }, { provide: APP_CONFIG, useValue: appConfig } ] - }); - - }); + }).compileComponents(); - beforeEach(() => { - fixture = TestBed.createComponent(StaticPageComponent); - component = fixture.componentInstance; - }); + const fixture = TestBed.createComponent(StaticPageComponent); + const component = fixture.componentInstance; + return { fixture, component, htmlContentService }; + } - it('should create', () => { + it('should create', async () => { + const { component } = await setupTest('
test
'); expect(component).toBeTruthy(); }); // Load `TEST MESSAGE` it('should load html file content', async () => { + const { component } = await setupTest('
TEST MESSAGE
'); await component.ngOnInit(); expect(component.htmlContent.value).toBe('
TEST MESSAGE
'); }); + + it('should rewrite OAI link with rest.baseUrl', async () => { + const oaiHtml = 'OAI'; + const { fixture, component } = await setupTest(oaiHtml, 'https://api.example.org/rest'); + + await component.ngOnInit(); + fixture.detectChanges(); + + const rewritten = 'https://api.example.org/server/oai/request?verb=ListSets'; + expect(component.htmlContent.value).toContain(rewritten); + const anchor = fixture.nativeElement.querySelector('a'); + expect(anchor.getAttribute('href')).toBe(rewritten); + }); + + it('should leave OAI link unchanged when rest.baseUrl is missing', async () => { + const oaiHtml = 'OAI'; + const { fixture, component } = await setupTest(oaiHtml, undefined); + + await component.ngOnInit(); + fixture.detectChanges(); + + expect(component.htmlContent.value).toContain('/server/oai/request?verb=Identify'); + }); + + it('should avoid double slashes when rest.baseUrl ends with slash', async () => { + const oaiHtml = 'OAI'; + const { fixture, component } = await setupTest(oaiHtml, 'https://api.example.org/rest/'); + + await component.ngOnInit(); + fixture.detectChanges(); + + expect(component.htmlContent.value).toContain('https://api.example.org/server/oai/request?verb=ListRecords'); + expect(component.htmlContent.value).not.toContain('//server'); + }); + + it('should leave content unchanged when no OAI link is present', async () => { + const otherHtml = 'Other'; + const { fixture, component } = await setupTest(otherHtml, 'https://api.example.org/rest'); + + await component.ngOnInit(); + fixture.detectChanges(); + + expect(component.htmlContent.value).toBe(otherHtml); + }); }); diff --git a/src/app/static-page/static-page.component.ts b/src/app/static-page/static-page.component.ts index bb19403a704..aff3eed0aed 100644 --- a/src/app/static-page/static-page.component.ts +++ b/src/app/static-page/static-page.component.ts @@ -28,8 +28,12 @@ export class StaticPageComponent implements OnInit { // Fetch html file name from the url path. `static/some_file.html` this.htmlFileName = this.getHtmlFileName(); - const htmlContent = await this.htmlContentService.getHmtlContentByPathAndLocale(this.htmlFileName); + let htmlContent = await this.htmlContentService.getHmtlContentByPathAndLocale(this.htmlFileName); if (isNotEmpty(htmlContent)) { + const restBase = this.appConfig?.rest?.baseUrl; + const oaiUrl = restBase ? new URL('/server/oai', restBase).href : '/server/oai'; + htmlContent = htmlContent.replace(/href="\/server\/oai/gi, 'href="' + oaiUrl); + this.htmlContent.next(htmlContent); return; }