Skip to content

Commit b3b6388

Browse files
committed
fix proxy handeler and add unit test
1 parent 3f334ff commit b3b6388

File tree

5 files changed

+106
-24
lines changed

5 files changed

+106
-24
lines changed

.github/workflows/ci.yml

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
name: AST Javascript wrapper CI
22

3-
on: [pull_request]
3+
on: [ pull_request ]
44
jobs:
5+
unit-tests:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v4
9+
- name: Use Node.js 22.11.0
10+
uses: actions/[email protected]
11+
with:
12+
node-version: 22.11.0
13+
registry-url: https://npm.pkg.github.com/
14+
- run: npm ci
15+
- name: Run Unit Tests
16+
run: npm run test:unit
517
integration-tests:
618
runs-on: ubuntu-latest
719
steps:
8-
- uses: actions/checkout@v4
9-
- name: Use Node.js 22.11.0
10-
uses: actions/[email protected]
11-
with:
12-
node-version: 22.11.0
13-
registry-url: https://npm.pkg.github.com/
14-
- run: npm ci
15-
- name: Code Linting
16-
run: npm run lint
17-
- run: npm run build --if-present
20+
- uses: actions/checkout@v4
21+
- name: Use Node.js 22.11.0
22+
uses: actions/[email protected]
23+
with:
24+
node-version: 22.11.0
25+
registry-url: https://npm.pkg.github.com/
26+
- run: npm ci
27+
- name: Code Linting
28+
run: npm run lint
29+
- run: npm run build --if-present
1830

19-
- name: Run tests
20-
env:
21-
CX_CLIENT_ID: ${{ secrets.CX_CLIENT_ID}}
22-
CX_CLIENT_SECRET: ${{ secrets.CX_CLIENT_SECRET}}
23-
CX_BASE_URI: ${{ secrets.CX_BASE_URI }}
24-
CX_TENANT: ${{ secrets.CX_TENANT }}
25-
CX_APIKEY: ${{ secrets.CX_APIKEY }}
26-
run: npm test
31+
- name: Run tests
32+
env:
33+
CX_CLIENT_ID: ${{ secrets.CX_CLIENT_ID}}
34+
CX_CLIENT_SECRET: ${{ secrets.CX_CLIENT_SECRET}}
35+
CX_BASE_URI: ${{ secrets.CX_BASE_URI }}
36+
CX_TENANT: ${{ secrets.CX_TENANT }}
37+
CX_APIKEY: ${{ secrets.CX_APIKEY }}
38+
run: npm test

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
// preset configs
2929
// preset: 'ts-jest/presets/js-with-ts',
3030
// which files to test and which to ignore
31-
testMatch: ['**/src/tests/*.test.(ts|tsx)'],
31+
testMatch: ['**/src/tests/**/*.test.(ts|tsx)'],
3232
testPathIgnorePatterns: ['/node_modules/', '/tmp/', '/coverage/', '/stories/', '/\\.storybook/'],
3333
// don't watch for file changes in node_modules
3434
watchPathIgnorePatterns: ['/node_modules/'],

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"postbuild": "copyfiles -u 1 src/tests/data/* dist/;",
2424
"lint": "eslint . --ext .ts",
2525
"lint-and-fix": "eslint . --ext .ts --fix",
26-
"test": "copyfiles -u 1 src/tests/data/* dist/; tsc && jest --runInBand"
26+
"test": "copyfiles -u 1 src/tests/data/* dist/; tsc && jest --runInBand",
27+
"test:unit": "tsc && jest --runInBand --testPathPattern=tests/unit"
2728
},
2829
"repository": "https://github.com/CheckmarxDev/ast-cli-javascript-wrapper-runtime-cli.git",
2930
"author": "Jay Nanduri",

src/main/client/AstClient.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class AstClient {
1919

2020
try {
2121
const proxyUrl = new URL(proxyEnv);
22-
if (proxyUrl.port === '') {
22+
const proxyPort = proxyUrl.port || (proxyUrl.protocol === 'http:' ? '80' : proxyUrl.protocol === 'https:' ? '443' : '');
23+
if (proxyPort === '') {
2324
logger.error(`Invalid proxy URL: ${proxyUrl}. Port is missing. Proceeding without proxy agent.`);
2425
return undefined;
2526
}
@@ -31,12 +32,12 @@ export class AstClient {
3132
const agent = tunnel.httpsOverHttp({
3233
proxy: {
3334
host: proxyUrl.hostname,
34-
port: Number(proxyUrl.port),
35+
port: Number(proxyPort),
3536
proxyAuth,
3637
}
3738
});
3839

39-
logger.info(`Using proxy agent for host: ${proxyUrl.hostname} and port: ${proxyUrl.port}`);
40+
logger.info(`Using proxy agent for host: ${proxyUrl.hostname} and port: ${proxyPort}`);
4041

4142
return {
4243
prepareRequest: (options: any): any => {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { AstClient } from "../../main/client/AstClient";
2+
3+
describe("AstClient - createProxyRequestHandler", () => {
4+
const OLD_ENV = process.env;
5+
6+
beforeEach(() => {
7+
jest.resetModules();
8+
process.env = { ...OLD_ENV };
9+
});
10+
11+
afterAll(() => {
12+
process.env = OLD_ENV;
13+
});
14+
15+
it("should return proxy with default port 80 when HTTP_PROXY is http without port", () => {
16+
process.env.HTTP_PROXY = "http://31.220.15.234:80";
17+
18+
const client = new AstClient();
19+
const handler = (client as any).createProxyRequestHandler();
20+
21+
expect(handler).toBeDefined();
22+
});
23+
24+
it("should return proxy with default port 443 when HTTP_PROXY is https without port", () => {
25+
process.env.HTTP_PROXY = "https://31.220.15.234:443";
26+
27+
const client = new AstClient();
28+
const handler = (client as any).createProxyRequestHandler();
29+
30+
expect(handler).toBeDefined();
31+
});
32+
33+
it("should return undefined when HTTP_PROXY is empty", () => {
34+
delete process.env.HTTP_PROXY;
35+
36+
const client = new AstClient();
37+
const handler = (client as any).createProxyRequestHandler();
38+
39+
expect(handler).toBeUndefined();
40+
});
41+
42+
it("should return undefined when HTTP_PROXY is invalid protocol", () => {
43+
process.env.HTTP_PROXY = "ftp://31.220.15.234";
44+
45+
const client = new AstClient();
46+
const handler = (client as any).createProxyRequestHandler();
47+
48+
expect(handler).toBeUndefined();
49+
});
50+
51+
it("should use default port 80 when HTTP_PROXY is http without port", () => {
52+
process.env.HTTP_PROXY = "http://31.220.15.234"; // ללא פורט מפורש
53+
54+
const client = new AstClient();
55+
const handler = (client as any).createProxyRequestHandler();
56+
57+
expect(handler).toBeDefined();
58+
});
59+
60+
it("should use default port 443 when HTTP_PROXY is https without port", () => {
61+
process.env.HTTP_PROXY = "https://31.220.15.234"; // ללא פורט מפורש
62+
63+
const client = new AstClient();
64+
const handler = (client as any).createProxyRequestHandler();
65+
66+
expect(handler).toBeDefined();
67+
});
68+
});

0 commit comments

Comments
 (0)