Skip to content

Commit

Permalink
Wait for response: add request data
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorpela committed Oct 21, 2020
1 parent 95d1ec5 commit b13b16e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
24 changes: 19 additions & 5 deletions Browser/keywords/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ def _get_headers(body: str, headers: Dict):


def _format_response(response: Dict):
headers = json.loads(response["headers"])
response["headers"] = headers
_jsonize_content(response, "body")
if "request" in response:
request = response["request"]
_jsonize_content(request, "postData")
logger.info(response)
return response

def _jsonize_content(data, bodykey):
headers = json.loads(data["headers"])
data["headers"] = headers
if "content-type" in headers and "application/json" in headers["content-type"]:
try:
response["body"] = json.loads(response["body"])
data[bodykey] = json.loads(data[bodykey])
except json.decoder.JSONDecodeError:
pass
logger.info(response)
return response


class Network(LibraryComponent):
Expand Down Expand Up @@ -136,6 +142,14 @@ def wait_for_response(self, matcher: str = "", timeout: str = "") -> Any:
``timeout`` <str> Timeout in milliseconds. Uses default timeout of 10 seconds if not set.
The response is a Python dictionary with following attributes:
- ``status`` <int> The status code of the response.
- ``statusText`` <str> Status text corresponding to ``status``, e.g OK or INTERNAL SERVER ERROR.
- ``body`` <dict> | <str> The response body. If the body can be parsed as a JSON obejct,
it will be returned as Python dictionary, otherwise it is returned as a string.
- ``headers`` <dict> A dictionary containing all response headers.
- ``ok`` <bool> Whether the request was successfull, i.e. the ``status`` is range 200-299.
- ``request`` <dict> containing ``method`` <str>, ``headers`` <dict> and ``postData`` <dict> | <str>
"""
return self._wait_for_http("Response", matcher, timeout)

Expand Down
4 changes: 4 additions & 0 deletions atest/test/05_JS_Tests/http_with_waiting.robot
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GET with waiting json response
Should be equal ${content}[body] ${response}[body]
Should be equal ${content}[status] ${response}[status]
Should be equal ${content}[url] ${response}[url]
Should be equal ${content}[request][method] GET

POST with waiting json response
${promise}= Promise To Wait For Response matcher=/api/post timeout=3s
Expand All @@ -18,6 +19,8 @@ POST with waiting json response
Should be equal ${content}[body] ${response}[body]
Should be equal ${content}[status] ${response}[status]
Should be equal ${content}[url] ${response}[url]
Should be equal ${content}[request][method] POST
Should be equal ${content}[request][postData][name] George

GET with text response
${promise}= Promise To Wait For Response matcher=/api/get/text timeout=3s
Expand All @@ -26,3 +29,4 @@ GET with text response
Should be equal ${content}[body] ${response}[body]
Should be equal ${content}[status] ${response}[status]
Should be equal ${content}[url] ${response}[url]
Should be equal ${content}[request][method] GET
5 changes: 5 additions & 0 deletions node/playwright-wrapper/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export async function waitForResponse(
statusText: data.statusText(),
url: data.url(),
ok: data.ok(),
request: {
headers: JSON.stringify(data.request().headers()),
method: data.request().method(),
postData: data.request().postData(),
},
}),
'',
),
Expand Down

0 comments on commit b13b16e

Please sign in to comment.