Skip to content
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

Merged
merged 12 commits into from
Jul 19, 2019

Conversation

medric
Copy link
Contributor

@medric medric commented Jul 15, 2019

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.

@medric medric requested review from fwouts and lfportal July 15, 2019 07:21
@medric medric force-pushed the TT-347-proxy-non-draft-endpoints branch from 91875fe to 1949cb2 Compare July 15, 2019 07:23
Copy link
Contributor

@fwouts fwouts left a 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.

pathPrefix: string;
proxyBaseUrl: string;
}) {
const [protocol] = proxyBaseUrl.split("://");
Copy link
Contributor

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?


// non-draft end points get real response
if (shouldProxy && proxyBaseUrl) {
proxyRequest({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For conciseness, consider:

Suggested change
proxyRequest({
return proxyRequest({

@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"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
Copy link
Contributor

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?

@medric medric force-pushed the TT-347-proxy-non-draft-endpoints branch from daaf26a to 57f7051 Compare July 18, 2019 00:27

if (protocol !== "http" && protocol !== "https") {
throw new Error(
'Err - could not infer protocol from proxy base url, should be either "http" or "https".'
Copy link
Contributor

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?

Suggested change
'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".'

lib/src/mockserver/server.spec.ts Show resolved Hide resolved
@medric medric force-pushed the TT-347-proxy-non-draft-endpoints branch from d4c9f85 to 9f9c44b Compare July 19, 2019 03:23
@@ -0,0 +1,20 @@
import { ProxyConfig } from '../../../lib/src/mockserver/server';

export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | void {
export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | null {


export default function inferProxyConfig(proxyBaseUrl: string): ProxyConfig | void {
if (!proxyBaseUrl) {
return undefined;
Copy link
Contributor

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?

Suggested change
return undefined;
return null;


if (protocol !== "http" && protocol !== "https") {
throw new Error(
'Err - could not infer protocol from proxy base url, should be either "http" or "https".'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address #335 (comment)

@medric medric force-pushed the TT-347-proxy-non-draft-endpoints branch from d495881 to 4bb3223 Compare July 19, 2019 04:08
@medric medric force-pushed the TT-347-proxy-non-draft-endpoints branch from 66e023c to 02567ed Compare July 19, 2019 04:15
@medric medric merged commit d18d752 into master Jul 19, 2019
@medric medric deleted the TT-347-proxy-non-draft-endpoints branch July 19, 2019 04:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants