Skip to content

Commit cb86d07

Browse files
Rewrite OAI links in static page HTML with rest.baseUrl (#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.
1 parent 5dfb106 commit cb86d07

2 files changed

Lines changed: 71 additions & 25 deletions

File tree

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { TestBed } from '@angular/core/testing';
22

33
import { StaticPageComponent } from './static-page.component';
44
import { HtmlContentService } from '../shared/html-content.service';
@@ -11,27 +11,25 @@ import { environment } from '../../environments/environment';
1111
import { ClarinSafeHtmlPipe } from '../shared/utils/clarin-safehtml.pipe';
1212

1313
describe('StaticPageComponent', () => {
14-
let component: StaticPageComponent;
15-
let fixture: ComponentFixture<StaticPageComponent>;
16-
17-
let htmlContentService: HtmlContentService;
18-
let appConfig: any;
19-
20-
const htmlContent = '<div id="idShouldNotBeRemoved">TEST MESSAGE</div>';
21-
22-
beforeEach(async () => {
23-
htmlContentService = jasmine.createSpyObj('htmlContentService', {
24-
fetchHtmlContent: of(htmlContent),
25-
getHmtlContentByPathAndLocale: Promise.resolve(htmlContent)
14+
async function setupTest(html: string, restBase?: string) {
15+
const htmlContentService = jasmine.createSpyObj('htmlContentService', {
16+
fetchHtmlContent: of(html),
17+
getHmtlContentByPathAndLocale: Promise.resolve(html)
2618
});
2719

28-
appConfig = Object.assign(environment, {
20+
const appConfig = {
21+
...environment,
2922
ui: {
23+
...(environment as any).ui,
3024
namespace: 'testNamespace'
25+
},
26+
rest: {
27+
...(environment as any).rest,
28+
baseUrl: restBase
3129
}
32-
});
30+
};
3331

34-
TestBed.configureTestingModule({
32+
await TestBed.configureTestingModule({
3533
declarations: [ StaticPageComponent, ClarinSafeHtmlPipe ],
3634
imports: [
3735
TranslateModule.forRoot()
@@ -41,22 +39,66 @@ describe('StaticPageComponent', () => {
4139
{ provide: Router, useValue: new RouterMock() },
4240
{ provide: APP_CONFIG, useValue: appConfig }
4341
]
44-
});
45-
46-
});
42+
}).compileComponents();
4743

48-
beforeEach(() => {
49-
fixture = TestBed.createComponent(StaticPageComponent);
50-
component = fixture.componentInstance;
51-
});
44+
const fixture = TestBed.createComponent(StaticPageComponent);
45+
const component = fixture.componentInstance;
46+
return { fixture, component, htmlContentService };
47+
}
5248

53-
it('should create', () => {
49+
it('should create', async () => {
50+
const { component } = await setupTest('<div>test</div>');
5451
expect(component).toBeTruthy();
5552
});
5653

5754
// Load `TEST MESSAGE`
5855
it('should load html file content', async () => {
56+
const { component } = await setupTest('<div id="idShouldNotBeRemoved">TEST MESSAGE</div>');
5957
await component.ngOnInit();
6058
expect(component.htmlContent.value).toBe('<div id="idShouldNotBeRemoved">TEST MESSAGE</div>');
6159
});
60+
61+
it('should rewrite OAI link with rest.baseUrl', async () => {
62+
const oaiHtml = '<a href="/server/oai/request?verb=ListSets">OAI</a>';
63+
const { fixture, component } = await setupTest(oaiHtml, 'https://api.example.org/rest');
64+
65+
await component.ngOnInit();
66+
fixture.detectChanges();
67+
68+
const rewritten = 'https://api.example.org/server/oai/request?verb=ListSets';
69+
expect(component.htmlContent.value).toContain(rewritten);
70+
const anchor = fixture.nativeElement.querySelector('a');
71+
expect(anchor.getAttribute('href')).toBe(rewritten);
72+
});
73+
74+
it('should leave OAI link unchanged when rest.baseUrl is missing', async () => {
75+
const oaiHtml = '<a href="/server/oai/request?verb=Identify">OAI</a>';
76+
const { fixture, component } = await setupTest(oaiHtml, undefined);
77+
78+
await component.ngOnInit();
79+
fixture.detectChanges();
80+
81+
expect(component.htmlContent.value).toContain('/server/oai/request?verb=Identify');
82+
});
83+
84+
it('should avoid double slashes when rest.baseUrl ends with slash', async () => {
85+
const oaiHtml = '<a href="/server/oai/request?verb=ListRecords">OAI</a>';
86+
const { fixture, component } = await setupTest(oaiHtml, 'https://api.example.org/rest/');
87+
88+
await component.ngOnInit();
89+
fixture.detectChanges();
90+
91+
expect(component.htmlContent.value).toContain('https://api.example.org/server/oai/request?verb=ListRecords');
92+
expect(component.htmlContent.value).not.toContain('//server');
93+
});
94+
95+
it('should leave content unchanged when no OAI link is present', async () => {
96+
const otherHtml = '<a href="/server/other">Other</a>';
97+
const { fixture, component } = await setupTest(otherHtml, 'https://api.example.org/rest');
98+
99+
await component.ngOnInit();
100+
fixture.detectChanges();
101+
102+
expect(component.htmlContent.value).toBe(otherHtml);
103+
});
62104
});

src/app/static-page/static-page.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ export class StaticPageComponent implements OnInit {
2828
// Fetch html file name from the url path. `static/some_file.html`
2929
this.htmlFileName = this.getHtmlFileName();
3030

31-
const htmlContent = await this.htmlContentService.getHmtlContentByPathAndLocale(this.htmlFileName);
31+
let htmlContent = await this.htmlContentService.getHmtlContentByPathAndLocale(this.htmlFileName);
3232
if (isNotEmpty(htmlContent)) {
33+
const restBase = this.appConfig?.rest?.baseUrl;
34+
const oaiUrl = restBase ? new URL('/server/oai', restBase).href : '/server/oai';
35+
htmlContent = htmlContent.replace(/href="\/server\/oai/gi, 'href="' + oaiUrl);
36+
3337
this.htmlContent.next(htmlContent);
3438
return;
3539
}

0 commit comments

Comments
 (0)