Skip to content

Commit

Permalink
Container-specific link header
Browse files Browse the repository at this point in the history
  • Loading branch information
srosset81 committed Dec 13, 2024
1 parent 872b5d1 commit 9b70891
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/middleware/packages/ldp/mixins/controlled-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
post: `${this.name}.post`,
list: `${this.name}.list`,
get: `${this.name}.get`,
getHeaderLinks: `${this.name}.getHeaderLinks`,
create: `${this.name}.create`,
patch: `${this.name}.patch`,
put: `${this.name}.put`,
Expand Down Expand Up @@ -72,6 +73,9 @@ module.exports = {
...ctx.params
});
},
getHeaderLinks(ctx) {
return [];
},
create(ctx) {
return ctx.call('ldp.resource.create', ctx.params);
},
Expand Down
17 changes: 14 additions & 3 deletions src/middleware/packages/ldp/services/link-header/actions/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ module.exports = {
},
async handler(ctx) {
const { uri } = ctx.params;
const link = new LinkHeader();
const linkHeader = new LinkHeader();

for (const actionName of this.registeredActionNames) {
const params = await ctx.call(actionName, { uri });

if (!params.uri) throw new Error(`An uri should be returned from the ${actionName} action`);

link.set(params);
linkHeader.set(params);
}

return link.toString();
// Get container-specific headers (if any)
const { controlledActions } = await ctx.call('ldp.registry.getByUri', { resourceUri: uri });
if (controlledActions?.getHeaderLinks) {
const links = await ctx.call(controlledActions.getHeaderLinks, { uri });
if (links && links.length > 0) {
for (const link of links) {
linkHeader.set(link);
}
}
}

return linkHeader.toString();
}
};
67 changes: 67 additions & 0 deletions src/middleware/tests/ldp/headers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const urlJoin = require('url-join');
const { parse: parseLinkHeader } = require('http-link-header');
const { fetchServer } = require('../utils');
const initialize = require('./initialize');
const CONFIG = require('../config');

jest.setTimeout(20000);
let broker;

beforeAll(async () => {
broker = await initialize();
});
afterAll(async () => {
if (broker) await broker.stop();
});

describe('Headers handling of LDP server', () => {
test('Get headers', async () => {
const { headers: postHeaders } = await fetchServer(urlJoin(CONFIG.HOME_URL, 'places'), {
method: 'POST',
body: {
'@type': 'pair:Place',
'pair:label': 'My place'
}
});

const resourceUri = postHeaders.get('Location');
const resourcePath = new URL(resourceUri).pathname;

const { headers } = await fetchServer(resourceUri, {
method: 'HEAD'
});

const parsedLinks = parseLinkHeader(headers.get('link'));

expect(parsedLinks.refs).toMatchObject([
{
uri: urlJoin(CONFIG.HOME_URL, '_acl', resourcePath),
rel: 'acl'
}
]);
});

test('Get container-specific headers', async () => {
const { headers: postHeaders } = await fetchServer(urlJoin(CONFIG.HOME_URL, 'pair', 'event'), {
method: 'POST',
body: {
'@type': 'pair:Event',
'pair:label': 'My event'
}
});

const resourceUri = postHeaders.get('Location');
const resourcePath = new URL(resourceUri).pathname;

const { headers } = await fetchServer(resourceUri, {
method: 'HEAD'
});

const parsedLinks = parseLinkHeader(headers.get('link'));

expect(parsedLinks.refs).toMatchObject([
{ uri: urlJoin(CONFIG.HOME_URL, '_acl', resourcePath), rel: 'acl' },
{ uri: 'http://foo.bar', rel: 'http://foo.baz' }
]);
});
});
21 changes: 20 additions & 1 deletion src/middleware/tests/ldp/initialize.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { ServiceBroker } = require('moleculer');
const fs = require('fs');
const urlJoin = require('url-join');
const { join: pathJoin } = require('path');
const { CoreService } = require('@semapps/core');
const { pair, petr } = require('@semapps/ontologies');
const { WebAclMiddleware, CacherMiddleware } = require('@semapps/webacl');
const { AuthLocalService } = require('@semapps/auth');
const { ControlledContainerMixin } = require('@semapps/ldp');
const path = require('path');
const CONFIG = require('../config');
const { clearDataset } = require('../utils');
Expand Down Expand Up @@ -95,6 +95,25 @@ const initialize = async () => {
}
});

broker.createService({
name: 'event',
mixins: [ControlledContainerMixin],
settings: {
acceptedTypes: ['pair:Event'],
permissions
},
actions: {
getHeaderLinks() {
return [
{
uri: 'http://foo.bar',
rel: 'http://foo.baz'
}
];
}
}
});

await broker.start();

return broker;
Expand Down
1 change: 1 addition & 0 deletions src/middleware/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dotenv-flow": "^3.1.0",
"fs-extra": "^9.0.1",
"ioredis": "^4.27.0",
"http-link-header": "^1.1.1",
"lru-cache": "10.1.0",
"moleculer": "^0.14.17",
"moleculer-web": "^0.10.0-beta1",
Expand Down

0 comments on commit 9b70891

Please sign in to comment.