Skip to content

Commit

Permalink
feat(rest): support for Axios 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-molak committed Dec 18, 2022
1 parent 9886526 commit b1ab268
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 65 deletions.
72 changes: 36 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"scripts": {
"clean": "rimraf target && lerna run clean",
"cc": "nx reset",
"lint": "eslint --ext ts --config .eslintrc.yml .",
"lint:fix": "eslint --ext ts --config .eslintrc.yml --fix .",
"compile:all": "lerna run compile",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Ensure, equals } from '@serenity-js/assertions';
import { LogicError } from '@serenity-js/core';
import { AxiosRequestHeaders } from 'axios';
import { afterEach, beforeEach, describe, it } from 'mocha';

import { ChangeApiConfig, GetRequest, LastResponse, Send } from '../../../src';
Expand Down Expand Up @@ -38,7 +39,7 @@ describe('ChangeApiConfig', () => {
const location = activity.instantiationLocation();

expect(location.path.basename()).to.equal('ChangeAPIConfig.spec.ts');
expect(location.line).to.equal(37);
expect(location.line).to.equal(38);
expect(location.column).to.equal(46);
});
});
Expand Down Expand Up @@ -81,7 +82,7 @@ describe('ChangeApiConfig', () => {
const location = activity.instantiationLocation();

expect(location.path.basename()).to.equal('ChangeAPIConfig.spec.ts');
expect(location.line).to.equal(80);
expect(location.line).to.equal(81);
expect(location.column).to.equal(46);
});
});
Expand All @@ -94,8 +95,10 @@ describe('ChangeApiConfig', () => {
const dataMatcher = undefined;
mock.onGet(originalUrl, dataMatcher).replyOnce(401);
mock.onGet(originalUrl, dataMatcher, {
'Accept': 'application/json, text/plain, */*',
'Authorization': 'my-token'
asymmetricMatch: function (actual: AxiosRequestHeaders) {
return actual.get('Accept') === 'application/json, text/plain, */*'
&& actual.get('Authorization') === 'my-token'
}
}).replyOnce(200);
});

Expand Down Expand Up @@ -126,7 +129,7 @@ describe('ChangeApiConfig', () => {
const location = activity.instantiationLocation();

expect(location.path.basename()).to.equal('ChangeAPIConfig.spec.ts');
expect(location.line).to.equal(125);
expect(location.line).to.equal(128);
expect(location.column).to.equal(46);
});
});
Expand Down
6 changes: 1 addition & 5 deletions packages/rest/src/screenplay/abilities/CallAnApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Ability, ConfigurationError, LogicError, TestCompromisedError, UsesAbilities } from '@serenity-js/core';
import axios, { AxiosDefaults, AxiosError, AxiosInstance, AxiosPromise, AxiosRequestConfig, AxiosResponse } from 'axios';
const mergeConfig = require('axios/lib/core/mergeConfig'); // eslint-disable-line @typescript-eslint/no-var-requires
const buildFullPath = require('axios/lib/core/buildFullPath'); // eslint-disable-line @typescript-eslint/no-var-requires

/**
* An {@apilink Ability} that enables the {@apilink Actor} to call an HTTP API.
Expand Down Expand Up @@ -158,9 +156,7 @@ export class CallAnApi implements Ability {
* @param config
*/
resolveUrl(config: AxiosRequestConfig): string {
const merged = mergeConfig(this.axiosInstance.defaults, config);

return buildFullPath(merged.baseURL, merged.url);
return this.axiosInstance.getUri(config);
}

/**
Expand Down
38 changes: 22 additions & 16 deletions packages/rest/src/screenplay/interactions/Send.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Answerable, AnswersQuestions, CollectsArtifacts, Interaction, UsesAbilities } from '@serenity-js/core';
import { Artifact, HTTPRequestResponse, Name, RequestAndResponse } from '@serenity-js/core/lib/model';
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { AxiosHeaders, AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders, RawAxiosRequestHeaders, RawAxiosResponseHeaders } from 'axios';

import { CallAnApi } from '../abilities';

Expand Down Expand Up @@ -67,21 +67,27 @@ export class Send extends Interaction {
}

private responseToArtifact(targetUrl: string, response: AxiosResponse): Artifact {
const
request: AxiosRequestConfig = response.config,
requestAndResponse: RequestAndResponse = {
request: {
method: request.method,
url: targetUrl,
headers: request.headers,
data: request.data,
},
response: {
status: response.status,
headers: response.headers,
data: response.data,
},
};
const request: AxiosRequestConfig = response.config;

const axiosRequestHeaders: RawAxiosRequestHeaders = request.headers;
const requestHeaders: Record<string, string | number | boolean> = AxiosHeaders.from(axiosRequestHeaders).toJSON(true) as Record<string, string | number | boolean>;

const axiosResponseHeaders: RawAxiosResponseHeaders | AxiosResponseHeaders = response.headers;
const responseHeaders: Record<string, string> & { 'set-cookie'?: string[] } = AxiosHeaders.from(axiosResponseHeaders).toJSON(false) as RawAxiosResponseHeaders;

const requestAndResponse: RequestAndResponse = {
request: {
method: request.method,
url: targetUrl,
headers: requestHeaders,
data: request.data,
},
response: {
status: response.status,
headers: responseHeaders,
data: response.data,
},
};

return HTTPRequestResponse.fromJSON(requestAndResponse);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/serenity-bdd/src/cli/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export = {
configure({
actors: new UpdateCommandActors(
new Path(process.cwd()),
() => axiosClient(repository, !! argv.ignoreSSL, process.env, Credentials.fromString(argv.auth))
() => axiosClient(repository, Boolean(argv.ignoreSSL), process.env, Credentials.fromString(argv.auth))
),
crew: [
new NotificationReporter(printer),
Expand Down
3 changes: 1 addition & 2 deletions packages/serenity-bdd/src/cli/io/axiosClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConfigurationError } from '@serenity-js/core';
import axios, { AxiosInstance, AxiosProxyConfig, AxiosRequestConfig } from 'axios';
import { httpAdapter } from 'axios/lib/adapters/http';
import * as fs from 'fs';
import * as https from 'https';
import { URL } from 'url';
Expand Down Expand Up @@ -31,7 +30,7 @@ export function axiosClient(

const options: AxiosRequestConfig = {
baseURL: repository.toString(),
adapter: httpAdapter,
adapter: [ 'http' ],
auth: repositoryAuth,
};

Expand Down

0 comments on commit b1ab268

Please sign in to comment.