Skip to content

Commit f65b727

Browse files
author
Joe Goggins
committed
Adds getEventStream tests that validate X-Particle-Tool and X-Particle-Project http headers
get set as we'd expect when using setContext('tool') or setContext('project')
1 parent 8cfbc89 commit f65b727

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/Particle.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,44 @@ describe('ParticleAPI', () => {
937937
sinon.restore();
938938
});
939939

940+
describe('http headers', () => {
941+
it('initializes options correctly with a blank headers object', async () => {
942+
const { options } = await api.getEventStream({});
943+
expect(options).to.be.an('object');
944+
expect(options.headers).to.be.an('object');
945+
});
946+
947+
it('determines http headers using ._getDefaultHttpHeadersForContext()', async () => {
948+
const fakeHeaders = { 'X-Fake-Header': 'foo' };
949+
sinon.stub(api, '_getDefaultHttpHeadersForContext').returns(fakeHeaders);
950+
const { options } = await api.getEventStream({});
951+
expect(options).to.be.an('object');
952+
expect(options.headers).to.eql(fakeHeaders);
953+
});
954+
955+
describe('._getDefaultHttpHeadersForContext() (a way to get http headers from context)', () => {
956+
957+
it('returns empty object when no context has been set', () => {
958+
expect(api._getDefaultHttpHeadersForContext()).to.eql({});
959+
});
960+
961+
it('includes X-Particle-Tool when this.context.tool is set', () => {
962+
api.setContext('tool', { name: 'fake', version: '1.2.3' });
963+
const r = api._getDefaultHttpHeadersForContext();
964+
expect(r).to.be.an('object');
965+
expect(r['X-Particle-Tool']).to.eql('fake@1.2.3');
966+
});
967+
968+
it('includes X-Particle-Project when this.context.project is set', () => {
969+
api.setContext('project', { name: 'fake', version: '1.2.3' });
970+
const r = api._getDefaultHttpHeadersForContext();
971+
expect(r).to.be.an('object');
972+
expect(r['X-Particle-Project']).to.eql('fake@1.2.3');
973+
});
974+
});
975+
976+
});
977+
940978
it('requests public events', () => {
941979
return api.getEventStream({ }).then(({ uri }) => {
942980
uri.should.endWith('events');

0 commit comments

Comments
 (0)