Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add logging for proxy use #603

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"lint": "eslint \"src/**/*.ts\"",
"test": "npm run lint && npm run build && jest --runInBand",
"test:local": "npm run lint && npm run build && jest --runInBand --no-cache",
"test:e2e": "jest test/e2e/output.test.ts",
"build:clean": "rm -rf dist",
"build": "tsc --build",
Expand Down
84 changes: 50 additions & 34 deletions src/helpers/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,68 +152,84 @@ export function generateRequestHeadersPOST(
source: string,
args: UploaderArgs,
): IRequestHeaders {
if (args.upstream !== '') {
const proxyAgent = new HttpsProxyAgent(args.upstream)
return {
url: new URL(`/upload/v4?package=${getPackage(
source,
)}&token=${token}&${query}`, postURL),
options: {
agent: proxyAgent,
method: 'post',
headers: {
'X-Upload-Token': token,
'X-Reduced-Redundancy': 'false',
},
},
}
if (args.upstream !== '') {
let displayURL: string
if (args.upstream.includes('@')) {
displayURL = args.upstream.split('@')[1]
} else {
displayURL = args.upstream
}

info(`Using ${displayURL} as the proxy for the POST command`)

const proxyAgent = new HttpsProxyAgent(args.upstream)
return {
url: new URL(`/upload/v4?package=${getPackage(
source,
)}&token=${token}&${query}`, postURL),
options: {
agent: proxyAgent,
method: 'post',
headers: {
'X-Upload-Token': token,
'X-Reduced-Redundancy': 'false',
},
},
}
}

return {
url: `${uploadURL}/upload/v4?package=${getPackage(
source,
)}&token=${token}&${query}`,
options: {
method: 'post',
headers: {
'X-Upload-Token': token,
'X-Reduced-Redundancy': 'false',
},
},
}
}

export function generateRequestHeadersPUT(
uploadURL: URL,
uploadFile: string | Buffer,
args: UploaderArgs,
): IRequestHeaders {

if (args.upstream !== '') {
const proxyAgent = new HttpsProxyAgent(args.upstream)
return {
url: uploadURL,
options: {
method: 'put',
agent: proxyAgent,
body: uploadFile,
headers: {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip',
},
},
}
if (args.upstream !== '') {
let displayURL: string
if (args.upstream.includes('@')) {
displayURL = args.upstream.split('@')[1]
} else {
displayURL = args.upstream
}

info(`Using ${displayURL} as the proxy for the PUT command`)

const proxyAgent = new HttpsProxyAgent(args.upstream)
return {
url: uploadURL, options: {
url: uploadURL,
options: {
method: 'put',
agent: proxyAgent,
body: uploadFile,
headers: {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip',
},
}

},
}

}
return {
url: uploadURL,
options: {
method: 'put',
body: uploadFile,
headers: {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip',
},
},
}
}
50 changes: 50 additions & 0 deletions test/helpers/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,53 @@ describe('generateRequestHeadersPUT()', () => {
)
})
})


describe('generateRequestHeadersPOST()', () => {
test('when args.upstream is set, returns a log line', () => {
// Arrange
const args: UploaderArgs = { ...createEmptyArgs() , upstream: "https://codecov.local:3131"}

// Act
void generateRequestHeadersPOST("https://unreachable.codecov.local", "not a token", "", "", args)

// Assert
expect(console.log).toHaveBeenCalledWith(expect.stringContaining(`Using https://codecov.local:3131 as the proxy for the POST command`))
})

test('when args.upstream is passed basic credentials, does not log the credentials, only the host', () => {
// Arrange
const args: UploaderArgs = { ...createEmptyArgs() , upstream: "https://testUser:[email protected]:3131"}

// Act
void generateRequestHeadersPOST("https://unreachable.codecov.local", "not a token", "", "", args)

// Assert
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Using codecov.local:3131 as the proxy for the POST command'))
})
})

describe('generateRequestHeadersPUT()', () => {
test('when args.upstream is set, returns a log line', () => {
// Arrange
const args: UploaderArgs = { ...createEmptyArgs() , upstream: "https://codecov.local:3131"}

// Act
void generateRequestHeadersPUT("https://unreachable.codecov.local", "I'm a coverage file", args)

// Assert
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Using https://codecov.local:3131 as the proxy for the PUT command'))
})

test('when args.upstream is passed basic credentials, does not log the credentials, only the host', () => {
// Arrange
const args: UploaderArgs = { ...createEmptyArgs() , upstream: "https://testUser:[email protected]:3131"}

// Act
void generateRequestHeadersPUT("https://unreachable.codecov.local", "I'm a coverage file", args)

// Assert
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Using https://codecov.local:3131 as the proxy for the PUT command'))
})
})