Skip to content

Commit 1202fa2

Browse files
author
nicola
committed
adding tests for CORS
1 parent c05415b commit 1202fa2

2 files changed

Lines changed: 107 additions & 37 deletions

File tree

‎index.js‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ function ldnode (argv) {
4242
// Setting options as local variable
4343
app.locals.ldp = opts;
4444

45-
// Setting CORS
46-
app.use(cors({
47-
methods: [
48-
'OPTIONS', 'HEAD', 'GET',
49-
'PATCH', 'POST', 'PUT', 'DELETE'
50-
],
51-
exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
52-
credentials: true,
53-
maxAge: 1728000,
54-
origin: true
55-
}));
56-
5745
// Session
5846
app.use(session({
5947
secret: opts.secret || uuid.v1(),
@@ -176,11 +164,25 @@ function routes () {
176164

177165
// Convert json-ld and nquads to turtle
178166
router.use('/*', parse.parseHandler);
167+
179168
// Add links headers
180169
router.use(metadata.linksHandler);
170+
181171
// Add response time
182172
router.use(responseTime());
183173

174+
// Setting CORS
175+
router.use(cors({
176+
methods: [
177+
'OPTIONS', 'HEAD', 'GET',
178+
'PATCH', 'POST', 'PUT', 'DELETE'
179+
],
180+
exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
181+
credentials: true,
182+
maxAge: 1728000,
183+
origin: true
184+
}));
185+
184186
// HTTP methods handlers
185187
router.get('/*', getHandler.handler);
186188
router.head('/*', getHandler.headHandler);

‎test/http.js‎

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ var ldnode = require('../index');
55
var fs = require('fs');
66
var S = require('string');
77

8+
var emptyResponse = function(res) {
9+
return 0 !== res.text.length;
10+
};
11+
812
describe('HTTP APIs', function() {
913
var address = 'http://localhost:3457';
1014
var ldp = ldnode.createServer({
@@ -14,30 +18,15 @@ describe('HTTP APIs', function() {
1418

1519
var server = supertest(address);
1620

17-
describe('CORS are enabled', function() {
18-
it('should have Access-Control-Allow-Origin in header on root container', function(done) {
19-
server.get('/')
20-
.set('Origin', 'http://example.com')
21-
.expect('Access-Control-Allow-Origin', 'http://example.com')
22-
.expect(200, done);
23-
});
24-
25-
it('should have Access-Control-Allow-Origin in header on other containers', function(done) {
26-
server.get('/sampleContainer')
27-
.set('Origin', 'http://example.com')
28-
.expect('Access-Control-Allow-Origin', 'http://example.com')
29-
.expect(200, done);
30-
});
31-
32-
it('should have Access-Control-Allow-Origin in header on resources', function(done) {
33-
server.get('/sampleContainer/example1.ttl')
34-
.set('Origin', 'http://example.com')
35-
.expect('Access-Control-Allow-Origin', 'http://example.com')
36-
.expect(200, done);
37-
});
38-
});
3921

4022
describe('GET Root container', function() {
23+
it('should have Access-Control-Allow-Origin as the req.Origin', function(done) {
24+
server.get('/')
25+
.set('Origin', 'http://example.com')
26+
.expect('Access-Control-Allow-Origin', 'http://example.com')
27+
.expect(200, done);
28+
});
29+
4130
it('Should exists', function(done) {
4231
server.get('/')
4332
.expect(200, done);
@@ -49,7 +38,69 @@ describe('HTTP APIs', function() {
4938
});
5039
});
5140

41+
describe('OPTIONS API', function (){
42+
43+
it('should have an empty response', function(done) {
44+
server.options('/sampleContainer/example1.ttl')
45+
.expect(emptyResponse)
46+
.end(done);
47+
});
48+
49+
it('should return 204 on success', function(done) {
50+
server.options('/sampleContainer/example1.ttl')
51+
.expect(204)
52+
.end(done);
53+
});
54+
55+
it('should have Access-Control-Allow-Origin on OPTIONS', function(done) {
56+
server.options('/sampleContainer/example1.ttl')
57+
.set('Origin', 'http://example.com')
58+
.expect('Access-Control-Allow-Origin', 'http://example.com')
59+
.end(done);
60+
});
61+
62+
it('should have set Link as resource in OPTIONS', function(done) {
63+
server.options('/sampleContainer/example1.ttl')
64+
.expect('Link', "<http://www.w3.org/ns/ldp#Resource>; rel='type'")
65+
.end(done);
66+
});
67+
68+
it('should have set Link as Container/BasicContainer in OPTIONS', function(done) {
69+
server.options('/sampleContainer/')
70+
.set('Origin', 'http://example.com')
71+
.expect('Link', "<http://www.w3.org/ns/ldp#Container>; rel='type'," +
72+
" <http://www.w3.org/ns/ldp#BasicContainer>; rel='type'")
73+
.end(done);
74+
});
75+
});
76+
5277
describe('GET API', function() {
78+
it('should have Access-Control-Allow-Origin as Origin on containers', function(done) {
79+
server.get('/sampleContainer')
80+
.set('Origin', 'http://example.com')
81+
.expect('Access-Control-Allow-Origin', 'http://example.com')
82+
.expect(200, done);
83+
});
84+
85+
it('should have Access-Control-Allow-Origin as Origin on resources', function(done) {
86+
server.get('/sampleContainer/example1.ttl')
87+
.set('Origin', 'http://example.com')
88+
.expect('Access-Control-Allow-Origin', 'http://example.com')
89+
.expect(200, done);
90+
});
91+
92+
it('should have set Link as resource', function(done) {
93+
server.get('/sampleContainer/example1.ttl')
94+
.expect('Link', "<http://www.w3.org/ns/ldp#Resource>; rel='type'")
95+
.expect(200, done);
96+
});
97+
98+
it('should have set Link as Container/BasicContainer', function(done) {
99+
server.head('/sampleContainer/')
100+
.expect('Link', "<http://www.w3.org/ns/ldp#Container>; rel='type'," +
101+
" <http://www.w3.org/ns/ldp#BasicContainer>; rel='type'")
102+
.expect(200, done);
103+
});
53104
it('Should return 404 for non-existent resource', function(done) {
54105
server.get('/invalidfile.foo')
55106
.expect(404, done);
@@ -72,14 +123,31 @@ describe('HTTP APIs', function() {
72123
});
73124

74125
describe('HEAD API', function() {
75-
var emptyResponse = function(res) {
76-
return 0 !== res.text.length;
77-
};
126+
it('should have Access-Control-Allow-Origin == Origin', function(done) {
127+
server.head('/sampleContainer/example1.ttl')
128+
.set('Origin', 'http://example.com')
129+
.expect('Access-Control-Allow-Origin', 'http://example.com')
130+
.expect(200, done);
131+
});
132+
78133
it('Should return empty response body', function(done) {
79134
server.head('/patch-5-initial.ttl')
80135
.expect(emptyResponse)
81136
.expect(200, done);
82137
});
138+
139+
it('should have set Link as Resource', function(done) {
140+
server.head('/sampleContainer/example1.ttl')
141+
.expect('Link', "<http://www.w3.org/ns/ldp#Resource>; rel='type'")
142+
.expect(200, done);
143+
});
144+
145+
it('should have set Link as Container/BasicContainer', function(done) {
146+
server.get('/sampleContainer/')
147+
.expect('Link', "<http://www.w3.org/ns/ldp#Container>; rel='type'," +
148+
" <http://www.w3.org/ns/ldp#BasicContainer>; rel='type'")
149+
.expect(200, done);
150+
});
83151
});
84152

85153
describe('PUT API', function() {

0 commit comments

Comments
 (0)