简体   繁体   中英

Testing post in bare Node.js app with Mocha and Superagent

I hope the day finds you well.

So I'm trying to build some TDD chops in Node and to that end I've built a super bare bones app which runs a simple GET and POST request. All it does is serve the worlds most simple form, then take what the user enters into this form and put it on the screen. This is virgin Node, no frameworks involved. I'm using Mocha and Superagent for testing and am getting stuck on the POST test. Here's my app:

    var http = require('http');
    var qs = require('querystring');

    var server = http.createServer(function(req, res){
        switch(req.method){
            case 'GET':
                console.log("Calling get");
                res.statusCode = 200;
                res.setHeader('Content-Type', 'text/html');
                res.end("<p>Hello World!</p>" + 
                    "<form method='post' action='/'>" +
                    "<input type='text' name='field'>" + 
                    "<input type='submit'>" +
                    "</form>");
                break;
            case 'POST':
                var body = "";
                req.on('data', function(data){
                    body += data;
                })
                req.on('end', function(){
                    var post = qs.parse(body);
                    res.statusCode = 200;
                    res.setHeader('Content-Type', 'text/html');
                    res.end("<p>" + post.field + "</p>")
                    // console.log(req)
                    console.log(post);
                });
        }
    })
    server.listen(8080);

    console.log('Server running on port 8080.')

And here are my tests:

    var request = require('superagent');
    var expect = require('expect.js');

    describe('Main page', function(){
        it("should get 'Hello World!'", function(done){
            request.get('localhost:8080').end(function(res){
                expect(res).to.exist;
                expect(res.status).to.equal(200);
                expect(res.text).to.contain("World");
                done();
            });
        });

        it("should display input text from a form.", function(done){
            request.post('localhost:8080')
                .send({field: "Test string."})
                .end(function(res){
                    expect(res).to.exist;
                    expect(res.status).to.equal(200);
                    expect(res.text).to.contain("Test");
                    done();
            })
        });
    });

I like to keep it as simple as possible when I'm learning things so that I can isolate what I'm doing. From what I know of Superagent the .send() method should take an object which contains the various post keys and values, this is then passed to the app and run along the given route. But when I run the test everything passes except the expect(res.text).to.contain("Test") assertion. I get an error that Mocha expected '

undefined

' to contain 'Test'. When I just start the app and run it in browser, everything is good.

I've been wrestling with this for a while and now I'm going to the hivemind. As I mentioned I'm a TDD newbie but I want to be a testing god and this is really harshing my mellow. Any enlightenment would be greatly appreciated.

And got it on my own. Amazing what documentation can teach when you stare at it long enough.

After looking at the docs located here:

http://visionmedia.github.io/superagent/#post-/%20put%20requests

I realized that for Superagent to post properly I had to tell it the type of post being made prior to the information being sent like so:

    it("should display input text from a form.", function(done){
        request.post('localhost:8080')
            .type('form')
            .send({field: "Test string."})
            .end(function(res){
                expect(res).to.exist;
                expect(res.status).to.equal(200);
                expect(res.text).to.contain("Test");
                done();
        })
    });

Nifty stuff. Hope others find this helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM