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

Need equivalent to assert.response() #86

Closed
Swatinem opened this issue Nov 26, 2011 · 12 comments
Closed

Need equivalent to assert.response() #86

Swatinem opened this issue Nov 26, 2011 · 12 comments

Comments

@Swatinem
Copy link

Expresso had a really easy way to test servers and their responses: assert.response(server, ...)

I saw you use a small request class (support/http) in connect and express, but copying it to my code didn't work as expected. An official tool to ease this task would be really awesome.

@rauchg
Copy link
Contributor

rauchg commented Nov 26, 2011

The problem is that there's not one perfect way of testing requests/responses. For example, in Socket.IO there's many specific requirements as far as testing long polling, for which assert.response just didn't work well.

I think this would be a good standalone project: require('http-assert') Think should.js for http.

@tj
Copy link
Contributor

tj commented Nov 26, 2011

yeah ideally what we should do is use superagent, or a project on top of superagent that allows request(app).get('/....) that will fire up the app on an ephemeral port. The goal of superagent was to provide every layer of abstraction necessary to perform both high-level and low-level requests, so it would be perfect for testing applications with broken json, broken multipart, valid multipart etc. Until I can finish up superagent I've been using this crappy little util for express/connect https://github.com/visionmedia/express/blob/master/test/support/http.js

which looks something like:

request(app).get('/foo').expect('bar', done)

a benefit of mocha's auto-exit feature as well is that we can safely nest these without worrying about manually closing severs unlike assert.response / expresso

@rauchg
Copy link
Contributor

rauchg commented Nov 26, 2011

Yeah a module on top of superagent would be ideal I think. Unless it's really minimal in which case it can go straight into superagent

@tj
Copy link
Contributor

tj commented Nov 26, 2011

should be small enough to add straight in, the only default that i can think off right now that would need defaulting to something different for testing would be the redirects since you'll probably want to assert locations etc i think the rest should be fine for testing

@tj
Copy link
Contributor

tj commented Nov 26, 2011

if app.get() for express wasn't for settings we could have all the verbs return a request thing:

app
  .get('/')
  .set('Content-Type', 'application/json')
  .data({ foo: 'bar' })
  .end(callback)

I guess it could be hacked with the leading / haha

@rauchg
Copy link
Contributor

rauchg commented Nov 26, 2011

We can always have superagent take a express Route.

@behrendtio
Copy link

Quick usage example for self implemented http support version:

// /test/support/http.js
var app = require('../../app');
var http = require('http');

module.exports.request = function(path, cb) {
    var port = app.address().port;
    http.request({ path: path, port: port }).on('response', function(res) {
        res.body = '';
        res.on('data', function(chunk) {
            res.body += chunk;
        });

        res.on('end', function() {
            cb(res);
        });
    }).end();
}

// In your tests
var http = require('support/http');
require('should');

describe(...) {
    it(...) {
        http.request('/', function(res) {
            res.statusCode.should.equal(200);
        });
    }
}

@tj
Copy link
Contributor

tj commented Dec 23, 2011

closing for now since it's semi-unrelated (mocha wont bundle anything like this)

@tj tj closed this as completed Dec 23, 2011
@jockster
Copy link

Has there been any change with this - ie. any new works created making this possible?

Can't get neither @MarioBehrendt 's solution or the http.js @visionmedia linked to, working in my application.

@behrendtio
Copy link

My version is pretty much a simple version of TJ's file. What's the problem? Can you paste your code and the errors, please?

@jockster
Copy link

Sorry. Had a syntax error that I overlooked. Is there a way that I can test POST, PUT & DELETE and send data along the request by modifying your above example?

Are you guys sure that this can't be included or at least mentioned in in the documentation? I absolutely pulled my hair before I found this post.

Performing response-testing probably is what many people are looking for and it's a shame that they choose a different testing component when mocha seems so mature.

@tj
Copy link
Contributor

tj commented Feb 24, 2012

@theindustry there's no reason to package it in mocha, it's unrelated to the client-side and it's not the be-all end-all solution for testing HTTP. Eventually I'll be recommending visionmedia/superagent for testing HTTP related stuff but it's not quite there yet

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

5 participants