Skip to content

Commit 37173c1

Browse files
committed
Accept-Put header
1 parent ab0545f commit 37173c1

File tree

6 files changed

+62
-27
lines changed

6 files changed

+62
-27
lines changed

lib/create-app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const corsSettings = cors({
3232
methods: [
3333
'OPTIONS', 'HEAD', 'GET', 'PATCH', 'POST', 'PUT', 'DELETE'
3434
],
35-
exposedHeaders: 'Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Accept-Post, Updates-Via, Allow, WAC-Allow, Content-Length, WWW-Authenticate, MS-Author-Via, X-Powered-By',
35+
exposedHeaders: 'Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Accept-Post, Accept-Put, Updates-Via, Allow, WAC-Allow, Content-Length, WWW-Authenticate, MS-Author-Via, X-Powered-By',
3636
credentials: true,
3737
maxAge: 1728000,
3838
origin: true,

lib/handlers/get.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function handler (req, res, next) {
3232

3333
res.header('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
3434
res.header('Accept-Post', '*/*')
35+
if (!path.endsWith('/') && !glob.hasMagic(path)) res.header('Accept-Put', '*/*')
3536

3637
// Set live updates
3738
if (ldp.live) {
@@ -53,6 +54,8 @@ async function handler (req, res, next) {
5354
try {
5455
ret = await ldp.get(options, req.accepts(['html', 'turtle', 'rdf+xml', 'n3', 'ld+json']) === 'html')
5556
} catch (err) {
57+
// set Accept-Put if container do not exist
58+
if (err.status === 404 && path.endsWith('/')) res.header('Accept-Put', 'text/turtle')
5659
// use globHandler if magic is detected
5760
if (err.status === 404 && glob.hasMagic(path)) {
5861
debug('forwarding to glob request')

lib/handlers/options.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = handler
88
function handler (req, res, next) {
99
linkServiceEndpoint(req, res)
1010
linkAuthProvider(req, res)
11-
linkSparqlEndpoint(res)
11+
linkAcceptEndpoint(res)
1212

1313
res.status(204)
1414

@@ -28,6 +28,8 @@ function linkServiceEndpoint (req, res) {
2828
addLink(res, serviceEndpoint, 'service')
2929
}
3030

31-
function linkSparqlEndpoint (res) {
32-
res.header('Accept-Patch', 'application/sparql-update')
31+
function linkAcceptEndpoint (res) {
32+
res.header('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
33+
res.header('Accept-Post', '*/*')
34+
res.header('Accept-Put', '*/*')
3335
}

lib/handlers/put.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const { stringToStream } = require('../utils')
88

99
async function handler (req, res, next) {
1010
debug(req.originalUrl)
11-
res.header('MS-Author-Via', 'SPARQL')
11+
// deprecated kept for compatibility
12+
res.header('MS-Author-Via', 'SPARQL') // is this needed ?
1213

1314
const contentType = req.get('content-type')
1415
// check for valid rdf content for auxiliary resource and /profile/card

test/integration/header-test.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,14 @@ describe('Header handler', () => {
3030
})
3131
})
3232

33-
describe('Accept-Patch', () => {
33+
describe('Accept-* for a resource document', () => {
3434
describeHeaderTest('read/append for the public', {
3535
resource: '/public-ra',
3636
headers: {
3737
'Accept-Patch': 'text/n3, application/sparql-update, application/sparql-update-single-match',
38-
'Access-Control-Expose-Headers': /(^|,\s*)Accept-Patch(,|$)/
39-
}
40-
})
41-
})
42-
43-
describe('Accept-Post', () => {
44-
describeHeaderTest('read/append for the public', {
45-
resource: '/public-ra',
46-
headers: {
4738
'Accept-Post': '*/*',
48-
'Access-Control-Expose-Headers': /(^|,\s*)Accept-Post(,|$)/
39+
'Accept-Put': '*/*',
40+
'Access-Control-Expose-Headers': /(^|,\s*)Accept-Patch, Accept-Post, Accept-Put(,|$)/
4941
}
5042
})
5143
})

test/integration/http-test.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,32 @@ describe('HTTP APIs', function () {
110110
.expect('Access-Control-Allow-Origin', 'http://example.com')
111111
.expect('Access-Control-Allow-Credentials', 'true')
112112
.expect('Access-Control-Allow-Methods', 'OPTIONS,HEAD,GET,PATCH,POST,PUT,DELETE')
113-
.expect('Access-Control-Expose-Headers', 'Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Accept-Post, Updates-Via, Allow, WAC-Allow, Content-Length, WWW-Authenticate, MS-Author-Via, X-Powered-By')
113+
.expect('Access-Control-Expose-Headers', 'Authorization, User, Location, Link, Vary, Last-Modified, ETag, Accept-Patch, Accept-Post, Accept-Put, Updates-Via, Allow, WAC-Allow, Content-Length, WWW-Authenticate, MS-Author-Via, X-Powered-By')
114114
.expect(204, done)
115115
})
116116

117-
describe('Accept-Patch header', function () {
117+
describe('Accept-* headers', function () {
118118
it('should be present for resources', function (done) {
119119
server.options('/sampleContainer/example1.ttl')
120-
.expect('Accept-Patch', 'application/sparql-update')
120+
.expect('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
121+
.expect('Accept-Post', '*/*')
122+
.expect('Accept-Put', '*/*')
121123
.expect(204, done)
122124
})
123125

124126
it('should be present for containers', function (done) {
125127
server.options('/sampleContainer/')
126-
.expect('Accept-Patch', 'application/sparql-update')
128+
.expect('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
129+
.expect('Accept-Post', '*/*')
130+
.expect('Accept-Put', '*/*')
127131
.expect(204, done)
128132
})
129133

130134
it('should be present for non-rdf resources', function (done) {
131135
server.options('/sampleContainer/solid.png')
132-
.expect('Accept-Patch', 'application/sparql-update')
136+
.expect('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
137+
.expect('Accept-Post', '*/*')
138+
.expect('Accept-Put', '*/*')
133139
.expect(204, done)
134140
})
135141
})
@@ -314,6 +320,11 @@ describe('HTTP APIs', function () {
314320
server.get('/invalidfile.foo')
315321
.expect(404, done)
316322
})
323+
it('should return 404 for non-existent container', function (done) { // alain
324+
server.get('/inexistant/')
325+
.expect('Accept-Put', 'text/turtle')
326+
.expect(404, done)
327+
})
317328
it('should return basic container link for directories', function (done) {
318329
server.get('/')
319330
.expect('Link', /http:\/\/www.w3.org\/ns\/ldp#BasicContainer/)
@@ -396,13 +407,39 @@ describe('HTTP APIs', function () {
396407
.expect('content-type', /text\/turtle/)
397408
.end(done)
398409
})
399-
it('should still redirect to the right container URI if missing / and HTML is requested',
400-
function (done) {
401-
server.get('/sampleContainer')
402-
.set('accept', 'text/html')
403-
.expect('location', /\/sampleContainer\//)
404-
.expect(301, done)
410+
it('should still redirect to the right container URI if missing / and HTML is requested', function (done) {
411+
server.get('/sampleContainer')
412+
.set('accept', 'text/html')
413+
.expect('location', /\/sampleContainer\//)
414+
.expect(301, done)
415+
})
416+
417+
describe('Accept-* headers', function () {
418+
it('should return 404 for non-existent resource', function (done) {
419+
server.get('/invalidfile.foo')
420+
.expect('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
421+
.expect('Accept-Post', '*/*')
422+
.expect('Accept-put', '*/*')
423+
.expect(404, done)
424+
})
425+
it('Accept-Put=text/turtle for non-existent container', function (done) {
426+
server.get('/inexistant/')
427+
.expect('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
428+
.expect('Accept-Post', '*/*')
429+
.expect('Accept-Put', 'text/turtle')
430+
.expect(404, done)
405431
})
432+
it('Accept-Put header do not exist for existing container', (done) => {
433+
server.get('/sampleContainer/')
434+
.expect(200)
435+
.expect('Accept-Patch', 'text/n3, application/sparql-update, application/sparql-update-single-match')
436+
.expect('Accept-Post', '*/*')
437+
.expect((res) => {
438+
if (res.headers['Accept-Put']) return done(new Error('Accept-Put header should not exist'))
439+
})
440+
.end(done)
441+
})
442+
})
406443
})
407444

408445
describe('HEAD API', function () {

0 commit comments

Comments
 (0)