-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathhttp.js
More file actions
325 lines (296 loc) · 11.9 KB
/
http.js
File metadata and controls
325 lines (296 loc) · 11.9 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*jslint node: true*/
var supertest = require('supertest');
var path = require('path');
var fs = require('fs');
var S = require('string');
var li = require('li');
var ldnode = require('../index');
describe('HTTP APIs', function() {
var emptyResponse = function(res) {
if (res.text.length !== 0) {
console.log("Not empty response");
}
};
var getLink = function(res, rel) {
var links = res.headers.link.split(',');
for (var i in links) {
var link = links[i];
var parsedLink = li.parse(link);
if (parsedLink[rel]) {
return parsedLink[rel];
}
}
return undefined;
};
var hasHeader = function(rel, value) {
var handler = function(res) {
var link = getLink(res, rel);
if (link) {
if (link !== value) {
console.log("Not same value:", value, '!=', link);
}
} else {
console.log(rel, "header does not exist:", link);
}
};
return handler;
};
var ldpServer = ldnode({
root: __dirname + '/resources'
});
var suffixAcl = ".acl";
var suffixMeta = ".meta";
var server = supertest(ldpServer);
describe('GET Root container', function() {
it('should have Access-Control-Allow-Origin as the req.Origin', function(done) {
server.get('/')
.set('Origin', 'http://example.com')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(200, done);
});
it('Should exists', function(done) {
server.get('/')
.expect(200, done);
});
it('Should be a turtle file by default', function(done) {
server.get('/')
.expect('content-type', /text\/turtle/)
.expect(200, done);
});
});
describe('OPTIONS API', function (){
it('should have an empty response', function(done) {
server.options('/sampleContainer/example1.ttl')
.expect(emptyResponse)
.end(done);
});
it('should return 204 on success', function(done) {
server.options('/sampleContainer/example1.ttl')
.expect(204)
.end(done);
});
it('should have Access-Control-Allow-Origin', function(done) {
server.options('/sampleContainer/example1.ttl')
.set('Origin', 'http://example.com')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.end(done);
});
it('should have set acl and describedBy Links for resource', function(done) {
server.options('/sampleContainer/example1.ttl')
.expect(hasHeader('acl', 'example1.ttl' + suffixAcl))
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
.end(done);
});
it('should have set Link as resource', function(done) {
server.options('/sampleContainer/example1.ttl')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
.end(done);
});
it('should have set Link as Container/BasicContainer', function(done) {
server.options('/sampleContainer/')
.set('Origin', 'http://example.com')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
.end(done);
});
it('should have set acl and describedBy Links for container', function(done) {
server.options('/sampleContainer/')
.expect(hasHeader('acl', suffixAcl))
.expect(hasHeader('describedBy', suffixMeta))
.end(done);
});
});
describe('GET API', function() {
it('should have Access-Control-Allow-Origin as Origin on containers', function(done) {
server.get('/sampleContainer')
.set('Origin', 'http://example.com')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(200, done);
});
it('should have Access-Control-Allow-Origin as Origin on resources', function(done) {
server.get('/sampleContainer/example1.ttl')
.set('Origin', 'http://example.com')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(200, done);
});
it('should have set Link as resource', function(done) {
server.get('/sampleContainer/example1.ttl')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
.expect(200, done);
});
it('should have set acl and describedBy Links for resource', function(done) {
server.get('/sampleContainer/example1.ttl')
.expect(hasHeader('acl', 'example1.ttl' + suffixAcl))
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
.end(done);
});
it('should have set Link as Container/BasicContainer', function(done) {
server.head('/sampleContainer/')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
.expect(200, done);
});
it('should return 404 for non-existent resource', function(done) {
server.get('/invalidfile.foo')
.expect(404, done);
});
it('should return basic container link for directories', function(done) {
server.get('/')
.expect('Link', /http:\/\/www.w3.org\/ns\/ldp#BasicContainer/)
.expect(200, done);
});
it('should return resource link for files', function(done) {
server.get('/hello.html')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
.expect(200, done);
});
it('should have glob support', function(done) {
server.get('/sampleContainer/example*')
.expect('content-type', /text\/turtle/)
.expect(200, done);
});
it('should have set acl and describedBy Links for container', function(done) {
server.get('/sampleContainer/')
.expect(hasHeader('acl', suffixAcl))
.expect(hasHeader('describedBy', suffixMeta))
.end(done);
});
});
describe('HEAD API', function() {
it('should have Access-Control-Allow-Origin as Origin', function(done) {
server.head('/sampleContainer/example1.ttl')
.set('Origin', 'http://example.com')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(200, done);
});
it('Should return empty response body', function(done) {
server.head('/patch-5-initial.ttl')
.expect(emptyResponse)
.expect(200, done);
});
it('should have set Link as Resource', function(done) {
server.head('/sampleContainer/example1.ttl')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
.expect(200, done);
});
it('should have set acl and describedBy Links for resource', function(done) {
server.get('/sampleContainer/example1.ttl')
.expect(hasHeader('acl', 'example1.ttl' + suffixAcl))
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
.end(done);
});
it('should have set Link as Container/BasicContainer', function(done) {
server.get('/sampleContainer/')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
.expect(200, done);
});
it('should have set acl and describedBy Links for container', function(done) {
server.get('/sampleContainer/')
.expect(hasHeader('acl', suffixAcl))
.expect(hasHeader('describedBy', suffixMeta))
.end(done);
});
});
describe('PUT API', function() {
var putRequestBody = fs.readFileSync(__dirname + '/resources/sampleContainer/put1.ttl', {
'encoding': 'utf8'
});
it('Should create new resource', function(done) {
server.put('/put-resource-1.ttl')
.send(putRequestBody)
.set('content-type', 'text/turtle')
.expect(201, done);
});
it('Should create directories if they do not exist', function (done) {
server.put('/foo/bar/baz.ttl')
.send(putRequestBody)
.set('content-type', 'text/turtle')
.expect(hasHeader('describedBy', 'baz.ttl' + suffixMeta))
.expect(hasHeader('acl', 'baz.ttl' + suffixAcl))
.expect(function() {
fs.unlinkSync(__dirname + '/resources/foo/bar/baz.ttl');
fs.rmdirSync(__dirname + '/resources/foo/bar/');
fs.rmdirSync(__dirname + '/resources/foo/');
})
.expect(201, done);
});
it('Should return 409 code when trying to put to a container', function(done) {
server.put('/')
.expect(409, done);
});
});
describe('DELETE API', function() {
it('Should return 404 status when deleting a file that does not exists',
function(done) {
server.delete('/false-file-48484848')
.expect(404, done);
});
it('Should delete previously PUT file', function(done) {
server.delete('/put-resource-1.ttl')
.expect(200, done);
});
});
describe('POST API', function() {
var postRequest1Body = fs.readFileSync(__dirname + '/resources/sampleContainer/put1.ttl', {
'encoding': 'utf8'
});
var postRequest2Body = fs.readFileSync(__dirname + '/resources/sampleContainer/post2.ttl', {
'encoding': 'utf8'
});
it('Should create new resource', function(done) {
server.post('/')
.send(postRequest1Body)
.set('content-type', 'text/turtle')
.set('slug', 'post-resource-1')
.expect('location', /\/post-resource-1.ttl/)
.expect(hasHeader('describedBy', suffixMeta))
.expect(hasHeader('acl', suffixAcl))
.expect(201, done);
});
it('Should reject requests to existing resources', function(done) {
server.post('/')
.send(postRequest1Body)
.set('content-type', 'text/turtle')
.set('slug', 'post-resource-1')
.expect(400, done);
});
it('Should be able to delete newly created resource', function(done) {
server.delete('/post-resource-1.ttl')
.expect(200, done);
});
var postResourceName;
var setResourceName = function(res) {
postResourceName = res.header.location;
};
it('Should create new resource without slug header', function(done) {
server.post('/')
.send(postRequest1Body)
.set('content-type', 'text/turtle')
.expect(201)
.expect(setResourceName)
.end(done);
});
it('Should be able to delete newly created resource', function(done) {
server.delete('/' + postResourceName.replace(/https?\:\/\/127.0.0.1:[0-9]*\//, ''))
.expect(200, done);
});
it('Should create container', function(done) {
server.post('/')
.set('content-type', 'text/turtle')
.set('slug', 'loans')
.set('link', '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"')
.send(postRequest2Body)
.expect(201, done);
});
it('Should be able to access container', function(done) {
server.get('/loans')
.expect('content-type', /text\/turtle/)
.expect(function() {
fs.unlinkSync(__dirname + '/resources/loans/.meta');
fs.rmdirSync(__dirname + '/resources/loans/');
})
.expect(200, done);
});
});
});