Commit 94cf3edd authored by Daniel Kastl's avatar Daniel Kastl Committed by Ken Zangelin
Browse files

fix(httpctrl): decode the query before matching GET stub parameters

For a GET request with a query, the mock looks for each part of the
stub's query (for example urn:ngsi-ld:Vehicle:1) as text in the request
query. A broker may send the id percent-encoded
(id=urn%3Angsi-ld%3AVehicle%3A1). This is the same query, but the text is
different, so no stub matches and the mock does not answer.
D011_03_inc_01 and D011_04_inc_01 fail this way.

Decode the request query once, and use it for both text checks. The
matching rules do not change otherwise.
parent 0cb6b158
Loading
Loading
Loading
Loading
+7 −3
Changes for libraries/robotframework-httpctrl/src/HttpCtrl/http_stub.py: 7 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ Copyright: The 3-Clause BSD License

from threading import Lock
import json
from urllib.parse import parse_qs
from urllib.parse import parse_qs, unquote
from HttpCtrl.utils.singleton import Singleton


@@ -82,6 +82,10 @@ class HttpStubContainer(metaclass=Singleton):
            
            # distribution brokers may add optional parameters to the url, so we should check if the url is valid even though it is not the same as the stub's url
            if criteria.method == "GET":
                # Decode the query first. A broker may percent-encode values
                # (id=urn%3Angsi-ld%3A...). This is the same query as the stub's
                # id=urn:ngsi-ld:..., but the text checks below would not find it.
                criteria_query = unquote(criteria_url_components[1])
                if '?' in stub.criteria.url:
                    stub_url_components = stub.criteria.url.split('?')

@@ -112,7 +116,7 @@ class HttpStubContainer(metaclass=Singleton):
                    # Flatten the list to get a list of key-value elements
                    stub_params_components = [element for param in stub_params for element in param.split("=")]
                    for param in stub_params_components:
                        if param not in criteria_url_components[1]:
                        if param not in criteria_query:
                            return False
                    return True
                
@@ -139,7 +143,7 @@ class HttpStubContainer(metaclass=Singleton):

                    id = stub_url[-1].split(":")
                    for elements in id:
                        if elements not in criteria_url_components[1]:
                        if elements not in criteria_query:
                            return False
                    return True
                else: