Add more advanced tests

pull/31/head
alufers 2 years ago
parent 36e545694e
commit 3ce956a965

@ -280,7 +280,6 @@ def main(override_args: Sequence[str] | None = None):
if response_json is not None:
resp_data_to_set = {
"description": req.get_response_reason(),
"headers": None,
"content": {
"application/json": {
"schema": swagger_util.value_to_schema(response_json)

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import urllib
from typing import Any, List
VERBS = [
"add",
@ -79,24 +80,22 @@ def url_to_params(url, path_template):
return params
# when given an url and its path template, generates the parameters section of the request
def request_to_headers(headers):
header = []
def request_to_headers(headers: dict[str, List[Any]], add_example: bool = False):
"""When given an url and its path template, generates the parameters
section of the request."""
params = []
if headers:
for key in headers:
header.append(
{
"name": key,
"value": headers[key][0],
"default": headers[key][0],
"in": "header",
"required": True,
"schema": {
"type": "number" if headers[key][0].isdigit() else "string"
},
}
)
return header
h = {
"name": key,
"in": "header",
"required": False,
"schema": {"type": "number" if headers[key][0].isdigit() else "string"},
}
if add_example:
h["example"] = headers[key][0]
params.append(h)
return params
def response_to_headers(headers):

@ -1,17 +1,30 @@
import os
from .mitmproxy2swagger import main
# -*- coding: utf-8 -*-
import sys
import tempfile
import ruamel.yaml as ruamel
from typing import Any, List
import ruamel.yaml as ruamel
from .mitmproxy2swagger import main
def get_nested_key(obj: Any, path: str) -> Any:
"""Gets a nested key from a dict."""
keys = path.split(".")
for key in keys:
if not isinstance(obj, dict):
return None
if key not in obj:
return None
obj = obj[key]
return obj
def mitmproxy2swagger_e2e_test(
input_file: str, url_prefix: str, extra_args: List[str] | None = None
) -> Any:
"""
Runs mitmproxy2swagger on the given input file twice,
and returns the detected endpoints.
"""
"""Runs mitmproxy2swagger on the given input file twice, and returns the
detected endpoints."""
yaml_tmp_path = tempfile.mktemp(suffix=".yaml", prefix="sklep.lisek.")
main(
[
@ -62,7 +75,6 @@ def mitmproxy2swagger_e2e_test(
def test_mitmproxy2swagger_generates_swagger_from_har():
data = mitmproxy2swagger_e2e_test(
"testdata/sklep.lisek.app.har", "https://sklep.lisek.app/"
)
@ -77,13 +89,51 @@ def test_mitmproxy2swagger_generates_swagger_from_har():
def test_mitmproxy2swagger_generates_swagger_from_mitmproxy_flow_file():
data = mitmproxy2swagger_e2e_test("testdata/test_flows", "https://httpbin.org/", [
"--format",
"flow",
"--headers",
])
data = mitmproxy2swagger_e2e_test(
"testdata/test_flows",
"https://httpbin.org/",
[
"--format",
"flow",
],
)
assert data is not None
assert "paths" in data
yaml = ruamel.YAML()
yaml.dump(data, sys.stdout)
assert len(data["paths"]) == 3 # 4 paths in the test file
assert get_nested_key(data, "paths./get.get.responses.200.content") is not None
def test_mitmproxy2swagger_generates_swagger_from_mitmproxy_flow_file_with_form_data():
data = mitmproxy2swagger_e2e_test(
"testdata/form_data_flows",
"https://httpbin.org/",
[
"--format",
"flow",
],
)
assert data is not None
assert (
get_nested_key(
data,
"paths./post.post.requestBody.content.application/x-www-form-urlencoded.schema",
)
is not None
)
def test_mitmproxy2swagger_generates_headers_for_flow_files():
data = mitmproxy2swagger_e2e_test(
"testdata/form_data_flows",
"https://httpbin.org/",
[
"--format",
"flow",
"--headers",
],
)
assert data is not None
assert get_nested_key(data, "paths./post.post.responses.200.headers.content-type") is not None

@ -0,0 +1,8 @@
#!/bin/bash
python3.9 -m venv env
source env/bin/activate
pip install mitmproxy mitmproxy2swagger
mitmproxy2swagger -i flows -o specs.yml -p https://api.ah.nl
cat specs.yml
Loading…
Cancel
Save