From d7b7bd9667581f9956f22ec402f30ad08fdc5fe8 Mon Sep 17 00:00:00 2001 From: Rahul Gupta Date: Sat, 1 Jun 2024 05:25:12 +0530 Subject: [PATCH 1/2] fix: Return 201 when PATCH creates a new resource A PATCH request may end up creating a new resource, in which case a 201 status code should be returned. --- lib/handlers/patch.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/handlers/patch.js b/lib/handlers/patch.js index 8001c9f5..04bcba1e 100644 --- a/lib/handlers/patch.js +++ b/lib/handlers/patch.js @@ -92,6 +92,7 @@ async function patchHandler (req, res, next) { }) // Send the result to the client + res.sendStatus(resourceExists ? 200 : 201) res.send(result) } catch (err) { return next(err) From a69da13884f8a5b4d071554ec0dca44bf4dd9708 Mon Sep 17 00:00:00 2001 From: bourgeoa Date: Sun, 2 Jun 2024 17:48:37 +0200 Subject: [PATCH 2/2] replace res.sendStatus with res.status and update tests --- lib/handlers/patch.js | 4 ++-- test/integration/acl-oidc-test.js | 4 ++-- test/integration/patch-sparql-update-test.js | 2 +- test/integration/patch-test.js | 10 +++++----- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/handlers/patch.js b/lib/handlers/patch.js index 04bcba1e..481027fd 100644 --- a/lib/handlers/patch.js +++ b/lib/handlers/patch.js @@ -91,8 +91,8 @@ async function patchHandler (req, res, next) { return writeGraph(graph, resource, ldp.resourceMapper.resolveFilePath(req.hostname), ldp.serverUri) }) - // Send the result to the client - res.sendStatus(resourceExists ? 200 : 201) + // Send the status and result to the client + res.status(resourceExists ? 200 : 201) res.send(result) } catch (err) { return next(err) diff --git a/test/integration/acl-oidc-test.js b/test/integration/acl-oidc-test.js index 77919a35..a5a31270 100644 --- a/test/integration/acl-oidc-test.js +++ b/test/integration/acl-oidc-test.js @@ -557,7 +557,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.headers['content-type'] = 'application/sparql-update' request.patch(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 200) + assert.equal(response.statusCode, 201) done() }) }) @@ -616,7 +616,7 @@ describe('ACL with WebID+OIDC over HTTP', function () { options.headers['content-type'] = 'application/sparql-update' request.patch(options, function (error, response, body) { assert.equal(error, null) - assert.equal(response.statusCode, 200) + assert.equal(response.statusCode, 201) done() }) }) diff --git a/test/integration/patch-sparql-update-test.js b/test/integration/patch-sparql-update-test.js index 3e8a1ad2..69ea9b01 100644 --- a/test/integration/patch-sparql-update-test.js +++ b/test/integration/patch-sparql-update-test.js @@ -22,7 +22,7 @@ describe('PATCH through application/sparql-update', function () { server.patch('/notExisting.ttl') .set('content-type', 'application/sparql-update') .send('INSERT DATA { :test :hello 456 .}') - .expect(200) + .expect(201) .end(function (err, res, body) { assert.equal( read('sampleContainer/notExisting.ttl'), diff --git a/test/integration/patch-test.js b/test/integration/patch-test.js index 846f0480..9d3d6f13 100644 --- a/test/integration/patch-test.js +++ b/test/integration/patch-test.js @@ -79,7 +79,7 @@ describe('PATCH through text/n3', () => { patch: `<> a solid:InsertDeletePatch; solid:inserts { . }.` }, { // expected: - status: 200, + status: 201, text: 'Patch applied successfully', result: '@prefix : .\n@prefix tim: .\n\ntim:x tim:y tim:z.\n\n' })) @@ -90,7 +90,7 @@ describe('PATCH through text/n3', () => { patch: `<> a solid:InsertDeletePatch; solid:inserts { . }.` }, { // expected: - status: 200, + status: 201, text: 'Patch applied successfully', // result: '{\n "@id": "/x",\n "/y": {\n "@id": "/z"\n }\n}' result: `{ @@ -110,7 +110,7 @@ describe('PATCH through text/n3', () => { patch: `<> a solid:InsertDeletePatch; solid:inserts { . }.` }, { // expected: - status: 200, + status: 201, text: 'Patch applied successfully', result: ` { patch: `<> a solid:InsertDeletePatch; solid:inserts { . }.` }, { // expected: - status: 200, + status: 201, text: 'Patch applied successfully', result: '@prefix : .\n@prefix tim: .\n\ntim:x tim:y tim:z.\n\n' })) @@ -186,7 +186,7 @@ describe('PATCH through text/n3', () => { patch: `<> a solid:InsertDeletePatch; solid:inserts { . }.` }, { - status: 200, + status: 201, text: 'Patch applied successfully', result: '@prefix : <#>.\n@prefix fol: <./>.\n\nfol:x fol:y fol:z.\n\n' }))