"""Tests para build_hoppscotch_collection.""" import json from build_hoppscotch_collection import build_hoppscotch_collection def test_golden_get_simple(): calls = [ { "method": "GET", "url": "https://api.example.com/api/search?q=foo", "headers": {"Accept": "application/json"}, } ] result = build_hoppscotch_collection(calls, name="MyCol") assert result["v"] == 1 assert result["name"] == "MyCol" assert result["folders"] == [] assert len(result["requests"]) == 1 req = result["requests"][0] assert req["v"] == "2" assert req["endpoint"] == "https://api.example.com/api/search?q=foo" assert req["name"] == "GET /api/search" assert req["method"] == "GET" assert req["params"] == [] assert req["headers"] == [ {"key": "Accept", "value": "application/json", "active": True} ] assert req["auth"] == {"authType": "none", "authActive": True} assert req["preRequestScript"] == "" assert req["testScript"] == "" assert req["requestVariables"] == [] assert req["body"] == {"contentType": None, "body": None} # JSON-serializable json.dumps(result) def test_edge_post_json_con_headers_y_cookies(): calls = [ { "method": "post", "url": "https://api.example.com/login", "headers": { "Content-Type": "application/json", "X-Csrf": "tok", }, "cookies": {"session": "abc", "csrf": "xyz"}, "body": '{"user":"neo"}', "body_type": "json", } ] result = build_hoppscotch_collection(calls) req = result["requests"][0] assert req["method"] == "POST" assert req["name"] == "POST /login" # Header Cookie generado al final con formato "; " join assert req["headers"] == [ {"key": "Content-Type", "value": "application/json", "active": True}, {"key": "X-Csrf", "value": "tok", "active": True}, {"key": "Cookie", "value": "session=abc; csrf=xyz", "active": True}, ] assert req["body"] == { "contentType": "application/json", "body": '{"user":"neo"}', } json.dumps(result) def test_request_names_sobreescribe_nombre_derivado(): calls = [ {"method": "GET", "url": "https://api.example.com/a"}, {"method": "GET", "url": "https://api.example.com/b"}, ] result = build_hoppscotch_collection( calls, request_names=["Custom A"] ) # Indice 0 usa el nombre explicito; indice 1 cae al derivado. assert result["requests"][0]["name"] == "Custom A" assert result["requests"][1]["name"] == "GET /b" def test_form_body_genera_contenttype_urlencoded(): calls = [ { "method": "POST", "url": "https://api.example.com/form", "body": "a=1&b=2", "body_type": "form", } ] req = build_hoppscotch_collection(calls)["requests"][0] assert req["body"] == { "contentType": "application/x-www-form-urlencoded", "body": "a=1&b=2", } def test_raw_body_genera_contenttype_text_plain(): calls = [ { "method": "POST", "url": "https://api.example.com/raw", "body": "hello", "body_type": "raw", } ] req = build_hoppscotch_collection(calls)["requests"][0] assert req["body"] == {"contentType": "text/plain", "body": "hello"} def test_body_type_desconocido_da_body_null(): calls = [ { "method": "POST", "url": "https://api.example.com/x", "body": "ignored", "body_type": "binary", } ] req = build_hoppscotch_collection(calls)["requests"][0] assert req["body"] == {"contentType": None, "body": None} def test_lista_vacia(): result = build_hoppscotch_collection([], name="Empty") assert result == { "v": 1, "name": "Empty", "folders": [], "requests": [], } json.dumps(result) def test_call_spec_sin_url_ni_method(): calls = [{}] req = build_hoppscotch_collection(calls)["requests"][0] assert req["endpoint"] == "" assert req["method"] == "GET" assert req["name"] == "GET /" assert req["headers"] == [] assert req["body"] == {"contentType": None, "body": None} def test_sin_cookies_no_anade_header_cookie(): calls = [ { "method": "GET", "url": "https://api.example.com/x", "headers": {"Accept": "*/*"}, "cookies": {}, } ] req = build_hoppscotch_collection(calls)["requests"][0] assert all(h["key"] != "Cookie" for h in req["headers"])