Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[reqheaders] first working try

  • Loading branch information...
commit 53f0e6c9abc00af90143ec40a7b2caf7b417d692 2 parents 5f77e92 + add91cb
@dscape authored
View
8 nano.js
@@ -124,6 +124,12 @@ module.exports = exports = nano = function database_module(cfg) {
req.uri = u.resolve(req.uri, opts.db);
}
+ if (opts.headers) {
+ for (var k in opts.headers) {
+ req.headers[k] = opts.headers[k];
+ }
+ }
+
if(opts.path) {
req.uri += "/" + opts.path;
}
@@ -269,7 +275,7 @@ module.exports = exports = nano = function database_module(cfg) {
try { parsed = JSON.parse(b); } catch (err) { parsed = b; }
- if (status_code >= 200 && status_code < 300) {
+ if (status_code >= 200 && status_code < 400) {
log({err: null, body: parsed, headers: rh});
callback(null,parsed,rh);
return stream;
View
1  package.json
@@ -20,6 +20,7 @@
, "Francesco Ceccon <francesco@ceccon.me> (http://francesco-cek.com/)"
, "Oliver Leics (http://oleics.github.com)"
, "Jonathan Mahoney (http://mahoney.eu)"
+ , "Dominique Sandoz <dominique.sandoz@gmail.com>"
]
, "keywords" :
["couchdb", "data", "request", "json", "nosql", "micro", "nano", "database"]
View
29 tests/fixtures/opts/headers.json
@@ -0,0 +1,29 @@
+[
+ { "method" : "put"
+ , "path" : "/opts_headers"
+ , "status" : 201
+ , "response" : "{ \"ok\": true }"
+ }
+, { "method" : "put"
+ , "path" : "/opts_headers/new/att"
+ , "body" : "\"Hello\""
+ , "status" : 201
+ , "response" : "{\"ok\":true,\"id\":\"new\",\"rev\":\"1-5142a2\"}"
+ }
+, { "path" : "/opts_headers/new/att"
+ , "status" : 304
+ , "response" : ""
+ , "reqheaders" : {
+ "If-None-Match": "\"1-5142a2e74e1ec33e6e5b621418210283\""
+ }
+ }
+, { "path" : "/opts_headers/new/att"
+ , "status" : 200
+ , "response" : "\"Hello\""
+ }
+, { "method" : "delete"
+ , "path" : "/opts_headers"
+ , "status" : 200
+ , "response" : "{ \"ok\": true }"
+ }
+]
View
33 tests/helpers.js
@@ -55,18 +55,19 @@ helpers.nock = function helpersNock(url, fixture) {
, nocks = helpers.loadFixture(fixture + '.json', true)
;
nocks.forEach(function(n) {
- var npath = n.path
- , method = n.method || "get"
- , status = n.status || 200
- , response = n.buffer
- ? endsWith(n.buffer, '.png')
- ? helpers.loadFixture(n.buffer)
- : new Buffer(n.buffer, 'base64')
- : n.response || ""
- , headers = n.headers || {}
- , body = n.base64
- ? new Buffer(n.base64, 'base64').toString()
- : n.body || ""
+ var npath = n.path
+ , method = n.method || "get"
+ , status = n.status || 200
+ , response = n.buffer
+ ? endsWith(n.buffer, '.png')
+ ? helpers.loadFixture(n.buffer)
+ : new Buffer(n.buffer, 'base64')
+ : n.response || ""
+ , headers = n.headers || {}
+ , reqheaders = n.reqheaders || {}
+ , body = n.base64
+ ? new Buffer(n.base64, 'base64').toString()
+ : n.body || ""
;
if(typeof response === "string" && endsWith(response, '.json')) {
@@ -75,11 +76,17 @@ helpers.nock = function helpersNock(url, fixture) {
if(typeof headers === "string" && endsWith(headers, '.json')) {
headers = helpers.loadFixture(path.join(fixture, headers));
}
+
if(body==="*") {
- nock(url).filteringRequestBody(function(path) {
+ nock(url).filteringRequestBody(function() {
return "*";
})[method](npath, "*").reply(status, response, headers);
} else {
+ if(reqheaders !== {}) {
+ for (var k in reqheaders) {
+ nock(url).matchHeader(k, reqheaders[k]);
+ }
+ }
nock(url)[method](npath, body).reply(status, response, headers);
}
});
View
57 tests/opts/headers.js
@@ -0,0 +1,57 @@
+
+var specify = require('specify')
+ , helpers = require('../helpers')
+ , timeout = helpers.timeout
+ , nano = helpers.nano
+ , nock = helpers.nock
+ ;
+
+var mock = nock(helpers.couch, "opts/headers")
+ , db = nano.use("opts_headers")
+ ;
+
+
+specify("opts_headers:setup", timeout, function (assert) {
+ nano.db.create("opts_headers", function (err) {
+ assert.equal(err, undefined, "Failed to create database");
+ });
+});
+
+specify("opts_headers:test", timeout, function (assert) {
+ db.attachment.insert("new", "att", "Hello", "text/plain",
+ function(error, hello) {
+ assert.equal(error, undefined, "Should store hello");
+ assert.equal(hello.ok, true, "Response should be ok");
+ assert.ok(hello.rev, "Should have a revision number");
+ nano.request({
+ db: "opts_headers",
+ doc: "new",
+ att: "att",
+ headers: {
+ "If-None-Match": "\"1-5142a2e74e1ec33e6e5b621418210283\""
+ }
+ },
+ function (error, helloWorld, rh) {
+ assert.equal(error, undefined, "Should get the hello");
+ assert.equal(rh["status-code"], 304, "status is 'not modified'");
+ });
+ nano.request({
+ db: "opts_headers",
+ doc: "new",
+ att: "att"
+ },
+ function (error, helloWorld, rh) {
+ assert.equal(error, undefined, "Should get the hello");
+ assert.equal(rh["status-code"], 200, "status is 'ok'");
+ });
+ });
+});
+
+specify("opts_headers:teardown", timeout, function (assert) {
+ nano.db.destroy("opts_headers", function (err) {
+ assert.equal(err, undefined, "Failed to destroy database");
+ assert.ok(mock.isDone(), "Some mocks didn't run");
+ });
+});
+
+specify.run(process.argv.slice(2));
Please sign in to comment.
Something went wrong with that request. Please try again.