reduce duplication in testclient implementations

pull/148/head
Tim Vahlbrock 7 months ago
parent e2537a738d
commit b1ce4cf5a5
No known key found for this signature in database

@ -88,7 +88,7 @@ def test_mitmproxy2swagger_generates_swagger_from_mitmproxy_flow_file_with_gener
assert (
get_nested_key(
data,
"paths./.post.responses.200.content.application/json.schema.properties.numeric.additionalProperties",
"paths./.post.responses.200.content.application/json.schemar.properties.numeric.additionalProperties",
)
is not None
)

@ -2,7 +2,7 @@
import json
import requests # type: ignore
from testclient import testclient
# Sample data
data = {
@ -44,20 +44,8 @@ data = {
},
}
url = "http://localhost:8082"
headers = {"Content-Type": "application/json"}
response = requests.post(
url,
data=json.dumps(data),
headers=headers,
proxies={"http": "http://localhost:8080", "https": "http://localhost:8080"},
testclient(
"application/json",
lambda: json.dumps(data),
lambda content: json.loads(content),
)
# Print the response
print(response.status_code)
print(response.headers)
# convert the response data from MessagePack to JSON
data = json.loads(response.content)
print(data)

@ -1,25 +1,13 @@
# -*- coding: utf-8 -*-
import msgpack
import requests # type: ignore
from testclient import testclient
# Sample MessagePack data
msgpack_data = {"field1": "value1", "field2": "value2"}
url = "http://localhost:8082"
headers = {"Content-Type": "application/msgpack"}
response = requests.post(
url,
data=msgpack.packb(msgpack_data, use_bin_type=True),
headers=headers,
proxies={"http": "http://localhost:8080", "https": "http://localhost:8080"},
testclient(
"application/msgpack",
lambda: msgpack.packb(msgpack_data),
lambda content: msgpack.unpackb(content),
)
# Print the response
print(response.status_code)
print(response.headers)
# convert the response data from MessagePack to JSON
data = msgpack.unpackb(response.content, raw=False)
print(data)

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
from typing import Any, Callable
import requests # type: ignore
def testclient(
contentType: str,
getData: Callable[[], Any],
decodeData: Callable[[Any], Any],
) -> None:
url = "http://localhost:8082"
headers = {"Content-Type": contentType}
response = requests.post(
url,
data=getData(),
headers=headers,
proxies={"http": "http://localhost:8080", "https": "http://localhost:8080"},
)
# Print the response
print(response.status_code)
print(response.headers)
# convert the response data from MessagePack to JSON
data = decodeData(response.content)
print(data)
Loading…
Cancel
Save