-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathldp.js
More file actions
220 lines (193 loc) · 7.33 KB
/
ldp.js
File metadata and controls
220 lines (193 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var assert = require('chai').assert
var $rdf = require('rdflib')
var ns = require('../lib/vocab/ns.js').ns
var LDP = require('../lib/ldp')
var path = require('path')
var stringToStream = require('../lib/utils').stringToStream
// Helper functions for the FS
var rm = require('./test-utils').rm
var write = require('./test-utils').write
// var cp = require('./test-utils').cp
var read = require('./test-utils').read
var fs = require('fs')
describe('LDP', function () {
var ldp = new LDP({
root: __dirname
})
describe('readFile', function () {
it('return 404 if file does not exist', function (done) {
ldp.readFile('resources/unexistent.ttl', function (err) {
assert.equal(err.status, 404)
done()
})
})
it('return file if file exists', function (done) {
// file can be empty as well
write('hello world', 'fileExists.txt')
ldp.readFile(path.join(__dirname, '/resources/fileExists.txt'), function (err, file) {
rm('fileExists.txt')
assert.notOk(err)
assert.equal(file, 'hello world')
done()
})
})
})
describe('readContainerMeta', function () {
it('should return 404 if .meta is not found', function (done) {
ldp.readContainerMeta('resources/', function (err) {
assert.equal(err.status, 404)
done()
})
})
it('should return content if metaFile exists', function (done) {
// file can be empty as well
write('This function just reads this, does not parse it', '.meta')
ldp.readContainerMeta(path.join(__dirname, '/resources/'), function (err, metaFile) {
rm('.meta')
assert.notOk(err)
assert.equal(metaFile, 'This function just reads this, does not parse it')
done()
})
})
it('should work also if trailing `/` is not passed', function (done) {
// file can be empty as well
write('This function just reads this, does not parse it', '.meta')
ldp.readContainerMeta(path.join(__dirname, '/resources'), function (err, metaFile) {
rm('.meta')
assert.notOk(err)
assert.equal(metaFile, 'This function just reads this, does not parse it')
done()
})
})
})
describe('put', function () {
it('should write a file in an existing dir', function (done) {
var stream = stringToStream('hello world')
ldp.put('localhost', '/resources/testPut.txt', stream, function (err) {
assert.notOk(err)
var found = read('testPut.txt')
rm('testPut.txt')
assert.equal(found, 'hello world')
done()
})
})
it('should fail if a trailing `/` is passed', function (done) {
var stream = stringToStream('hello world')
ldp.put('localhost', '/resources/', stream, function (err) {
assert.equal(err.status, 409)
done()
})
})
})
describe('delete', function () {
it('should delete a file in an existing dir', function (done) {
var stream = stringToStream('hello world')
ldp.put('localhost', '/resources/testPut.txt', stream, function (err) {
assert.notOk(err)
fs.stat(ldp.root + '/resources/testPut.txt', function (err) {
if (err) {
return done(err)
}
ldp.delete('localhost', '/resources/testPut.txt', function (err) {
if (err) done(err)
fs.stat(ldp.root + '/resources/testPut.txt', function (err) {
return done(err ? null : new Error('file still exists'))
})
})
})
})
})
})
describe('listContainer', function () {
it('should inherit type if file is .ttl', function (done) {
write('@prefix dcterms: <http://purl.org/dc/terms/>.' +
'@prefix o: <http://example.org/ontology>.' +
'<> a <http://www.w3.org/ns/ldp#MagicType> ;' +
' dcterms:title "This is a magic type" ;' +
' o:limit 500000.00 .', 'sampleContainer/magicType.ttl')
ldp.listContainer(path.join(__dirname, '/resources/sampleContainer/'), 'https://server.tld', '', 'text/turtle', function (err, data) {
if (err) done(err)
var graph = $rdf.graph()
$rdf.parse(
data,
graph,
'https://server.tld/sampleContainer',
'text/turtle')
var statements = graph
.each(
$rdf.sym('https://server.tld/magicType.ttl'),
ns.rdf('type'),
undefined)
.map(function (d) {
return d.uri
})
assert.equal(statements.length, 2)
assert.isAbove(statements.indexOf('http://www.w3.org/ns/ldp#MagicType'), -1)
assert.isAbove(statements.indexOf('http://www.w3.org/ns/ldp#Resource'), -1)
rm('sampleContainer/magicType.ttl')
done()
})
})
it('should not inherit type of BasicContainer/Container if type is File', function (done) {
write('@prefix dcterms: <http://purl.org/dc/terms/>.' +
'@prefix o: <http://example.org/ontology>.' +
'<> a <http://www.w3.org/ns/ldp#Container> ;' +
' dcterms:title "This is a container" ;' +
' o:limit 500000.00 .', 'sampleContainer/containerFile.ttl')
write('@prefix dcterms: <http://purl.org/dc/terms/>.' +
'@prefix o: <http://example.org/ontology>.' +
'<> a <http://www.w3.org/ns/ldp#BasicContainer> ;' +
' dcterms:title "This is a container" ;' +
' o:limit 500000.00 .', 'sampleContainer/basicContainerFile.ttl')
ldp.listContainer(path.join(__dirname, '/resources/sampleContainer/'), 'https://server.tld', '', 'text/turtle', function (err, data) {
if (err) done(err)
var graph = $rdf.graph()
$rdf.parse(
data,
graph,
'https://server.tld/sampleContainer',
'text/turtle')
var basicContainerStatements = graph.each(
$rdf.sym('https://server.tld/basicContainerFile.ttl'),
ns.rdf('type'),
undefined)
assert.equal(basicContainerStatements.length, 1)
var containerStatements = graph.each(
$rdf.sym('https://server.tld/containerFile.ttl'),
ns.rdf('type'),
undefined)
assert.equal(containerStatements.length, 1)
basicContainerStatements.forEach(function (statement) {
assert.equal(statement.uri, ns.ldp('Resource').uri)
})
containerStatements.forEach(function (statement) {
assert.equal(statement.uri, ns.ldp('Resource').uri)
})
rm('sampleContainer/containerFile.ttl')
rm('sampleContainer/basicContainerFile.ttl')
done()
})
})
it('should ldp:contains the same amount of files in dir', function (done) {
ldp.listContainer(path.join(__dirname, '/resources/sampleContainer/'), 'https://server.tld', '', 'text/turtle', function (err, data) {
if (err) done(err)
fs.readdir(path.join(__dirname, '/resources/sampleContainer/'), function (err, files) {
var graph = $rdf.graph()
$rdf.parse(
data,
graph,
'https://server.tld/sampleContainer',
'text/turtle')
var statements = graph.each(
undefined,
ns.ldp('contains'),
undefined)
assert.notEqual(graph.statements.length, 0)
assert.equal(statements.length, files.length)
assert.notOk(err)
done()
})
})
})
})
})