Skip to content

Commit 4070b07

Browse files
authored
Merge pull request #213 from reportportal/feature/update-work-with-axios
Keep axios instance between requests. Update work with headers
2 parents 72695fb + efc9462 commit 4070b07

18 files changed

+195
-191
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules
22
build
3-
spec/**/*.json
3+
__tests__/**/*.json

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
### Changed
22
- **Breaking change** Drop support of Node.js 12. The version [5.1.4](https://github.com/reportportal/client-javascript/releases/tag/v5.1.4) is the latest that supports it.
3+
- The client now creates an instance of the `axios` HTTP client in the constructor.
4+
- The `HOST` HTTP header is added to all requests as it was skipped by the HTTP client.
35
### Fixed
4-
- Proxy support on HTTPS requests. Resolves [#30](https://github.com/reportportal/client-javascript/issues/30), related to [axios#4531](https://github.com/axios/axios/issues/4531).
56
- Allow using `restClientConfig` in `checkConnect()` method. Thanks to [stevez](https://github.com/stevez).
67
### Security
78
- Updated versions of vulnerable packages (braces).
File renamed without changes.
File renamed without changes.

spec/helpers.spec.js renamed to __tests__/helpers.spec.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const os = require('os');
22
const fs = require('fs');
33
const glob = require('glob');
44
const helpers = require('../lib/helpers');
5-
const RestClient = require('../lib/rest');
65
const pjson = require('../package.json');
76

87
describe('Helpers', () => {
@@ -27,34 +26,6 @@ describe('Helpers', () => {
2726
});
2827
});
2928

30-
describe('getServerResults', () => {
31-
it('calls RestClient#request', () => {
32-
jest.spyOn(RestClient, 'request').mockImplementation();
33-
34-
helpers.getServerResult(
35-
'http://localhost:80/api/v1',
36-
{ userId: 1 },
37-
{
38-
headers: {
39-
'X-Custom-Header': 'WOW',
40-
},
41-
},
42-
'POST',
43-
);
44-
45-
expect(RestClient.request).toHaveBeenCalledWith(
46-
'POST',
47-
'http://localhost:80/api/v1',
48-
{ userId: 1 },
49-
{
50-
headers: {
51-
'X-Custom-Header': 'WOW',
52-
},
53-
},
54-
);
55-
});
56-
});
57-
5829
describe('readLaunchesFromFile', () => {
5930
it('should return the right ids', () => {
6031
jest.spyOn(glob, 'sync').mockReturnValue(['rplaunch-fileOne.tmp', 'rplaunch-fileTwo.tmp']);
File renamed without changes.

spec/report-portal-client.spec.js renamed to __tests__/report-portal-client.spec.js

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const process = require('process');
22
const RPClient = require('../lib/report-portal-client');
3-
const RestClient = require('../lib/rest');
43
const helpers = require('../lib/helpers');
54
const { OUTPUT_TYPES } = require('../lib/constants/outputs');
65

@@ -134,41 +133,12 @@ describe('ReportPortal javascript client', () => {
134133
project: 'test',
135134
endpoint: 'https://abc.com',
136135
});
137-
jest.spyOn(RestClient, 'request').mockReturnValue(Promise.resolve('ok'));
136+
jest.spyOn(client.restClient, 'request').mockReturnValue(Promise.resolve('ok'));
138137

139138
const request = client.checkConnect();
140139

141140
return expect(request).resolves.toBeDefined();
142141
});
143-
144-
it('client should include restClientConfig', () => {
145-
const client = new RPClient({
146-
apiKey: 'test',
147-
project: 'test',
148-
endpoint: 'https://abc.com/v1',
149-
restClientConfig: {
150-
proxy: false,
151-
timeout: 0,
152-
},
153-
});
154-
jest.spyOn(RestClient, 'request').mockImplementation();
155-
156-
client.checkConnect();
157-
158-
expect(RestClient.request).toHaveBeenCalledWith(
159-
'GET',
160-
'https://abc.com/v1/user',
161-
{},
162-
{
163-
headers: {
164-
'User-Agent': 'NodeJS',
165-
Authorization: `bearer test`,
166-
},
167-
proxy: false,
168-
timeout: 0,
169-
},
170-
);
171-
});
172142
});
173143

174144
describe('triggerAnalyticsEvent', () => {
@@ -278,15 +248,11 @@ describe('ReportPortal javascript client', () => {
278248
startTime: time,
279249
});
280250

281-
expect(client.restClient.create).toHaveBeenCalledWith(
282-
'launch',
283-
{
284-
name: 'Test launch name',
285-
startTime: time,
286-
attributes: fakeSystemAttr,
287-
},
288-
{ headers: client.headers },
289-
);
251+
expect(client.restClient.create).toHaveBeenCalledWith('launch', {
252+
name: 'Test launch name',
253+
startTime: time,
254+
attributes: fakeSystemAttr,
255+
});
290256
});
291257

292258
it('should call restClient with suitable parameters, attributes is concatenated', () => {
@@ -312,22 +278,18 @@ describe('ReportPortal javascript client', () => {
312278
attributes: [{ value: 'value' }],
313279
});
314280

315-
expect(client.restClient.create).toHaveBeenCalledWith(
316-
'launch',
317-
{
318-
name: 'Test launch name',
319-
startTime: time,
320-
attributes: [
321-
{ value: 'value' },
322-
{
323-
key: 'client',
324-
value: 'client-name|1.0',
325-
system: true,
326-
},
327-
],
328-
},
329-
{ headers: client.headers },
330-
);
281+
expect(client.restClient.create).toHaveBeenCalledWith('launch', {
282+
name: 'Test launch name',
283+
startTime: time,
284+
attributes: [
285+
{ value: 'value' },
286+
{
287+
key: 'client',
288+
value: 'client-name|1.0',
289+
system: true,
290+
},
291+
],
292+
});
331293
});
332294

333295
it('dont start new launch if launchDataRQ.id is not empty', () => {
@@ -599,9 +561,7 @@ describe('ReportPortal javascript client', () => {
599561

600562
expect(promise.then).toBeDefined();
601563
await promise;
602-
expect(client.restClient.create).toHaveBeenCalledWith('launch/merge', fakeMergeDataRQ, {
603-
headers: client.headers,
604-
});
564+
expect(client.restClient.create).toHaveBeenCalledWith('launch/merge', fakeMergeDataRQ);
605565
});
606566

607567
it('should not call rest client if something went wrong', async () => {

spec/rest.spec.js renamed to __tests__/rest.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ const nock = require('nock');
22
const isEqual = require('lodash/isEqual');
33
const http = require('http');
44
const RestClient = require('../lib/rest');
5+
const logger = require('../lib/logger');
56

67
describe('RestClient', () => {
78
const options = {
89
baseURL: 'http://report-portal-host:8080/api/v1',
910
headers: {
10-
Authorization: 'bearer 00000000-0000-0000-0000-000000000000',
1111
'User-Agent': 'NodeJS',
12+
Authorization: 'Bearer 00000000-0000-0000-0000-000000000000',
1213
},
1314
restClientConfig: {
1415
agent: {
@@ -34,6 +35,21 @@ describe('RestClient', () => {
3435
expect(restClient.baseURL).toBe(options.baseURL);
3536
expect(restClient.headers).toEqual(options.headers);
3637
expect(restClient.restClientConfig).toEqual(options.restClientConfig);
38+
expect(restClient.axiosInstance).toBeDefined();
39+
});
40+
41+
it('adds Logger to axios instance if enabled', () => {
42+
const spyLogger = jest.spyOn(logger, 'addLogger').mockReturnValue();
43+
const optionsWithLoggerEnabled = {
44+
...options,
45+
restClientConfig: {
46+
...options.restClientConfig,
47+
debug: true,
48+
},
49+
};
50+
const client = new RestClient(optionsWithLoggerEnabled);
51+
52+
expect(spyLogger).toHaveBeenCalledWith(client.axiosInstance);
3753
});
3854
});
3955

File renamed without changes.

jest.config.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
module.exports = {
22
moduleFileExtensions: ['js'],
3-
"testMatch": [
4-
"<rootDir>/spec/**/*[sS]pec.js"
5-
],
6-
coverageReporters: ["lcov", "text-summary"],
3+
testRegex: '/__tests__/.*\\.(test|spec).js$',
4+
testEnvironment: 'node',
5+
collectCoverageFrom: ['lib/**/*.js', '!lib/logger.js'],
6+
coverageThreshold: {
7+
global: {
8+
branches: 80,
9+
functions: 75,
10+
lines: 80,
11+
statements: 80,
12+
},
13+
},
714
bail: false,
815
};

0 commit comments

Comments
 (0)