Skip to content

Commit

Permalink
feat: add user agent to requests to the codecov api (#1250)
Browse files Browse the repository at this point in the history
* feat: add user agent to requests to the codecov api

* Update src/helpers/web.ts

---------

Co-authored-by: Tom Hu <88201630+thomasrockhu-codecov@users.noreply.github.com>
  • Loading branch information
matt-codecov and thomasrockhu-codecov authored Oct 20, 2023
1 parent e60489b commit 597e04b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/helpers/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { sleep } from './util'

const maxRetries = 4
const baseBackoffDelayMs = 1000 // Adjust this value based on your needs.
export const userAgent: string = `codecov-uploader/${version}`

/**
*
Expand Down Expand Up @@ -209,9 +210,10 @@ export function generateRequestHeadersPOST(
postURL,
)

const headers = {
const headers: Record<string, string> = {
'X-Upload-Token': token,
'X-Reduced-Redundancy': 'false',
'User-Agent': userAgent,
}

return {
Expand All @@ -232,9 +234,10 @@ export function generateRequestHeadersPUT(
envs: UploaderEnvs,
args: UploaderArgs,
): IRequestHeaders {
const headers = {
const headers: Record<string, string> = {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip',
'User-Agent': userAgent,
}

return {
Expand Down
32 changes: 32 additions & 0 deletions test/helpers/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ import {
populateBuildParams,
uploadToCodecovPOST,
uploadToCodecovPUT,
userAgent,
} from '../../src/helpers/web'
import { IServiceParams, PostResults, UploaderArgs, UploaderEnvs } from '../../src/types.js'
import { createEmptyArgs } from '../test_helpers'

import * as utilModule from '../../src/helpers/util'
import { IncomingHttpHeaders } from 'undici/types/header'

function hasUserAgent(headers: IncomingHttpHeaders | string[]): headers is IncomingHttpHeaders {
return "User-Agent" in headers
}

describe('Web Helpers', () => {
let uploadURL: string
Expand Down Expand Up @@ -544,6 +549,13 @@ describe('generateRequestHeadersPOST()', () => {
'G',
)}&token=134&slug=testOrg/testUploader`,
)

const headers = requestHeaders.options.headers!
expect(hasUserAgent(headers)).toEqual(true)
if (hasUserAgent(headers)) {
expect(headers['User-Agent']).toEqual(userAgent)
}

expect(typeof requestHeaders.options.body).toEqual('undefined')
expect(typeof requestHeaders.agent).toEqual('undefined')
})
Expand All @@ -566,6 +578,12 @@ describe('generateRequestHeadersPOST()', () => {
)}&token=134&slug=testOrg/testUploader`,
)

const headers = requestHeaders.options.headers!
expect(hasUserAgent(headers)).toEqual(true)
if (hasUserAgent(headers)) {
expect(headers['User-Agent']).toEqual(userAgent)
}

expect(typeof requestHeaders.options.body).toEqual('undefined')
expect(requestHeaders.agent).toBeInstanceOf(ProxyAgent)
})
Expand All @@ -585,6 +603,13 @@ describe('generateRequestHeadersPUT()', () => {
)

expect(requestHeaders.url.href).toEqual('https://localhost.local/')

const headers = requestHeaders.options.headers!
expect(hasUserAgent(headers)).toEqual(true)
if (hasUserAgent(headers)) {
expect(headers['User-Agent']).toEqual(userAgent)
}

expect(requestHeaders.options.body).toEqual("I'm a coverage report!")
expect(typeof requestHeaders.agent).toEqual('undefined')
})
Expand All @@ -600,6 +625,13 @@ describe('generateRequestHeadersPUT()', () => {
)

expect(requestHeaders.url.href).toEqual('https://localhost.local/')

const headers = requestHeaders.options.headers!
expect(hasUserAgent(headers)).toEqual(true)
if (hasUserAgent(headers)) {
expect(headers['User-Agent']).toEqual(userAgent)
}

expect(requestHeaders.options.body).toEqual("I'm a coverage report!")
expect(requestHeaders.agent).toBeInstanceOf(ProxyAgent)
})
Expand Down

0 comments on commit 597e04b

Please sign in to comment.