Skip to content

Commit

Permalink
Merge pull request #123 from aydrian/issue-122
Browse files Browse the repository at this point in the history
Added json flag to base request and tests to check for JSON response
  • Loading branch information
aydrian committed Mar 22, 2016
2 parents 8e2a6c1 + 9a735c5 commit f16b2bc
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lib/sparkpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ SparkPost.prototype.request = function( options, callback ) {
// set Strict SSL (Always true)
options.strictSSL = true;

// set JSON (Always true)
options.json = true;

// default to accepting gzipped responses
if (typeof options.gzip === 'undefined') {
options.gzip = true;
Expand Down
143 changes: 130 additions & 13 deletions test/spec/sparkpost.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('SparkPost Library', function() {
expect(err).to.be.null;
expect(data.statusCode).to.equal(200);
expect(data.body).to.equal(TEST_MESSAGE + TEST_MESSAGE);

// finish async test
done();
});
Expand Down Expand Up @@ -227,11 +227,18 @@ describe('SparkPost Library', function() {
});

describe('get method', function() {
it('should deliver a GET + response', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');
var client;

before(function() {
// setting up a client for all tests to use
var key = '12345678901234567890';
var client = new SparkPost(key);
var options = {};

client = new SparkPost(key, options);
});

it('should deliver a GET + response', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');

nock('https://api.sparkpost.com')
.get('/api/v1/get/test')
Expand All @@ -248,14 +255,41 @@ describe('SparkPost Library', function() {
done();
});
});

it('should return a parsed JSON object', function(done) {
nock('https://test.sparkpost.com')
.get('/test')
.reply(200, '{ "ok": true }');

var options = {
method: 'GET'
, uri: 'https://test.sparkpost.com/test'
};

client.request(options, function(err, data) {
expect(data.body).to.not.be.a('string');
expect(data.body).to.be.an('object');
expect(data.body).to.deep.equal({ok: true});

// finish async test
done();
});
});
});

describe('post method', function() {
it('should deliver a POST', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');
var client;

before(function() {
// setting up a client for all tests to use
var key = '12345678901234567890';
var client = new SparkPost(key);
var options = {};

client = new SparkPost(key, options);
});

it('should deliver a POST', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');

nock('https://api.sparkpost.com')
.post('/api/v1/post/test')
Expand All @@ -279,14 +313,44 @@ describe('SparkPost Library', function() {
done();
});
});

it('should return a parsed JSON object', function(done) {
nock('https://test.sparkpost.com')
.post('/test')
.reply(200, '{ "ok": true }');

var options = {
method: 'POST'
, uri: 'https://test.sparkpost.com/test'
, json: {
testingData: 'test data'
}
};

client.request(options, function(err, data) {
expect(data.body).to.not.be.a('string');
expect(data.body).to.be.an('object');
expect(data.body).to.deep.equal({ok: true});

// finish async test
done();
});
});
});

describe('put method', function() {
it('should deliever a PUT/UPDATE', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');
var client;

before(function() {
// setting up a client for all tests to use
var key = '12345678901234567890';
var client = new SparkPost(key);
var options = {};

client = new SparkPost(key, options);
});

it('should deliver a PUT/UPDATE', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');

nock('https://api.sparkpost.com')
.put('/api/v1/put/test')
Expand All @@ -310,14 +374,44 @@ describe('SparkPost Library', function() {
done();
});
});

it('should return a parsed JSON object', function(done) {
nock('https://test.sparkpost.com')
.put('/test')
.reply(200, '{ "ok": true }');

var options = {
method: 'PUT'
, uri: 'https://test.sparkpost.com/test'
, json: {
testingData: 'test data'
}
};

client.request(options, function(err, data) {
expect(data.body).to.not.be.a('string');
expect(data.body).to.be.an('object');
expect(data.body).to.deep.equal({ok: true});

// finish async test
done();
});
});
});

describe('delete method', function() {
it('should deliever a DELETE', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');
var client;

before(function() {
// setting up a client for all tests to use
var key = '12345678901234567890';
var client = new SparkPost(key);
var options = {};

client = new SparkPost(key, options);
});

it('should deliver a DELETE', function(done) {
var requestSpy = sinon.spy(SparkPost.prototype, 'request');

nock('https://api.sparkpost.com')
.delete('/api/v1/delete/test')
Expand All @@ -341,5 +435,28 @@ describe('SparkPost Library', function() {
done();
});
});

it('should return a parsed JSON object', function(done) {
nock('https://test.sparkpost.com')
.delete('/test')
.reply(200, '{ "ok": true }');

var options = {
method: 'DELETE'
, uri: 'https://test.sparkpost.com/test'
, json: {
testingData: 'test data'
}
};

client.request(options, function(err, data) {
expect(data.body).to.not.be.a('string');
expect(data.body).to.be.an('object');
expect(data.body).to.deep.equal({ok: true});

// finish async test
done();
});
});
});
});

0 comments on commit f16b2bc

Please sign in to comment.