-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
octokit client should follow proxy settings (#314)
- Loading branch information
1 parent
e69833e
commit ab5bd9d
Showing
18 changed files
with
473 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import * as http from 'http' | ||
import proxy from 'proxy' | ||
import {GitHub} from '../src/github' | ||
|
||
describe('@actions/github', () => { | ||
const proxyUrl = 'http://127.0.0.1:8080' | ||
const originalProxyUrl = process.env['https_proxy'] | ||
let proxyConnects: string[] | ||
let proxyServer: http.Server | ||
let first = true | ||
|
||
beforeAll(async () => { | ||
// Start proxy server | ||
proxyServer = proxy() as http.Server | ||
await new Promise(resolve => { | ||
const port = Number(proxyUrl.split(':')[2]) | ||
proxyServer.listen(port, () => resolve()) | ||
}) | ||
proxyServer.on('connect', req => { | ||
proxyConnects.push(req.url) | ||
}) | ||
}) | ||
|
||
beforeEach(() => { | ||
delete process.env['https_proxy'] | ||
proxyConnects = [] | ||
}) | ||
|
||
afterAll(async () => { | ||
// Stop proxy server | ||
await new Promise(resolve => { | ||
proxyServer.once('close', () => resolve()) | ||
proxyServer.close() | ||
}) | ||
|
||
if (originalProxyUrl) { | ||
process.env['https_proxy'] = originalProxyUrl | ||
} | ||
}) | ||
|
||
it('basic REST client', async () => { | ||
const token = getToken() | ||
if (!token) { | ||
return | ||
} | ||
|
||
const octokit = new GitHub(token) | ||
const branch = await octokit.repos.getBranch({ | ||
owner: 'actions', | ||
repo: 'toolkit', | ||
branch: 'master' | ||
}) | ||
expect(branch.data.name).toBe('master') | ||
expect(proxyConnects).toHaveLength(0) | ||
}) | ||
|
||
it('basic REST client with custom auth', async () => { | ||
const token = getToken() | ||
if (!token) { | ||
return | ||
} | ||
|
||
// Valid token | ||
let octokit = new GitHub({auth: `token ${token}`}) | ||
const branch = await octokit.repos.getBranch({ | ||
owner: 'actions', | ||
repo: 'toolkit', | ||
branch: 'master' | ||
}) | ||
expect(branch.data.name).toBe('master') | ||
expect(proxyConnects).toHaveLength(0) | ||
|
||
// Invalid token | ||
octokit = new GitHub({auth: `token asdf`}) | ||
let failed = false | ||
try { | ||
await octokit.repos.getBranch({ | ||
owner: 'actions', | ||
repo: 'toolkit', | ||
branch: 'master' | ||
}) | ||
} catch (err) { | ||
failed = true | ||
} | ||
expect(failed).toBeTruthy() | ||
}) | ||
|
||
it('basic REST client with proxy', async () => { | ||
const token = getToken() | ||
if (!token) { | ||
return | ||
} | ||
|
||
process.env['https_proxy'] = proxyUrl | ||
const octokit = new GitHub(token) | ||
const branch = await octokit.repos.getBranch({ | ||
owner: 'actions', | ||
repo: 'toolkit', | ||
branch: 'master' | ||
}) | ||
expect(branch.data.name).toBe('master') | ||
expect(proxyConnects).toEqual(['api.github.com:443']) | ||
}) | ||
|
||
it('basic GraphQL client', async () => { | ||
const token = getToken() | ||
if (!token) { | ||
return | ||
} | ||
|
||
const octokit = new GitHub(token) | ||
const repository = await octokit.graphql( | ||
'{repository(owner:"actions", name:"toolkit"){name}}' | ||
) | ||
expect(repository).toEqual({repository: {name: 'toolkit'}}) | ||
expect(proxyConnects).toHaveLength(0) | ||
}) | ||
|
||
it('basic GraphQL client with custom auth', async () => { | ||
const token = getToken() | ||
if (!token) { | ||
return | ||
} | ||
|
||
// Valid token | ||
let octokit = new GitHub(token) | ||
const repository = await octokit.graphql( | ||
'{repository(owner:"actions", name:"toolkit"){name}}' | ||
) | ||
expect(repository).toEqual({repository: {name: 'toolkit'}}) | ||
expect(proxyConnects).toHaveLength(0) | ||
|
||
// Invalid token | ||
octokit = new GitHub({auth: `token asdf`}) | ||
let failed = false | ||
try { | ||
await octokit.graphql( | ||
'{repository(owner:"actions", name:"toolkit"){name}}' | ||
) | ||
} catch (err) { | ||
failed = true | ||
} | ||
expect(failed).toBeTruthy() | ||
}) | ||
|
||
it('basic GraphQL client with proxy', async () => { | ||
const token = getToken() | ||
if (!token) { | ||
return | ||
} | ||
|
||
process.env['https_proxy'] = proxyUrl | ||
const octokit = new GitHub(token) | ||
const repository = await octokit.graphql( | ||
'{repository(owner:"actions", name:"toolkit"){name}}' | ||
) | ||
expect(repository).toEqual({repository: {name: 'toolkit'}}) | ||
expect(proxyConnects).toEqual(['api.github.com:443']) | ||
}) | ||
|
||
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 | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare module 'proxy' { | ||
import * as http from 'http' | ||
function internal(): http.Server | ||
export = internal | ||
} |
Oops, something went wrong.