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: if response_json is not None:
resp_data_to_set = { resp_data_to_set = {
"description": req.get_response_reason(), "description": req.get_response_reason(),
"headers": None,
"content": { "content": {
"application/json": { "application/json": {
"schema": swagger_util.value_to_schema(response_json) "schema": swagger_util.value_to_schema(response_json)

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

@ -1,17 +1,30 @@
import os # -*- coding: utf-8 -*-
from .mitmproxy2swagger import main import sys
import tempfile import tempfile
import ruamel.yaml as ruamel
from typing import Any, List 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( def mitmproxy2swagger_e2e_test(
input_file: str, url_prefix: str, extra_args: List[str] | None = None input_file: str, url_prefix: str, extra_args: List[str] | None = None
) -> Any: ) -> Any:
""" """Runs mitmproxy2swagger on the given input file twice, and returns the
Runs mitmproxy2swagger on the given input file twice, detected endpoints."""
and returns the detected endpoints.
"""
yaml_tmp_path = tempfile.mktemp(suffix=".yaml", prefix="sklep.lisek.") yaml_tmp_path = tempfile.mktemp(suffix=".yaml", prefix="sklep.lisek.")
main( main(
[ [
@ -62,7 +75,6 @@ def mitmproxy2swagger_e2e_test(
def test_mitmproxy2swagger_generates_swagger_from_har(): def test_mitmproxy2swagger_generates_swagger_from_har():
data = mitmproxy2swagger_e2e_test( data = mitmproxy2swagger_e2e_test(
"testdata/sklep.lisek.app.har", "https://sklep.lisek.app/" "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(): def test_mitmproxy2swagger_generates_swagger_from_mitmproxy_flow_file():
data = mitmproxy2swagger_e2e_test("testdata/test_flows", "https://httpbin.org/", [ data = mitmproxy2swagger_e2e_test(
"testdata/test_flows",
"https://httpbin.org/",
[
"--format", "--format",
"flow", "flow",
"--headers", ],
]) )
assert data is not None assert data is not None
assert "paths" in data assert "paths" in data
yaml = ruamel.YAML()
yaml.dump(data, sys.stdout)
assert len(data["paths"]) == 3 # 4 paths in the test file 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