Commit 6415e404 authored by Daniel Kastl's avatar Daniel Kastl Committed by Ken Zangelin
Browse files

fix(httpctrl): read attrs from every entity of an array response

When a GET request has attrs=, the mock checks that every requested
attribute is a key in the stub's response body. For an array body it used
an empty list of keys, so the check always failed. Query responses are
always arrays, and only queries use attrs. So these stubs never matched,
not even a request with exactly the stub's URL. D011_01_03_inc,
D011_01_exc and D011_02_exc_01 fail this way.

Use the keys of all entities in an array body. An object body works as
before. An empty array still matches nothing.
parent 94cf3edd
Loading
Loading
Loading
Loading
+4 −3
Changes for libraries/robotframework-httpctrl/src/HttpCtrl/http_stub.py: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -96,11 +96,12 @@ class HttpStubContainer(metaclass=Singleton):
                    if criteria_url != stub_url_components[0].rstrip("/"):
                        return False

                    # extraction of attributes from the response body (only works when response body is a dictionary)
                    # return an empty array if response body is an array
                    # Collect the attribute names from the response body. If the body
                    # is an array (a query response always is), use the names of all
                    # entities in it.
                    parsed_body = json.loads(stub.response.get_body())
                    if isinstance(parsed_body, list):
                        attributes = []
                        attributes = [key.lower() for item in parsed_body if isinstance(item, dict) for key in item]
                    else:
                        attributes = [key.lower() for key in parsed_body]
                    # criteria parameters should be separated to check if attributes are into the response body