Skip to content

Commit

Permalink
test: for request next
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-nico committed Jul 8, 2016
1 parent f3ab2bc commit 536bc91
Showing 1 changed file with 180 additions and 2 deletions.
182 changes: 180 additions & 2 deletions test/spec/request-next.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';

var client = require('@request/client'),
var api = require('@request/api'),
client = require('@request/client'),
Bluebird = require('bluebird'),
configure = require('../../configure/request-next.js');
configure = require('../../configure/request-next.js'),
errors = require('../../errors'),
startServer = require('../fixtures/server.js');


describe('Promise-Core for Request@next', function () {
Expand Down Expand Up @@ -84,4 +87,179 @@ describe('Promise-Core for Request@next', function () {

});

/**
* The following tests are testing the correct integration of plumbing.init and plumbing.callback.
*
* That means their test coverage is not 100% (the plumbing unit tests already do that)
* but instead focus on the following integration aspects:
* - All input parameters are passed to the two functions as expected.
* - All return values of the two functions are processed by Request as expected.
* - All operations on the context (this) of the two functions have the expected effect.
* - Plus 100% coverage of the configuration code.
*/
describe('doing requests', function () {

var request = null, stopServer = null;

before(function (done) {

request = api({
type: 'basic',
define: {
request: configure({
client: client,
PromiseImpl: Bluebird,
expose: [
'then',
'catch',
'finally',
'promise'
]
})
}
});

startServer(4000, function (stop) {
stopServer = stop;
done();
});

});

after(function (done) {

stopServer(done);

});

it('that is successful', function (done) {

request('http://localhost:4000/200')
.then(function (body) {
expect(body).to.eql('GET /200');
done();
})
.catch(function (err) {
done(err);
});

});

// TODO: Check if request@next fixed passing the response to the callback
xit('that is successful with non-default options', function (done) {

request({
uri: 'http://localhost:4000/404',
simple: false,
resolveWithFullResponse: true
})
.then(function (response) {
expect(response.body).to.eql('GET /404');
done();
})
.catch(function (err) {
done(err);
});

});

it('with method "post" in lower case', function (done) {

request({
method: 'post',
uri: 'http://localhost:4000/200',
body: {
a: 'b'
},
json: true
})
.then(function (body) {
// TODO: Check if request@next fixed sending the body
// expect(body).to.eql('POST /200 - {"a":"b"}');
expect(body).to.eql('POST /200 - {}');
done();
})
.catch(function (err) {
done(err);
});

});

it('with a transform function', function (done) {

request({
method: 'post',
uri: 'http://localhost:4000/200',
body: {
a: 'b'
},
json: true,
transform: function (body, response, resolveWithFullResponse) {
return body.split('').reverse().join('');
}
})
.then(function (body) {
// TODO: Check if request@next fixed sending the body
// expect(body).to.eql('POST /200 - {"a":"b"}'.split('').reverse().join(''));
expect(body).to.eql('POST /200 - {}'.split('').reverse().join(''));
done();
})
.catch(function (err) {
done(err);
});

});

it('that fails', function (done) {

request('http://localhost:1/200')
.then(function () {
done(new Error('Expected promise to be rejected.'));
})
.catch(function (err) {
expect(err instanceof errors.RequestError).to.eql(true);
done();
});

});

it('that gets a 500 response', function (done) {

request('http://localhost:4000/500')
.then(function () {
done(new Error('Expected promise to be rejected.'));
})
.catch(function (err) {
expect(err instanceof errors.StatusCodeError).to.eql(true);
done();
});

});

it('calling the callback, too', function (done) {

var callbackWasCalled = false;

request('http://localhost:4000/200', function () {
callbackWasCalled = true;
})
.then(function (body) {
expect(body).to.eql('GET /200');
expect(callbackWasCalled).to.eql(true);
done();
})
.catch(function (err) {
done(err);
});

});

});

describe('should support Request\'s', function () {

// TODO: Add tests for Request's non-promise related features

});

});

0 comments on commit 536bc91

Please sign in to comment.