Skip to content

Commit 7751681

Browse files
committed
Define user-agent header
1 parent 2ec68ae commit 7751681

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/Agent.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import fetch from 'node-fetch';
2121
import FormData from 'form-data';
2222
import qs from 'qs';
2323
import fs from 'fs';
24+
import packageJson from '../package.json';
2425

2526
/**
2627
* The object returned for a basic request
@@ -223,6 +224,7 @@ export default class Agent {
223224
actualUri = `${actualUri}${hasParams ? '&' : '?'}${queryParams}`;
224225
}
225226

227+
const userAgentHeader = { 'User-Agent': `${packageJson.name}/${packageJson.version} (${packageJson.repository.url})` };
226228
let body;
227229
let contentTypeHeader;
228230
if (files){
@@ -236,6 +238,7 @@ export default class Agent {
236238
body = JSON.stringify(data);
237239
}
238240
const finalHeaders = Object.assign({},
241+
userAgentHeader,
239242
contentTypeHeader,
240243
this._getAuthorizationHeader(auth),
241244
this._getContextHeaders(context),

test/Agent.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ describe('Agent', () => {
345345
expect(extractFilename(opts.body, 'file2', 3)).to.eql('dir/file2path.cpp');
346346
});
347347

348+
it('sets the user agent to particle-api-js', () => {
349+
const [, opts] = agent._buildRequest({ uri: 'uri', method: 'get' });
350+
expect(opts.headers).to.have.property('User-Agent').that.match(/^particle-api-js/);
351+
});
352+
348353
if (!inBrowser()){
349354
it('should handle Windows nested dirs', () => {
350355
const files = {

0 commit comments

Comments
 (0)