reduce duplication in testclient implementations

pull/148/head
Tim Vahlbrock 2 years 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 ( assert (
get_nested_key( get_nested_key(
data, 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 is not None
) )

@ -2,7 +2,7 @@
import json import json
import requests # type: ignore from testclient import testclient
# Sample data # Sample data
data = { data = {
@ -44,20 +44,8 @@ data = {
}, },
} }
testclient(
url = "http://localhost:8082" "application/json",
headers = {"Content-Type": "application/json"} lambda: json.dumps(data),
response = requests.post( lambda content: json.loads(content),
url,
data=json.dumps(data),
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 = json.loads(response.content)
print(data)

@ -1,25 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import msgpack import msgpack
import requests # type: ignore from testclient import testclient
# Sample MessagePack data # Sample MessagePack data
msgpack_data = {"field1": "value1", "field2": "value2"} msgpack_data = {"field1": "value1", "field2": "value2"}
testclient(
url = "http://localhost:8082" "application/msgpack",
headers = {"Content-Type": "application/msgpack"} lambda: msgpack.packb(msgpack_data),
response = requests.post( lambda content: msgpack.unpackb(content),
url,
data=msgpack.packb(msgpack_data, use_bin_type=True),
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 = 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