Skip to content

Commit

Permalink
Merge pull request expressjs#11 from mafintosh/master
Browse files Browse the repository at this point in the history
Support for connect usage without express
  • Loading branch information
troygoode committed Oct 14, 2013
2 parents 96696d3 + f963183 commit f934e78
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 59 deletions.
11 changes: 6 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
function configureOrigin(options, req) {
var origin = options.origin;
if (origin === true) {
origin = req.header('Origin');
origin = req.headers.origin;
} else if (!origin) {
origin = '*';
}
Expand Down Expand Up @@ -47,7 +47,7 @@
function configureHeaders(options, req) {
var headers = options.headers;
if (!headers) {
headers = req.header('Access-Control-Request-Headers'); // .headers wasn't specified, so reflect the request headers
headers = req.headers['access-control-request-headers']; // .headers wasn't specified, so reflect the request headers
} else if (headers.join) {
headers = headers.join(','); // .headers is an array, so turn it into a string
}
Expand Down Expand Up @@ -76,7 +76,7 @@
applyHeaders = function (headers, res) {
headers.forEach(function (header) {
if (header && header.value) {
res.header(header.key, header.value);
res.setHeader(header.key, header.value);
}
});
};
Expand All @@ -88,7 +88,8 @@
headers.push(configureHeaders(options, req));
headers.push(configureMaxAge(options, req));
applyHeaders(headers, res);
res.send(204);
res.statusCode = 204;
res.end();
} else {
headers.push(configureOrigin(options, req));
headers.push(configureCredentials(options, req));
Expand Down Expand Up @@ -141,7 +142,7 @@

if (originCallback) {
req.pause(); // make sure downstream middleware don't miss any data events
originCallback(req.header('Origin'), function (err, origin) {
originCallback(req.headers.origin, function (err, origin) {
req.resume();
if (err || !origin) {
next(err);
Expand Down
106 changes: 52 additions & 54 deletions test/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
'access-control-request-headers': 'requestedHeader1,requestedHeader2'
};
return {
header: function (key) {
return headers[key.toLowerCase()];
},
headers:headers,
pause: function () {
// do nothing
return;
Expand All @@ -29,10 +27,10 @@
fakeResponse = function () {
var headers = {};
return {
header: function (key, value) {
if (value === undefined) {
return headers[key];
}
getHeader: function(key) {
return headers[key];
},
setHeader: function (key, value) {
headers[key] = value;
return;
}
Expand All @@ -59,9 +57,9 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function (code) {
res.end = function () {
// assert
code.should.equal(204);
res.statusCode.should.equal(204);
done();
};
next = function () {
Expand All @@ -80,8 +78,8 @@
res = fakeResponse();
next = function () {
// assert
res.header('Access-Control-Allow-Origin').should.equal('*');
should.not.exist(res.header('Access-Control-Allow-Methods'));
res.getHeader('Access-Control-Allow-Origin').should.equal('*');
should.not.exist(res.getHeader('Access-Control-Allow-Methods'));
done();
};

Expand All @@ -95,15 +93,15 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function (code) {
res.end = function () {
// assert
code.should.equal(204);
res.statusCode.should.equal(204);
done();
};
next = function () {
// assert
res.header('Access-Control-Allow-Origin').should.equal('*');
res.header('Access-Control-Allow-Methods').should.equal('GET,PUT,POST,DELETE');
res.getHeader('Access-Control-Allow-Origin').should.equal('*');
res.getHeader('Access-Control-Allow-Methods').should.equal('GET,PUT,POST,DELETE');
done();
};

Expand All @@ -125,18 +123,18 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function (code) {
res.end = function () {
// assert
code.should.equal(204);
res.statusCode.should.equal(204);
done();
};
next = function () {
// assert
res.header('Access-Control-Allow-Origin').should.equal('example.com');
res.header('Access-Control-Allow-Methods').should.equal('FOO,bar');
res.header('Access-Control-Allow-Headers').should.equal('FIZZ,buzz');
res.header('Access-Control-Allow-Credentials').should.equal('true');
res.header('Access-Control-Allow-Max-Age').should.equal('123');
res.getHeader('Access-Control-Allow-Origin').should.equal('example.com');
res.getHeader('Access-Control-Allow-Methods').should.equal('FOO,bar');
res.getHeader('Access-Control-Allow-Headers').should.equal('FIZZ,buzz');
res.getHeader('Access-Control-Allow-Credentials').should.equal('true');
res.getHeader('Access-Control-Allow-Max-Age').should.equal('123');
done();
};

Expand All @@ -158,11 +156,11 @@
res = fakeResponse();
next = function () {
// assert
should.not.exist(res.header('Access-Control-Allow-Origin'));
should.not.exist(res.header('Access-Control-Allow-Methods'));
should.not.exist(res.header('Access-Control-Allow-Headers'));
should.not.exist(res.header('Access-Control-Allow-Credentials'));
should.not.exist(res.header('Access-Control-Allow-Max-Age'));
should.not.exist(res.getHeader('Access-Control-Allow-Origin'));
should.not.exist(res.getHeader('Access-Control-Allow-Methods'));
should.not.exist(res.getHeader('Access-Control-Allow-Headers'));
should.not.exist(res.getHeader('Access-Control-Allow-Credentials'));
should.not.exist(res.getHeader('Access-Control-Allow-Max-Age'));
done();
};

Expand All @@ -180,7 +178,7 @@
res = fakeResponse();
next = function () {
// assert
res.header('Access-Control-Allow-Origin').should.equal('example.com');
res.getHeader('Access-Control-Allow-Origin').should.equal('example.com');
done();
};

Expand All @@ -197,7 +195,7 @@
res = fakeResponse();
next = function () {
// assert
res.header('Access-Control-Allow-Origin').should.equal('*');
res.getHeader('Access-Control-Allow-Origin').should.equal('*');
done();
};

Expand All @@ -215,7 +213,7 @@
res = fakeResponse();
next = function () {
// assert
res.header('Access-Control-Allow-Origin').should.equal('request.com');
res.getHeader('Access-Control-Allow-Origin').should.equal('request.com');
done();
};

Expand All @@ -234,7 +232,7 @@
req = fakeRequest();
res = fakeResponse();
next = function () {
res.header('Access-Control-Allow-Origin').should.equal('request.com');
res.getHeader('Access-Control-Allow-Origin').should.equal('request.com');
done();
};

Expand All @@ -252,11 +250,11 @@
req = fakeRequest();
res = fakeResponse();
next = function () {
should.not.exist(res.header('Access-Control-Allow-Origin'));
should.not.exist(res.header('Access-Control-Allow-Methods'));
should.not.exist(res.header('Access-Control-Allow-Headers'));
should.not.exist(res.header('Access-Control-Allow-Credentials'));
should.not.exist(res.header('Access-Control-Allow-Max-Age'));
should.not.exist(res.getHeader('Access-Control-Allow-Origin'));
should.not.exist(res.getHeader('Access-Control-Allow-Methods'));
should.not.exist(res.getHeader('Access-Control-Allow-Headers'));
should.not.exist(res.getHeader('Access-Control-Allow-Credentials'));
should.not.exist(res.getHeader('Access-Control-Allow-Max-Age'));
done();
};

Expand All @@ -272,14 +270,14 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function (code) {
res.end = function () {
// assert
code.should.equal(204);
res.statusCode.should.equal(204);
done();
};
next = function () {
// assert
res.header('Access-Control-Allow-Methods').should.equal('method1,method2');
res.getHeader('Access-Control-Allow-Methods').should.equal('method1,method2');
done();
};

Expand All @@ -295,14 +293,14 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function (code) {
res.end = function () {
// assert
code.should.equal(204);
res.statusCode.should.equal(204);
done();
};
next = function () {
// assert
res.header('Access-Control-Allow-Methods').should.equal('GET,PUT,POST,DELETE');
res.getHeader('Access-Control-Allow-Methods').should.equal('GET,PUT,POST,DELETE');
done();
};

Expand All @@ -319,9 +317,9 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function () {
res.end = function () {
// assert
res.header('Access-Control-Allow-Headers').should.equal('header1,header2');
res.getHeader('Access-Control-Allow-Headers').should.equal('header1,header2');
done();
};

Expand All @@ -339,7 +337,7 @@
res = fakeResponse();
next = function () {
// assert
should.not.exist(res.header('Access-Control-Allow-Headers'));
should.not.exist(res.getHeader('Access-Control-Allow-Headers'));
done();
};

Expand All @@ -355,9 +353,9 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function () {
res.end = function () {
// assert
res.header('Access-Control-Allow-Headers').should.equal('requestedHeader1,requestedHeader2');
res.getHeader('Access-Control-Allow-Headers').should.equal('requestedHeader1,requestedHeader2');
done();
};

Expand All @@ -374,9 +372,9 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function () {
res.end = function () {
// assert
res.header('Access-Control-Allow-Credentials').should.equal('true');
res.getHeader('Access-Control-Allow-Credentials').should.equal('true');
done();
};

Expand All @@ -393,7 +391,7 @@
res = fakeResponse();
next = function () {
// assert
should.not.exist(res.header('Access-Control-Allow-Credentials'));
should.not.exist(res.getHeader('Access-Control-Allow-Credentials'));
done();
};

Expand All @@ -410,9 +408,9 @@
req = fakeRequest();
req.method = 'OPTIONS';
res = fakeResponse();
res.send = function () {
res.end = function () {
// assert
res.header('Access-Control-Allow-Max-Age').should.equal('456');
res.getHeader('Access-Control-Allow-Max-Age').should.equal('456');
done();
};

Expand All @@ -429,7 +427,7 @@
res = fakeResponse();
next = function () {
// assert
should.not.exist(res.header('Access-Control-Allow-Max-Age'));
should.not.exist(res.getHeader('Access-Control-Allow-Max-Age'));
done();
};

Expand All @@ -453,7 +451,7 @@
res = fakeResponse();
next = function () {
// assert
res.header('Access-Control-Allow-Origin').should.equal('delegate.com');
res.getHeader('Access-Control-Allow-Origin').should.equal('delegate.com');
done();
};

Expand Down

0 comments on commit f934e78

Please sign in to comment.