chris-l / mock-couch

A node.js module designed to mock a CouchDB server, mostly for unit testing purposes.
http://chris-l.github.io/mock-couch/
67 stars 44 forks source link

Getting 405 PUT is not allowed when trying to insert a _design doc with nano #41

Closed KingScooty closed 8 years ago

KingScooty commented 8 years ago

I seem to be getting a 405 PUT not allowed error when trying to insert a design document;

{ [Error: PUT is not allowed]
  name: 'Error',
  statusCode: 405,
  body: { code: 'MethodNotAllowedError', message: 'PUT is not allowed' },
  scope: 'couch',
  request: 
   { method: 'PUT',
     headers: 
      { 'content-type': 'application/json',
        accept: 'application/json' },
[...]

I'm simply doing the following with nano after creating a database:

var design = {
  "views": {
    "all": {
      map: function(doc) {
        if (doc._id) emit(doc._id, doc);
      }
    }
  }
};

db.insert(design, '_design/tweets', function(err, res) {
  if (err) return console.log(err);
  console.log('Inserted!');
});

This gives the correct response when using a real couchdb database, but seems to fail when using mock-couch.

chris-l commented 8 years ago

Hm yeah, that is for the way its designed at the moment.

In the documentation (here http://chris-l.github.io/mock-couch/#views ) I mention that the functions should be passed as functions, that is, not as strings. But this is by using mockcouch functions to add documents. If you add a design document using nano, nano will convert your function into a string before to actually sending it to mockcouch. (it make sense, there is no other way to pass a function over JSON than stringifying it)

That is, at this moment, the mockcouch is not prepared to receive design documents by any other means than using directly the mockcouch methods, ie addDoc or addDB.

I haven't touched the source code in a while, so I'm not sure if is possible to do it, but I'll check it out.

dermidgen commented 8 years ago

I've added support for this; creating a PR for ya.