Skip to content
Merged
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
69 changes: 67 additions & 2 deletions packages/http-client/__tests__/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ describe('basics', () => {
_http = new httpm.HttpClient('http-client-tests')
})

afterEach(() => {})
afterEach(() => {
// Clean up environment variable to prevent test pollution
delete process.env['ACTIONS_ORCHESTRATION_ID']
})

it('constructs', () => {
const http: httpm.HttpClient = new httpm.HttpClient('thttp-client-tests')
Expand Down Expand Up @@ -60,7 +63,7 @@ describe('basics', () => {
const body: string = await res.readBody()
const obj = JSON.parse(body)
expect(obj.url).toBe('https://postman-echo.com/get')
expect(obj.headers['user-agent']).toBeFalsy()
expect(obj.headers['user-agent']).toBe('actions/http-client')
})

/* TODO write a mock rather then relying on a third party
Expand Down Expand Up @@ -374,4 +377,66 @@ 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}`
)
})

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'
)
})

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')
})

it('uses default user-agent with orchestration ID when no custom user-agent provided', async () => {
const orchId = 'test-default-id-12345'
process.env['ACTIONS_ORCHESTRATION_ID'] = orchId

const http: httpm.HttpClient = new httpm.HttpClient()
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(
`actions/http-client actions_orchestration_id/${orchId}`
)
})
})
14 changes: 13 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,18 @@ export class HttpClient {
return proxyAgent
}

private _getUserAgentWithOrchestrationId(userAgent?: string): string {
const baseUserAgent = userAgent || 'actions/http-client'
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, '_')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove invalid characters instead of replacing with underscores?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't need to since the orchestrationid we generate always has valid chars.

return `${baseUserAgent} actions_orchestration_id/${sanitizedId}`
}
return baseUserAgent
}

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