|
| 1 | +import { getMockObjectCacheService } from '../../shared/mocks/object-cache.service.mock'; |
| 2 | +import { ObjectCacheService } from '../cache/object-cache.service'; |
| 3 | +import { RawRestResponse } from '../dspace-rest/raw-rest-response.model'; |
| 4 | +import { RestRequest } from './rest-request.model'; |
| 5 | +import { RestRequestMethod } from './rest-request-method'; |
| 6 | +import { DspaceRestResponseParsingService } from './dspace-rest-response-parsing.service'; |
| 7 | + |
| 8 | +class TestService extends DspaceRestResponseParsingService { |
| 9 | + constructor(protected objectCache: ObjectCacheService) { |
| 10 | + super(objectCache); |
| 11 | + } |
| 12 | + |
| 13 | + public ensureSelfLinkForTest(request: RestRequest, response: RawRestResponse): RawRestResponse { |
| 14 | + return this.ensureSelfLink(request, response); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe('DspaceRestResponseParsingService', () => { |
| 19 | + let service: TestService; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + service = new TestService(getMockObjectCacheService()); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('ensureSelfLink', () => { |
| 26 | + let warnSpy: jasmine.Spy; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + warnSpy = spyOn(console, 'warn'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('does not replace self link when only query params differ', () => { |
| 33 | + const request = { |
| 34 | + uuid: 'request-id', |
| 35 | + href: 'https://rest.test/server/api/core/items/f639b124-1234-1234-1234-abcdef123456?projection=preventMetadataSecurity', |
| 36 | + method: RestRequestMethod.GET, |
| 37 | + } as RestRequest; |
| 38 | + const response: RawRestResponse = { |
| 39 | + payload: { |
| 40 | + _links: { |
| 41 | + self: { |
| 42 | + href: 'https://rest.test/server/api/core/items/f639b124-1234-1234-1234-abcdef123456', |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + statusCode: 200, |
| 47 | + statusText: 'OK', |
| 48 | + }; |
| 49 | + |
| 50 | + const result = service.ensureSelfLinkForTest(request, response); |
| 51 | + |
| 52 | + expect(result.payload._links.self.href).toBe('https://rest.test/server/api/core/items/f639b124-1234-1234-1234-abcdef123456'); |
| 53 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('replaces self link when path differs', () => { |
| 57 | + const request = { |
| 58 | + uuid: 'request-id', |
| 59 | + href: 'https://rest.test/server/api/core/items/f639b124-1234-1234-1234-abcdef123456', |
| 60 | + method: RestRequestMethod.GET, |
| 61 | + } as RestRequest; |
| 62 | + const response: RawRestResponse = { |
| 63 | + payload: { |
| 64 | + _links: { |
| 65 | + self: { |
| 66 | + href: 'https://rest.test/server/api/core/items/some-other-id', |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + statusCode: 200, |
| 71 | + statusText: 'OK', |
| 72 | + }; |
| 73 | + |
| 74 | + const result = service.ensureSelfLinkForTest(request, response); |
| 75 | + |
| 76 | + expect(result.payload._links.self.href).toBe('https://rest.test/server/api/core/items/f639b124-1234-1234-1234-abcdef123456'); |
| 77 | + expect(warnSpy).toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
0 commit comments