Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
50 changes: 50 additions & 0 deletions packages/http-client/__tests__/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,54 @@ describe('basics', () => {
httpm.MediaTypes.ApplicationJson
)
})

it('appends orchestration ID to user-agent when ACTIONS_ORCHESTRATION_ID is set', async () => {
const orchId = 'test-orch-id-12345'
process.env['ACTIONS_ORCHESTRATION_ID'] = orchId

const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests')
const res: httpm.HttpClientResponse = await http.get(
'https://postman-echo.com/get'
)
expect(res.message.statusCode).toBe(200)
const body: string = await res.readBody()
const obj = JSON.parse(body)
expect(obj.headers['user-agent']).toBe(
`http-client-tests actions_orchestration_id/${orchId}`
)

delete process.env['ACTIONS_ORCHESTRATION_ID']
})
Comment thread
TingluoHuang marked this conversation as resolved.
Outdated

it('sanitizes invalid characters in orchestration ID', async () => {
const orchId = 'test (with) special/chars'
process.env['ACTIONS_ORCHESTRATION_ID'] = orchId

const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests')
const res: httpm.HttpClientResponse = await http.get(
'https://postman-echo.com/get'
)
expect(res.message.statusCode).toBe(200)
const body: string = await res.readBody()
const obj = JSON.parse(body)
// Spaces, parentheses, and slashes should be replaced with underscores
expect(obj.headers['user-agent']).toBe(
'http-client-tests actions_orchestration_id/test__with__special_chars'
)

delete process.env['ACTIONS_ORCHESTRATION_ID']
})
Comment thread
TingluoHuang marked this conversation as resolved.
Outdated

it('does not modify user-agent when ACTIONS_ORCHESTRATION_ID is not set', async () => {
delete process.env['ACTIONS_ORCHESTRATION_ID']

const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests')
const res: httpm.HttpClientResponse = await http.get(
'https://postman-echo.com/get'
)
expect(res.message.statusCode).toBe(200)
const body: string = await res.readBody()
const obj = JSON.parse(body)
expect(obj.headers['user-agent']).toBe('http-client-tests')
})
Comment thread
TingluoHuang marked this conversation as resolved.
})
18 changes: 17 additions & 1 deletion packages/http-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class HttpClient {
handlers?: ifm.RequestHandler[],
requestOptions?: ifm.RequestOptions
) {
this.userAgent = userAgent
this.userAgent = this._getUserAgentWithOrchestrationId(userAgent)
this.handlers = handlers || []
this.requestOptions = requestOptions
if (requestOptions) {
Expand Down Expand Up @@ -816,6 +816,22 @@ export class HttpClient {
return proxyAgent
}

private _getUserAgentWithOrchestrationId(
userAgent?: string
): string | undefined {
if (!userAgent) {
Comment thread
TingluoHuang marked this conversation as resolved.
Outdated
return undefined
}
const orchId = process.env['ACTIONS_ORCHESTRATION_ID']
if (orchId) {
// Sanitize the orchestration ID to ensure it contains only valid characters
// Valid characters: 0-9, a-z, _, -, .
const sanitizedId = orchId.replace(/[^a-z0-9_.\-]/gi, '_')
return `${userAgent} actions_orchestration_id/${sanitizedId}`
}
return userAgent
}

private async _performExponentialBackoff(retryNumber: number): Promise<void> {
retryNumber = Math.min(ExponentialBackoffCeiling, retryNumber)
const ms: number = ExponentialBackoffTimeSlice * Math.pow(2, retryNumber)
Expand Down