Skip to content

Commit

Permalink
prep
Browse files Browse the repository at this point in the history
  • Loading branch information
bourgeoa committed Apr 9, 2024
1 parent 97618fd commit ff444cb
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/handlers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ async function handler (req, res, next) {
res.statusCode = 206
}

if (isRdf(contentType) && !res.sendEvents({
// PREP response
/* if (isRdf(contentType) && !res.sendEvents({
config: { prep: '' },
body: stream,
headers
})) return
})) return */
res.set(headers)
return stream.pipe(res)
}
Expand Down
1 change: 1 addition & 0 deletions lib/handlers/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ async function patchHandler (req, res, next) {

// Send the result to the client
res.send(result)
// PREP response
res.events.prep.trigger({
generateNotifications: () => `\r\n${rfc822Template({ res })}`
})
Expand Down
131 changes: 131 additions & 0 deletions test/integration/prep-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Integration tests for PATCH with text/n3
const { assert } = require('chai')
const ldnode = require('../../index')
const path = require('path')
const supertest = require('supertest')
const fs = require('fs')
const { read, rm, backup, restore } = require('../utils')

// Server settings
const port = 7777
const serverUri = `https://tim.localhost:${port}`
const root = path.join(__dirname, '../resources/patch')
const configPath = path.join(__dirname, '../resources/config')
const serverOptions = {
root,
configPath,
serverUri,
multiuser: false,
webid: true,
sslKey: path.join(__dirname, '../keys/key.pem'),
sslCert: path.join(__dirname, '../keys/cert.pem'),
forceUser: `${serverUri}/profile/card#me`
}

describe('PATCH through text/n3', () => {
let request
let server

// Start the server
before(done => {
server = ldnode.createServer(serverOptions)
server.listen(port, done)
request = supertest(serverUri)
})

after(() => {
server.close()
})

describe('with a patch document', () => {
describe('on a resource with append-only access', describePatch({
path: '/append-only.ttl',
patch: `<> a solid:InsertDeletePatch;
solid:inserts { <x> <y> <z>. }.`
}, { // expected:
status: 200,
text: 'Patch applied successfully',
result: '@prefix : </append-only.ttl#>.\n@prefix tim: </>.\n\ntim:a tim:b tim:c.\n\ntim:d tim:e tim:f.\n\ntim:x tim:y tim:z.\n\n'
}))

describe('on a resource with write-only access', describePatch({
path: '/write-only.ttl',
patch: `<> a solid:InsertDeletePatch;
solid:inserts { <x> <y> <z>. }.`
}, { // expected:
status: 200,
text: 'Patch applied successfully',
result: '@prefix : </write-only.ttl#>.\n@prefix tim: </>.\n\ntim:a tim:b tim:c.\n\ntim:d tim:e tim:f.\n\ntim:x tim:y tim:z.\n\n'
}))
})

// Creates a PATCH test for the given resource with the given expected outcomes
function describePatch ({ path, exists = true, patch, contentType = 'text/n3' },
{ status = 200, text, result }) {
return () => {
const filename = `patch${path}`
let originalContents
// Back up and restore an existing file
if (exists) {
before(() => backup(filename))
after(() => restore(filename))
// Store its contents to verify non-modification
if (!result) {
originalContents = read(filename)
}
// Ensure a non-existing file is removed
} else {
before(() => rm(filename))
after(() => rm(filename))
}

// Create the request and obtain the response
let response
before((done) => {
request.patch(path)
.set('Content-Type', contentType)
.send(`@prefix solid: <http://www.w3.org/ns/solid/terms#>.\n${patch}`)
.then(res => { response = res })
.then(done, done)
})

// Verify the response's status code and body text
it(`returns HTTP status code ${status}`, () => {
assert.isObject(response)
assert.equal(response.statusCode, status)
})
it(`has "${text}" in the response`, () => {
assert.isObject(response)
assert.include(response.text, text)
})

// For existing files, verify correct patch application
if (exists) {
if (result) {
it('patches the file correctly', () => {
assert.equal(read(filename), result)
})
} else {
it('does not modify the file', () => {
assert.equal(read(filename), originalContents)
})
}
// For non-existing files, verify creation and contents
} else {
if (result) {
it('creates the file', () => {
assert.isTrue(fs.existsSync(`${root}/${path}`))
})

it('writes the correct contents', () => {
assert.equal(read(filename), result)
})
} else {
it('does not create the file', () => {
assert.isFalse(fs.existsSync(`${root}/${path}`))
})
}
}
}
}
})

0 comments on commit ff444cb

Please sign in to comment.