2020-06-03 01:39:46 +00:00
|
|
|
import * as http from 'http'
|
|
|
|
import * as https from 'https'
|
2023-09-15 15:04:21 +00:00
|
|
|
import { createProxy } from 'proxy'
|
2020-06-03 01:39:46 +00:00
|
|
|
|
|
|
|
// Default values are set when the module is imported, so we need to set proxy first.
|
|
|
|
const proxyUrl = 'http://127.0.0.1:8081'
|
|
|
|
const originalProxyUrl = process.env['https_proxy']
|
|
|
|
process.env['https_proxy'] = proxyUrl
|
|
|
|
// eslint-disable-next-line import/first
|
2023-09-15 13:45:26 +00:00
|
|
|
import { getOctokit } from '../src/github'
|
2020-06-03 01:39:46 +00:00
|
|
|
|
|
|
|
describe('@actions/github', () => {
|
|
|
|
let proxyConnects: string[]
|
|
|
|
let proxyServer: http.Server
|
|
|
|
let first = true
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
// Start proxy server
|
2023-09-15 15:04:21 +00:00
|
|
|
proxyServer = createProxy()
|
2023-09-15 13:28:29 +00:00
|
|
|
await new Promise<void>(resolve => {
|
2020-06-03 01:39:46 +00:00
|
|
|
const port = Number(proxyUrl.split(':')[2])
|
2023-09-15 13:28:29 +00:00
|
|
|
proxyServer.listen(port, () => resolve())
|
2020-06-03 01:39:46 +00:00
|
|
|
})
|
|
|
|
proxyServer.on('connect', req => {
|
2023-09-15 13:28:29 +00:00
|
|
|
console.log('connect', req.url)
|
2022-12-14 00:28:46 +00:00
|
|
|
proxyConnects.push(req.url ?? '')
|
2020-06-03 01:39:46 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
proxyConnects = []
|
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
// Stop proxy server
|
2023-09-15 13:28:29 +00:00
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
proxyServer.once('close', () => resolve())
|
2020-06-03 01:39:46 +00:00
|
|
|
proxyServer.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
if (originalProxyUrl) {
|
|
|
|
process.env['https_proxy'] = originalProxyUrl
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('basic REST client with proxy', async () => {
|
|
|
|
const token = getToken()
|
|
|
|
if (!token) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const octokit = getOctokit(token)
|
2021-05-04 20:20:38 +00:00
|
|
|
const branch = await octokit.rest.repos.getBranch({
|
2020-06-03 01:39:46 +00:00
|
|
|
owner: 'actions',
|
|
|
|
repo: 'toolkit',
|
2020-07-21 15:33:05 +00:00
|
|
|
branch: 'main'
|
2020-06-03 01:39:46 +00:00
|
|
|
})
|
2020-07-21 15:33:05 +00:00
|
|
|
expect(branch.data.name).toBe('main')
|
2020-06-03 01:39:46 +00:00
|
|
|
expect(proxyConnects).toEqual(['api.github.com:443'])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('basic GraphQL client with proxy', async () => {
|
|
|
|
const token = getToken()
|
|
|
|
if (!token) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
process.env['https_proxy'] = proxyUrl
|
|
|
|
const octokit = getOctokit(token)
|
|
|
|
|
|
|
|
const repository = await octokit.graphql(
|
|
|
|
'{repository(owner:"actions", name:"toolkit"){name}}'
|
|
|
|
)
|
2023-09-15 13:45:26 +00:00
|
|
|
expect(repository).toEqual({ repository: { name: 'toolkit' } })
|
2020-06-03 01:39:46 +00:00
|
|
|
expect(proxyConnects).toEqual(['api.github.com:443'])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should only use default agent if one is not provided', async () => {
|
|
|
|
const token = getToken()
|
|
|
|
if (!token) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid token
|
|
|
|
const octokit = getOctokit(token, {
|
|
|
|
request: {
|
|
|
|
agent: new https.Agent()
|
|
|
|
}
|
|
|
|
})
|
2021-05-04 20:20:38 +00:00
|
|
|
const branch = await octokit.rest.repos.getBranch({
|
2020-06-03 01:39:46 +00:00
|
|
|
owner: 'actions',
|
|
|
|
repo: 'toolkit',
|
2020-07-21 15:33:05 +00:00
|
|
|
branch: 'main'
|
2020-06-03 01:39:46 +00:00
|
|
|
})
|
2020-07-21 15:33:05 +00:00
|
|
|
expect(branch.data.name).toBe('main')
|
2020-06-03 01:39:46 +00:00
|
|
|
expect(proxyConnects).toHaveLength(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
function getToken(): string {
|
|
|
|
const token = process.env['GITHUB_TOKEN'] || ''
|
|
|
|
if (!token && first) {
|
|
|
|
/* eslint-disable-next-line no-console */
|
|
|
|
console.warn(
|
|
|
|
'Skipping GitHub tests. Set $GITHUB_TOKEN to run REST client and GraphQL client tests'
|
|
|
|
)
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
})
|