expresso icon indicating copy to clipboard operation
expresso copied to clipboard

data element not passed in request body with assert.response

Open eventi opened this issue 13 years ago • 5 comments

I'm trying to use assert.response to test a PUT method, but the data part doesn't get passed.

The express part works with

curl -X PUT -d '[email protected]' http://localhost:3000/user'

Here's the code to illustrate the problem - using node v0.4.0 and npm tells me these are the other versions: [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]

express = require('express');
assert = require('assert');

app  = express.createServer();
app.configure(function() {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyDecoder());
    app.use(express.methodOverride());
    app.use(express.cookieDecoder());
    app.use(express.session());
    app.use(app.router);
    return app.use(express.staticProvider(__dirname + '/public'));
});
app.put('/user', function(req, res) {
    var email;
    email = req.param('email');
    if (email) {
      return res.send({
        success: 'Email Accepted...'
      });
    } else {
      return res.send({
        error: 'email required'
      });
    }
});

module.exports = {
    //curl -X PUT -d'[email protected]' http://localhost:3000/user", 'success'
    'PUT /user with email': function(){
        assert.response(app, {
            method: 'PUT',
            url: '/user',
            timeout: 5000,
            data: '[email protected]'
        },{
            status: 200,
            headers: { 'Content-Type': 'application/json' },
        },
        function(res){
            assert.includes(res.body, 'success');
        });
    },
}

lib versions:

[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]

eventi avatar Feb 12 '11 23:02 eventi

I think the problem may be that Content-Type and Content-Length headers are not being sent from issue() in the expresso executable. I was able to monkey patch it to work for POST requests by setting those values.

Something like:

    if (req.method.match(/post/i)) {
      req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
      req.headers['Content-Length'] = req.data.length;
    }

If it weren't so late I'd fork this right now.

adrianbravo avatar Aug 15 '11 10:08 adrianbravo

I am having the same issue, no data is transmitted to server

flockonus avatar Aug 16 '11 18:08 flockonus

I path'ed from what @adrianbravo has suggested, but I did not submit the path, since I got to admit, I don't really understand the headers I've set! xD

From line 431:

    if (req.method.toLowerCase() != 'get') {
      if(!req.headers) req.headers = {}
      req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
      req.headers['Content-Length'] = (data && data.length) || 0;
    }

flockonus avatar Aug 16 '11 19:08 flockonus

Also ran into this problem.

req.headers in issue() was undefined but needs to include req.headers['Content-Type'] = 'application/x-www-form-urlencoded'; on form submitted (non-get) requests.

Correct me if I'm wrong but Content-Length is not necessary due to using chunked encoding by default.

Would be helpful to update the examples on the homepage to make this clear.

assert.response(server, {
    url: '/foo',
    method: 'POST',
    headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
    data: 'test=hello%20world'
},{
    status: 200
},

seanabrahams avatar Sep 18 '11 18:09 seanabrahams

Hey, my solution to this was switching to Vows and Request, even wrote a post about it, hope it helps: http://fabianosoriani.wordpress.com/2011/08/31/testing-a-node-js-express-api-server-with-vows-functional/

flockonus avatar Sep 19 '11 01:09 flockonus