-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(request): proxy-agent breaks Studio (#580)
* fix(request): proxy-agent breaks Studio * feat: do not load http-agent at all if not in cli * test: support env in test-harness
- Loading branch information
Showing
7 changed files
with
106 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,82 @@ | ||
import * as http from 'http'; | ||
import * as url from 'url'; | ||
import { DEFAULT_REQUEST_OPTIONS } from '../request'; | ||
import { httpAndFileResolver } from '../resolvers/http-and-file'; | ||
import { Spectral } from '../spectral'; | ||
const ProxyAgent = require('proxy-agent'); | ||
|
||
const PORT = 4001; | ||
|
||
describe('request', () => { | ||
const oldProxyEnv = process.env.PROXY; | ||
describe('when agent is set', () => { | ||
let server: http.Server; | ||
|
||
beforeEach(() => { | ||
process.env = { PROXY: 'http://localhost:3000/' }; | ||
}); | ||
beforeAll(() => { | ||
// nock cannot mock proxied requests | ||
server = http | ||
.createServer((req, res) => { | ||
const { pathname } = url.parse(String(req.url)); | ||
if (pathname === '/custom-ruleset') { | ||
res.writeHead(403); | ||
} else if (pathname === '/ok.json') { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
res.write( | ||
JSON.stringify({ | ||
info: { | ||
title: '', | ||
description: 'Foo', | ||
}, | ||
}), | ||
); | ||
} else { | ||
res.writeHead(404); | ||
} | ||
|
||
afterEach(() => { | ||
process.env.proxy = oldProxyEnv; | ||
}); | ||
res.end(); | ||
}) | ||
.listen(PORT, '0.0.0.0'); | ||
}); | ||
|
||
afterAll(() => { | ||
server.close(); | ||
}); | ||
|
||
describe('loading a ruleset', () => { | ||
it('proxies the request', async () => { | ||
const s = new Spectral(); | ||
beforeEach(() => { | ||
DEFAULT_REQUEST_OPTIONS.agent = new ProxyAgent(`http://localhost:${PORT}`); | ||
}); | ||
|
||
afterEach(() => { | ||
delete DEFAULT_REQUEST_OPTIONS.agent; | ||
}); | ||
|
||
describe('loading a ruleset', () => { | ||
it('proxies the request', () => { | ||
const s = new Spectral(); | ||
|
||
try { | ||
await s.loadRuleset('http://localhost:4000/custom-ruleset'); | ||
} catch (e) { | ||
expect(e.message).toBe( | ||
'Could not parse http://localhost:4000/custom-ruleset: request to http://localhost:4000/custom-ruleset failed, reason: connect ECONNREFUSED 127.0.0.1:3000', | ||
return expect(s.loadRuleset('http://localhost:4000/custom-ruleset')).rejects.toHaveProperty( | ||
'message', | ||
'Could not parse http://localhost:4000/custom-ruleset: Forbidden', | ||
); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('loading a $ref', () => { | ||
it('proxies the request', async () => { | ||
const spec = { | ||
openapi: '3.0.2', | ||
paths: { | ||
'/pets': { | ||
get: { | ||
responses: { | ||
'200': { | ||
description: 'abc', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
$ref: 'http://localhost:8089/ok.json', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
describe('loading a $ref', () => { | ||
it('proxies the request', () => { | ||
const doc = { | ||
info: { | ||
$ref: 'http://localhost:8089/ok.json#/info', | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
const s = new Spectral({ resolver: httpAndFileResolver }); | ||
const results = await s.run(spec); | ||
const s = new Spectral({ resolver: httpAndFileResolver }); | ||
|
||
expect(results[0].message).toBe( | ||
'FetchError: request to http://localhost:8089/ok.json failed, reason: connect ECONNREFUSED 127.0.0.1:3000', | ||
); | ||
return expect(s.runWithResolved(doc)).resolves.toHaveProperty('resolved', { | ||
info: { | ||
title: '', | ||
description: 'Foo', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import { RequestInit } from 'node-fetch'; | ||
import * as ProxyAgent from 'proxy-agent'; | ||
import fetch, { RequestInit } from 'node-fetch'; | ||
|
||
const fetch = require('node-fetch'); | ||
export const DEFAULT_REQUEST_OPTIONS: RequestInit = {}; | ||
|
||
export default (uri: string, opts: RequestInit = {}) => { | ||
const options = | ||
typeof process === 'object' && process.env.PROXY ? { ...opts, agent: new ProxyAgent(process.env.PROXY) } : opts; | ||
|
||
return fetch(uri, options); | ||
export default async (uri: string, opts: RequestInit = {}) => { | ||
return fetch(uri, { ...opts, ...DEFAULT_REQUEST_OPTIONS }); | ||
}; |
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,15 @@ | ||
====test==== | ||
Requests for $refs are proxied when PROXY env variable is set | ||
====document==== | ||
foo: | ||
$ref: http://localhost:3002/foo.json#/ref | ||
====env==== | ||
PROXY=http://localhost:3001 | ||
====command==== | ||
lint {document} | ||
====stdout==== | ||
|
||
{document} | ||
2:9 error invalid-ref FetchError: request to http://localhost:3002/foo.json failed, reason: connect ECONNREFUSED 127.0.0.1:3001 | ||
|
||
✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints) |