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

Asserts with unmatched XPath nodes #503

Closed
jods4 opened this issue Jul 20, 2023 · 4 comments
Closed

Asserts with unmatched XPath nodes #503

jods4 opened this issue Jul 20, 2023 · 4 comments

Comments

@jods4
Copy link

jods4 commented Jul 20, 2023

When no node matches an XPath assert, the (string) expected value is parsed as JSON and this crashes httpyac instead of cleaning failing the assert.

I looked at the code and I think I know what happens.

  1. provideAssertValueXPath uses xpath lib to select the matching nodes. If no node in response matches, this would be an empty array.

  2. provideAssertValueXPath maps the array to strings, but the final provided value remains an empty array.
    https://github.com/AnWeber/httpyac/blob/main/src/plugins/xml/provideAssertValueXPath.ts#L40-L44
    I suggest this is where the fix might be: add a case for empty array, and return undefined instead?

  3. parseAssertLine attempts an auto-conversion of expected value to actual value type.
    https://github.com/AnWeber/httpyac/blob/main/src/plugins/assert/assertHttpRegionParser.ts#L57
    When XPath matched a node, the target value is a string and there's no conversion: it works great.
    But when there's no match, target value is an empty array, and the auto-conversion attempts to parse the expected value as JSON.
    https://github.com/AnWeber/httpyac/blob/main/src/plugins/assert/assertHttpRegionParser.ts#L98-L100

@jods4
Copy link
Author

jods4 commented Jul 20, 2023

Maybe another possible fix, that might be more general:

I guess the fundamental issue is that trying to convert an expected string value into the current target value is inherently fragile, even when matching JSON or other sources. If the target value is not a string as expected that process is likely to crash.

So a better fix could be to wrap that JSON parse in a try..catch and cleanly fail the assert when the target and expected value types are incompatible.

(might still be nice to convert a non-matching xpath selector into undefined).

@AnWeber
Copy link
Owner

AnWeber commented Jul 20, 2023

@jods4 Yeah, what can I say. I agree with you on all points. You can tell that I don't use the XPath myself. For objects, it currently works rather mediocre. My asserts are either very simple or I use the javascript notation right away.

@jods4
Copy link
Author

jods4 commented Jul 21, 2023

Thank you!

@AnWeber
Copy link
Owner

AnWeber commented Jul 23, 2023

update is released with v6.6.1

@AnWeber AnWeber closed this as completed Jul 23, 2023
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

No branches or pull requests

2 participants