Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data element not passed in request body with assert.response #71

Open
eventi opened this issue Feb 12, 2011 · 5 comments
Open

data element not passed in request body with assert.response #71

eventi opened this issue Feb 12, 2011 · 5 comments

Comments

@eventi
Copy link

eventi commented Feb 12, 2011

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]

@adrianbravo
Copy link

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.

@flockonus
Copy link

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

@flockonus
Copy link

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;
    }

@seanabrahams
Copy link

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
},

@flockonus
Copy link

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/

ilkovich pushed a commit to ilkovich/expresso that referenced this issue Dec 15, 2011
This resolves the issue where a data element is not passed in request body
with assert.response.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants