vowsjs/api-easy

Failed to get the right status code?

Closed this issue · 2 comments

I did a http server like this:

var http = require('http');

http.createServer(function( req, res ) {
    res.writeHead(302, {
        'Content-Type' : 'text/html',
        'Location' : 'http://www.google.com'
    });
    res.write('Yay');
    res.end();
}).listen(9000);

and a test case like this:

var ApiEasy = require('api-easy');

var suite = ApiEasy.describe('Check if server is alive');

suite.discuss('GIVEN that I want to check the server\'s basic REST API')
    .use('localhost', 9000)
    .path('/')
        .get()
        .expect(302);

suite.export(module);

But it shows:

should respond with 302
» expected 302,
got 200 (==)

@soggie Sorry for the delay in getting back to you, but this is actually by design, but there is an easy solution. The underlying request module that api-easy uses will by default follow 302 redirects.

So when you redirect to google.com it actually goes there and gets the 200 from google. You just have to tell your suite not to follow redirects.

suite.discuss('GIVEN that I want to check the server\'s basic REST API')
  .use('localhost', 9000)
  .followRedirect(false)
  .path('/')
    .get()
    .expect(302);

Thanks. I kind of figured this out few hours after I logged this issue by combing the source code, but forgot to update this issue. Thanks!