Last active
April 23, 2022 06:49
-
-
Save peterjpxie/5a64d4d9ad91bea30824638fddece9df to your computer and use it in GitHub Desktop.
parse_test_input.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_test_input(filename): | |
import re | |
with open(filename, "r") as f: | |
content = f.read() | |
# 3 parts split by empty line | |
parts = re.split("\n\n", content) | |
parts_len = len(parts) | |
# part 1: Method and url | |
assert len(parts[0].split()) == 2 | |
method, url = parts[0].split() | |
method, url = method.strip(), url.strip() | |
# part 2: headers | |
if parts_len > 1 and parts[1].strip() != "": | |
header_lines = re.split("\s*\n", parts[1]) | |
header_lines = [line.strip() for line in header_lines] # strip line spaces | |
headers = dict([re.split(":\s*", line) for line in header_lines]) | |
else: | |
headers = {} | |
# part 3: body | |
if parts_len > 2 and parts[2].strip() != "": | |
body = parts[2].strip().strip("\n") | |
else: | |
body = None | |
return method, url, headers, body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment