-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TT-347] Proxy non-draft endpoints to get real response #335
Conversation
91875fe
to
1949cb2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one. Unfortunately it looks like the mock server doesn't have test coverage yet (my bad). Would you mind adding some? We need to ensure that it correctly mocks draft endpoints and proxies the others unless there is no proxy URL.
lib/src/mockserver/proxy.ts
Outdated
pathPrefix: string; | ||
proxyBaseUrl: string; | ||
}) { | ||
const [protocol] = proxyBaseUrl.split("://"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to fail with a helpful message if we forgot to specify the protocol? Or fallback to http:// maybe?
lib/src/mockserver/server.ts
Outdated
|
||
// non-draft end points get real response | ||
if (shouldProxy && proxyBaseUrl) { | ||
proxyRequest({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For conciseness, consider:
proxyRequest({ | |
return proxyRequest({ |
cli/src/commands/mock.ts
Outdated
@@ -23,6 +23,10 @@ export default class Mock extends Command { | |||
|
|||
static flags = { | |||
help: flags.help({ char: "h" }), | |||
proxyBaseUrl: flags.string({ | |||
description: | |||
"If set, the server would act as a proxy and fetch data from the given remote server instead of mocking it" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"If set, the server would act as a proxy and fetch data from the given remote server instead of mocking it" | |
"If set, the server will act as a proxy and fetch data from the given remote server for all non-draft endpoints instead of mocking them" |
-h, --help show CLI help | ||
-p, --port=port (required) [default: 3010] Port on which to run the mock server | ||
--pathPrefix=pathPrefix Prefix to prepend to each endpoint path | ||
--proxyBaseUrl=proxyBaseUrl If set, the server would act as a proxy and fetch data from the given remote server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, should we only update the README.md once we release a new version?
daaf26a
to
57f7051
Compare
cli/src/common/infer-protocol.ts
Outdated
|
||
if (protocol !== "http" && protocol !== "https") { | ||
throw new Error( | ||
'Err - could not infer protocol from proxy base url, should be either "http" or "https".' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think Err -
is necessary since it should be fairly clear in the stack trace that it's an error already?
'Err - could not infer protocol from proxy base url, should be either "http" or "https".' | |
'Could not infer protocol from proxy base url, should be either "http" or "https".' |
d4c9f85
to
9f9c44b
Compare
cli/src/common/infer-proxy-config.ts
Outdated
@@ -0,0 +1,20 @@ | |||
import { ProxyConfig } from '../../../lib/src/mockserver/server'; | |||
|
|||
export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | void { | |
export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | null { |
cli/src/common/infer-proxy-config.ts
Outdated
|
||
export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | void { | ||
if (!proxyBaseUrl) { | ||
return undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think null
better represents the concept of "absence of a value" rather than undefined
. Thoughts?
return undefined; | |
return null; |
cli/src/common/infer-proxy-config.ts
Outdated
|
||
if (protocol !== "http" && protocol !== "https") { | ||
throw new Error( | ||
'Err - could not infer protocol from proxy base url, should be either "http" or "https".' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address #335 (comment)
d495881
to
4bb3223
Compare
66e023c
to
02567ed
Compare
This introduces a new module used within the main server middleware to proxy requests that expect real response as opposed to mock ones. The proxy will be leveraged when an endpoint is not in a draft state.
To specify the url of the server the proxy needs to hit, users can set a
--proxyBaseUrl
flag when starting the mock server.