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

Support for simple JSON extraction from the response body #106

Merged
merged 1 commit into from Nov 15, 2015
Merged

Support for simple JSON extraction from the response body #106

merged 1 commit into from Nov 15, 2015

Conversation

tkeffer
Copy link
Contributor

@tkeffer tkeffer commented Nov 7, 2015

I have an API that returns a JSON array in the response body. I want to test that it is, in fact, an array. Unfortunately, that doesn't seem to be possible. It's possible to test an element of the array, but not the whole array.

My test looks like this:

- test:
    - name: "Get all created streams and validate them"
    - url: {'template': "/api/v1/streams"}
    - expected_status: [200]
    - validators:
        - compare: {jsonpath_mini: "", comparator: 'type', expected: 'array'}

This fails because the jsonpath_mini spec causes the validator to actually test (None, array). The reason is the logic in function query_dictionary():

    def query_dictionary(query, dictionary, delimiter='.'):
        try:
            for x in query.strip(delimiter).split(delimiter):
                try:
                    x = int(x)
                    dictionary = dictionary[x]
                except ValueError:
                    dictionary = dictionary[x]
        except:
            return None
        return dictionary

If a blank string is used as a query, then the expression query.strip(delimiter).split(delimiter) returns an array one element long, containing a blank string. So, dictionary=dictionary[x] evaluates to None.

The suggested fix tests for an empty string and, if it finds one, simply returns the dictionary which, in this case, will be the full array. Hence, both subscripting of the array, as well as the full array, can be supported.

This is a general fix. A jsonpath_mini spec of an empty string simply causes the response body to be parsed as JSON and returned. So, you could test against anything.

dictionary element. It was not possible to simply convert the raw body
to JSON and test against that. This addition allows one to do that.
@svanoort-jenkins
Copy link
Collaborator

Can one of the admins verify this patch?

@svanoort
Copy link
Owner

svanoort commented Nov 8, 2015

Hi! Thank you for your contribution -- it looks like supports a valuable use case (and I'm glad to see the new type comparator is already proving helpful). There are a couple more things we need before it is fully ready to merge (per https://github.com/svanoort/pyresttest#feedback):

Please could you add these items, and if those changes are solid, we will be ready to merge!
Thanks, Sam

@svanoort svanoort added this to the 1.7.0 - Python 3 + Parsing/Configuration Internals milestone Nov 8, 2015
@svanoort
Copy link
Owner

svanoort commented Nov 8, 2015

Jenkins test build

@svanoort
Copy link
Owner

@tkeffer I'm getting more community interest in this feature, so I wanted to see if you were going to be able to add tests / documentation for this? Otherwise, I'm going to go ahead and add them + merge in the near future, so we can get this out for people to use.

Thanks!
Sam

@tkeffer
Copy link
Contributor Author

tkeffer commented Nov 12, 2015

Hi, Sam
Unfortunately, I'm using another testing strategy, so I won't be able to help you.
Apologies.
-tk

@svanoort svanoort merged commit 42dbe88 into svanoort:master Nov 15, 2015
@svanoort
Copy link
Owner

@tkeffer No worries, tests and docs are added, this is now part of the master. Cheers!

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