def test_methods_with_host(): request = Request() request.set_host('vk.com') request.set_path('/feed/ru') request.set_scheme('http') request.set_body('Hello') request.set_headers({'Cookie': '1234', 'Reference': 'blablacar.com'}) request.set_header(header='User-Agent', value='Yandex') request.set_method('delete') request.set_protocol('HTTP/1.0') expected_bytes_request: str = 'DELETE /feed/ru HTTP/1.0\r\n' \ 'Host: vk.com\r\n' \ 'Connection: close\r\n' \ 'Content-Length: 5\r\n' \ 'Cookie: 1234\r\n' \ 'Reference: blablacar.com\r\n' \ 'User-Agent: Yandex\r\n\r\n' \ 'Hello' assert bytes(request).decode() == expected_bytes_request assert request.scheme == 'http' assert request.host == 'vk.com'
def prepare_request(arguments) -> Request: request = Request() if arguments.url is not None: request.set_url(arguments.url) elif arguments.host is not None: request.set_host(arguments.host) else: raise AttributeError if arguments.method: request.set_method(arguments.method) if arguments.path: request.set_path(arguments.path) if arguments.scheme: request.set_scheme(arguments.scheme) if arguments.my_headers: headers = {} for header in arguments.my_headers: separator_ind = header.find(":") key = header[0:separator_ind] value = header[separator_ind + 1::].strip() headers[key] = value request.set_headers(headers) if arguments.reference: request.set_header('Reference', arguments.reference) if arguments.cookie: request.set_header('Cookie', arguments.cookie) if arguments.cookiefile: with open(arguments.cookiefile, "r") as file: request.set_header('Cookie', file.read()) if arguments.agent: request.set_header('User-Agent', arguments.agent) if arguments.data: request.set_body(arguments.data) if arguments.file: with open(arguments.file, 'r') as file: request.set_body(file.read()) return request