Skip to content

Commit

Permalink
Merge pull request #16 from pactflow/fix/upstream-merge
Browse files Browse the repository at this point in the history
fix: replace unmaintained dependency request for axios (merge from upstream)
  • Loading branch information
vwong authored Mar 20, 2023
2 parents 252fac3 + 72dadeb commit 8ac9d3e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 56 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set MSVS version
run: npm config set msvs_version 2017
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
Expand All @@ -62,4 +60,4 @@ jobs:
# with:
# node-version: ${{ matrix.node-version }}
# - run: ./bin/build.sh
# shell: bash
# shell: bash
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.vs
node_modules
.idea
.npmrc
build-output
npm-debug.log
package-lock.json
Expand Down
63 changes: 13 additions & 50 deletions lib/swagger-mock-validator/clients/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,23 @@
import * as request from 'request';

type RequestOptions = request.Options;

const hasHttp2xxStatusCode = (response: request.RequestResponse) =>
response.statusCode && response.statusCode >= 200 && response.statusCode <= 299;
import axios, {AxiosRequestHeaders} from 'axios';

export class HttpClient {
private static getRequestOptions(url: string, auth?: string): RequestOptions {
let requestOptions: RequestOptions = {
public async get(url: string, auth?: string): Promise<string> {
const headers: AxiosRequestHeaders = auth
? {authorization: 'Basic ' + Buffer.from(auth).toString('base64')}
: {}
const response = await axios.get(url, {
headers,
timeout: 30000,
url
};
if (auth) {
requestOptions = {...requestOptions, headers: {
authorization: 'Basic ' + Buffer.from(auth).toString('base64')
}};
}

return requestOptions;
}

public get(url: string, auth?: string): Promise<string> {
return new Promise((resolve, reject) => {
const requestOptions = HttpClient.getRequestOptions(url, auth);

request(requestOptions, (error, response, body) => {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
reject(new Error(`Expected 200 but received ${response.statusCode}`));
} else {
resolve(body);
}
});
transformResponse: (data) => data,
validateStatus: (status) => status === 200
});
return response.data;
}

public post(url: string, body: any): Promise<void> {
const requestOptions = {
body,
json: true,
method: 'POST',
public async post(url: string, body: any): Promise<void> {
await axios.post(url, body, {
timeout: 5000,
url
};

return new Promise<void>((resolve, reject) => {
request(requestOptions, (error, response) => {
if (error) {
reject(error);
} else if (!hasHttp2xxStatusCode(response)) {
reject(new Error(`Expected 2xx but received ${response.statusCode}}`));
} else {
resolve();
}
});
validateStatus: (status) => status >= 200 && status <= 299
});
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@types/lodash": "^4.14.126",
"@types/node": "^14.14.23",
"@types/q": "^1.0.0",
"@types/request": "^2.0.3",
"@types/validator": "^13.1.0",
"@types/verror": "^1.9.0",
"ansi-colors": "^4.1.1",
Expand All @@ -59,13 +58,13 @@
},
"dependencies": {
"ajv": "^6.12.6",
"axios": "^0.27.2",
"commander": "^7.0.0",
"decimal.js": "^10.2.0",
"js-yaml": "^4.0.0",
"jsonpointer": "^5.0.0",
"lodash": "^4.17.10",
"openapi-types": "^7.0.1",
"request": "^2.87.0",
"swagger-parser": "^10.0.2",
"uuidjs": "^4.0.3",
"validator": "^13.1.17",
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('swagger-mock-validator/cli', () => {
swagger: urlTo('test/e2e/fixtures/swagger-provider.json')
}));

expect(error).toEqual(jasmine.stringMatching('Expected 200 but received 404'));
expect(error).toEqual(jasmine.stringMatching('Request failed with status code 404'));
}, 30000);

it('should succeed when a pact broker url and a swagger url are compatible', async () => {
Expand Down

0 comments on commit 8ac9d3e

Please sign in to comment.