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

add --match-server-url flag to improve openapi server url matching #10

Merged
merged 2 commits into from
Aug 3, 2022

Conversation

dachrillz
Copy link
Contributor

It will probably be useful to have the full URL in the HAR type. It's
defined as Node's builtin URL class. We will se if this bites us in the
future!

It will probably be useful to have the full URL in the HAR type. It's
defined as Node's builtin URL class. We will se if this bites us in the
future!
@dachrillz dachrillz marked this pull request as draft August 2, 2022 09:03
This flag allows one to specify how strict the open api server matching
should be. None means that it should ignore the host, protocol etc and
only match paths. This is useful if e.g. your tests target localhost but
you specify a proper url in the open api. Full means that the request
has to fully match what is stated in the specification.

The logic is somewhat convoluted and can be improved. However, this
covers the cases that I tested. We can improve as we further understand
how this tool will be used
@dachrillz dachrillz marked this pull request as ready for review August 2, 2022 10:41
@dachrillz
Copy link
Contributor Author

This PR add the --match-server-url flag.

For now it has two options: "none", and "full". (It defaults to none)

If you set it to none then it ignores protocol, host etc. And only matches on subpaths. If you set it to full, everything has to match.

None is useful if you e.g. have tests that target localhost, but your specification only has proper urls.

The logic is probably somewhat convoluted since I'm not entirerly sure how this will be used. However a somewhat extensive test suite for the url matching function was written that covers the cases I'm currently interested in. As we understand the tool better we can extend this test suite and then refactor the url matching function!

@dachrillz dachrillz changed the title include full URL in HAR type add --match-server-url flag to improve openapi server url matching Aug 2, 2022
@dachrillz dachrillz mentioned this pull request Aug 2, 2022
Copy link
Contributor

@bittrance bittrance left a comment

Choose a reason for hiding this comment

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

Some URL corner case nitpicking below, but generally speaking looks good. However, your formatter seems to be more opinionated than prettier and has performed some whitespace changes. It would be good if you could tell your formatter to behave or include config to make everyone else's formatter to behave.

Also, it is clear that we will at some point introduce an option --spec-source-url to be able to implement the spec:s strategy for making relative URLs absolute by prefixing the URL from whence the OpenAPI spec was supposedly served.

src/index.ts Show resolved Hide resolved
src/rule/findPath.test.ts Show resolved Hide resolved
@@ -1,4 +1,4 @@
import { findPath } from ".";
import { findPathNodeFromPath } from ".";
Copy link
Contributor

Choose a reason for hiding this comment

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

You may want to rename the file as well?

src/rule/index.test.ts Show resolved Hide resolved
src/rule/index.ts Show resolved Hide resolved
// If the relative server ends with a trailing slash we remove to simplify logic
if (server.endsWith("/")) {
serverToMatch = serverToMatch.slice(0, serverToMatch.length - 2);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a violation of RFC3986. /foo/bar and /foo//bar are not the same URL. The OpenAPI spec explicitly states "he path is appended (no relative URL resolution) to the expanded URL from the Server Object's url field in order to construct the full URL." If we want to address this case, I think it would be better to warn on (or refuse) a spec with a url ending in a "non-path-abempty" path.

serverToMatch = serverToMatch.slice(0, serverToMatch.length - 2);
}

if (pathToMatch.indexOf(serverToMatch) === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this pathToMatch.startsWith(serverToMatch)? I think that would be easier to read.

}

if (pathToMatch.indexOf(serverToMatch) === 0) {
return pathToMatch.slice(serverToMatch.length, pathToMatch.length);
Copy link
Contributor

Choose a reason for hiding this comment

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

"foo".slice(1, 1) will return "". There seems to be no test case where this happen. Can it happen?

);
};

export const removeServer = (
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is now two different non-overlapping execution paths depending on whether the server url is absolute or not. I think it would be better if the big if statement was made into a separate function.

@@ -7,17 +7,20 @@ import { Result } from "../rule/model";

export const run = async (
openApiPath: string,
requestResponsePaths: string[]
requestResponsePaths: string[],
match_server_url: string
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 we can save ourselves some work by introducing an options object from the start.

@dachrillz dachrillz merged commit 189660a into main Aug 3, 2022
@@ -12,6 +12,7 @@ export type response = {

export type request = {
method: "post" | "get" | "put" | "patch" | "delete";
url: URL;
path: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems odd that we keep the path?

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.

2 participants