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

Added json flag to base request and tests to check for JSON response #123

Merged
merged 2 commits into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor: Do we need separate string and an object versions of the response object? We could just keep it in a var and encode/decode/compare as necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you give nock and object to return, it will do stuff on it's side. I wanted to make sure that the response was a string and was being manipulated by the request lib.


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

client.request(options, function(err, data) {
expect(data.body).to.not.be.a('string');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both !string and ==object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Belt and suspenders. Also I didn't know if it was like "a function is an object, a string is an object, I'm javascript and I treat everything as an object"

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 deliever a PUT/UPDATE', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delivier -> deliver

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't even consistently spell something wrong.

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 deliever a DELETE', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delivier -> deliver

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow... that's been in there forever. Ha

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();
});
});
});
});