Skip to content

Commit c541613

Browse files
committed
Moving code around, to handle streams correctly
And make old tests pass
1 parent ebfe3f3 commit c541613

File tree

6 files changed

+127
-30
lines changed

6 files changed

+127
-30
lines changed

lib/handlers/put.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = handler
22

3+
const bodyParser = require('body-parser')
34
const debug = require('debug')('solid:put')
45
const getContentType = require('../utils').getContentType
56

@@ -20,3 +21,45 @@ async function handler (req, res, next) {
2021
return next(err)
2122
}
2223
}
24+
25+
// function handler (req, res, next) {
26+
// const ldp = req.app.locals.ldp
27+
// debug(req.originalUrl)
28+
// res.header('MS-Author-Via', 'SPARQL')
29+
//
30+
// const contentType = req.get('content-type')
31+
// if (ldp.mimetypeIsRdf(contentType)) {
32+
// return bodyParser.text({ type: () => true })(req, res, () => putText(req, res, next))
33+
// }
34+
// return putStream(req, res, next)
35+
// }
36+
//
37+
// function putText (req, res, next) {
38+
// const ldp = req.app.locals.ldp
39+
// const contentType = req.get('content-type')
40+
// const requestUri = `${req.protocol}//${req.get('host')}${req.originalUrl}`
41+
// if (ldp.isValidRdf(req.body, requestUri, contentType)) {
42+
// return ldp.putText(req.hostname, req.path, req.body, handleCallback(res, next))
43+
// }
44+
// next(error(400, 'RDF file contains invalid syntax'))
45+
// }
46+
//
47+
// function putStream (req, res, next) {
48+
// const ldp = req.app.locals.ldp
49+
// ldp.putStream(req.hostname, req.path, req, handleCallback(res, next))
50+
// }
51+
//
52+
// function handleCallback (res, next) {
53+
// return function (err) {
54+
// if (err) {
55+
// debug('error putting the file:' + err.message)
56+
// err.message = 'Can\'t write file: ' + err.message
57+
// return next(err)
58+
// }
59+
//
60+
// debug('succeded putting the file')
61+
//
62+
// res.sendStatus(201)
63+
// return next()
64+
// }
65+
// }

lib/handlers/validate.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/ldp-middleware.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const del = require('./handlers/delete')
1010
const patch = require('./handlers/patch')
1111
const index = require('./handlers/index')
1212
const copy = require('./handlers/copy')
13-
// const validate = require('./handlers/validate')
1413

1514
function LdpMiddleware (corsSettings) {
1615
const router = express.Router('/')

lib/ldp.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ class LDP {
203203
return await this.put(path, stream, contentType)
204204
}
205205

206+
// isValidRdf (body, requestUri, contentType) {
207+
// const resourceGraph = $rdf.graph()
208+
// try {
209+
// $rdf.parse(body, resourceGraph, requestUri, contentType)
210+
// } catch (err) {
211+
// debug.handlers('VALIDATE -- Error parsing data: ' + err)
212+
// return false
213+
// }
214+
// return true
215+
// }
216+
206217
async put (url, stream, contentType) {
207218
// PUT requests not supported on containers. Use POST instead
208219
if ((url.url || url).endsWith('/')) {
@@ -452,7 +463,7 @@ class LDP {
452463
return ensureNotExists(this, path.join(containerURI, filename))
453464
}
454465

455-
static mimetypeIsRdf (mimeType) {
466+
mimetypeIsRdf (mimeType) {
456467
return RDF_MIME_TYPES.indexOf(mimeType) !== -1
457468
}
458469
}

test/integration/validate-tts.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const { setupSuperServer } = require('../utils')
4+
5+
const server = setupSuperServer({
6+
live: true,
7+
dataBrowserPath: 'default',
8+
root: path.join(__dirname, '../resources'),
9+
auth: 'oidc',
10+
webid: false
11+
})
12+
13+
describe('Validate HTTP requests with Turtle syntax', function () {
14+
describe('PUT API', function () {
15+
const putRequestBody = fs.readFileSync(path.join(__dirname, '../resources/sampleContainer/invalid.ttl'), {
16+
'encoding': 'utf8'
17+
})
18+
it('should return 400', function (done) {
19+
server.put('/put-resource-1.ttl')
20+
.send(putRequestBody)
21+
.set('content-type', 'text/turtle')
22+
.expect(400, done)
23+
})
24+
})
25+
26+
// describe('POST (multipart)', function () {
27+
// it('should create as many files as the ones passed in multipart',
28+
// function (done) {
29+
// server.post('/sampleContainer/')
30+
// .attach('timbl', path.join(__dirname, '../resources/timbl.jpg'))
31+
// .attach('nicola', path.join(__dirname, '../resources/nicola.jpg'))
32+
// .expect(200)
33+
// .end(function (err) {
34+
// if (err) return done(err)
35+
//
36+
// var sizeNicola = fs.statSync(path.join(__dirname,
37+
// '../resources/nicola.jpg')).size
38+
// var sizeTim = fs.statSync(path.join(__dirname, '../resources/timbl.jpg')).size
39+
// var sizeNicolaLocal = fs.statSync(path.join(__dirname,
40+
// '../resources/sampleContainer/nicola.jpg')).size
41+
// var sizeTimLocal = fs.statSync(path.join(__dirname,
42+
// '../resources/sampleContainer/timbl.jpg')).size
43+
//
44+
// if (sizeNicola === sizeNicolaLocal && sizeTim === sizeTimLocal) {
45+
// return done()
46+
// } else {
47+
// return done(new Error('Either the size (remote/local) don\'t match or files are not stored'))
48+
// }
49+
// })
50+
// })
51+
// after(function () {
52+
// // Clean up after POST (multipart) API tests
53+
// return Promise.all([
54+
// rm('/sampleContainer/nicola.jpg'),
55+
// rm('/sampleContainer/timbl.jpg')
56+
// ])
57+
// })
58+
// })
59+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@prefix ldp: <http://www.w3.org/ns/ldp#>.
2+
@prefix o: <http://example.org/ontology#>.
3+
4+
<http://example.org/netWorth/nw1/>
5+
test o:NetWorth;
6+
o:netWorthOf <http://example.org/users/JohnZSmith>;
7+
o:asset
8+
<assets/a1>,
9+
<assets/a2>;
10+
o:liability
11+
<liabilities/l1>,
12+
<liabilities/l2>,
13+
<liabilities/l3>.

0 commit comments

Comments
 (0)