2014-04-28 11:29:41 +02:00
|
|
|
"""Test data"""
|
2022-02-01 10:14:24 +01:00
|
|
|
import json
|
2020-09-28 12:16:57 +02:00
|
|
|
from pathlib import Path
|
2022-02-01 10:14:24 +01:00
|
|
|
from typing import Optional, Dict, Any
|
2014-04-24 15:17:04 +02:00
|
|
|
|
2022-02-01 10:14:24 +01:00
|
|
|
import httpie
|
2021-10-06 17:27:07 +02:00
|
|
|
from httpie.encoding import UTF8
|
|
|
|
from httpie.output.formatters.xml import pretty_xml, parse_xml
|
2021-08-05 20:58:43 +02:00
|
|
|
|
2014-04-24 15:17:04 +02:00
|
|
|
|
|
|
|
def patharg(path):
|
2014-04-26 23:04:45 +02:00
|
|
|
"""
|
|
|
|
Back slashes need to be escaped in ITEM args,
|
|
|
|
even in Windows paths.
|
|
|
|
|
|
|
|
"""
|
2020-09-28 12:16:57 +02:00
|
|
|
return str(path).replace('\\', '\\\\\\')
|
2014-04-24 15:17:04 +02:00
|
|
|
|
|
|
|
|
2020-09-28 12:16:57 +02:00
|
|
|
FIXTURES_ROOT = Path(__file__).parent
|
|
|
|
FILE_PATH = FIXTURES_ROOT / 'test.txt'
|
|
|
|
JSON_FILE_PATH = FIXTURES_ROOT / 'test.json'
|
2021-09-21 19:07:59 +02:00
|
|
|
JSON_WITH_DUPE_KEYS_FILE_PATH = FIXTURES_ROOT / 'test_with_dupe_keys.json'
|
2020-09-28 12:16:57 +02:00
|
|
|
BIN_FILE_PATH = FIXTURES_ROOT / 'test.bin'
|
2022-02-01 10:14:24 +01:00
|
|
|
|
2021-08-31 22:49:53 +02:00
|
|
|
XML_FILES_PATH = FIXTURES_ROOT / 'xmldata'
|
|
|
|
XML_FILES_VALID = list((XML_FILES_PATH / 'valid').glob('*_raw.xml'))
|
|
|
|
XML_FILES_INVALID = list((XML_FILES_PATH / 'invalid').glob('*.xml'))
|
2014-04-24 15:17:04 +02:00
|
|
|
|
2022-02-01 10:14:24 +01:00
|
|
|
SESSION_FILES_PATH = FIXTURES_ROOT / 'session_data'
|
|
|
|
SESSION_FILES_OLD = sorted((SESSION_FILES_PATH / 'old').glob('*.json'))
|
|
|
|
SESSION_FILES_NEW = sorted((SESSION_FILES_PATH / 'new').glob('*.json'))
|
|
|
|
|
|
|
|
SESSION_VARIABLES = {
|
|
|
|
'__version__': httpie.__version__,
|
|
|
|
'__host__': 'null',
|
|
|
|
}
|
|
|
|
|
2014-04-24 15:17:04 +02:00
|
|
|
FILE_PATH_ARG = patharg(FILE_PATH)
|
|
|
|
BIN_FILE_PATH_ARG = patharg(BIN_FILE_PATH)
|
|
|
|
JSON_FILE_PATH_ARG = patharg(JSON_FILE_PATH)
|
|
|
|
|
2020-09-28 12:16:57 +02:00
|
|
|
# Strip because we don't want new lines in the data so that we can
|
|
|
|
# easily count occurrences also when embedded in JSON (where the new
|
|
|
|
# line would be escaped).
|
2021-08-05 20:58:43 +02:00
|
|
|
FILE_CONTENT = FILE_PATH.read_text(encoding=UTF8).strip()
|
2014-04-26 18:41:28 +02:00
|
|
|
|
2021-12-23 20:05:58 +01:00
|
|
|
ASCII_FILE_CONTENT = "random text" * 10
|
|
|
|
|
2014-04-26 19:47:14 +02:00
|
|
|
|
2021-08-05 20:58:43 +02:00
|
|
|
JSON_FILE_CONTENT = JSON_FILE_PATH.read_text(encoding=UTF8)
|
2020-09-28 12:16:57 +02:00
|
|
|
BIN_FILE_CONTENT = BIN_FILE_PATH.read_bytes()
|
2014-04-26 19:47:14 +02:00
|
|
|
UNICODE = FILE_CONTENT
|
2021-10-06 17:27:07 +02:00
|
|
|
XML_DATA_RAW = '<?xml version="1.0" encoding="utf-8"?><root><e>text</e></root>'
|
|
|
|
XML_DATA_FORMATTED = pretty_xml(parse_xml(XML_DATA_RAW))
|
2022-02-01 10:14:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
def read_session_file(session_file: Path, *, extra_variables: Optional[Dict[str, str]] = None) -> Any:
|
|
|
|
with open(session_file) as stream:
|
|
|
|
data = stream.read()
|
|
|
|
|
|
|
|
session_vars = {**SESSION_VARIABLES, **(extra_variables or {})}
|
|
|
|
for variable, value in session_vars.items():
|
|
|
|
data = data.replace(variable, value)
|
|
|
|
|
|
|
|
return json.loads(data)
|