def test_exceptions(self): self.assertRaises(ValueRequestTypeException, Request, protocol='HTTP/1.1', method='HHHHEEE', timeout=1000, data='', url='https://habr.com/ru/') self.assertRaises(HTTPSClientException, Request, protocol='HTTP/1.1', method='GET', timeout=1000, data='', url='https://habr.com/ru/', cookie_file='sdzc.txt') client = Client(10) req = Request(protocol='HTTP/1.1', method='GET', timeout=1000, data='', url='httff://habrweerwd.erff/ru/') self.assertRaises(ConnectException, client.do_request, req) self.assertRaises(UnreadableFileException, Request, protocol='HTTP/1.1', method='GET', timeout=1000, data='', url='https://habr.com/ru/', cookie_file=str(Path.cwd()))
def test_bytes(self): req = Request(protocol='HTTP/1.1', method='POST', timeout=1000, data='Hello', url='http://ptsv2.com/t/lp5td-1586273836/post', agent='Mozilla/5.0') e = str(req) b = bytes(req).decode('ISO-8859-1') assert b == e
def test_check_get_request(self): req = Request(protocol='HTTP/1.1', method='GET', timeout=1000, data='', url='http://ptsv2.com/t/lp5td-1586273836/post') assert ('GET /t/lp5td-1586273836/post HTTP/1.1\r\n' + 'Host: ptsv2.com\r\n' + 'Connection: close\r\n' + 'Content-Length: 0\r\n\r\n') == str(req)
def test_request_https(self, mock_request): client_mock = mock_request() client_mock.do_request.return_value = \ Response(code=200, message='<!DOCTYPE html>', charset='', protocol='', headers=dict(), request=None) req = Request(protocol='HTTP/1.1', method='GET', timeout=1000, data='', url='https://habr.com/ru/') data = client_mock.do_request(req) assert data is not None assert data.code == 200 assert data.message.__contains__('<!DOCTYPE html>')
def test_request_http(self, mock_request): client_mock = mock_request() client_mock.do_request.return_value = \ Response(message='Thank you for this dump. ' 'I hope you have a lovely day!', charset='', code=200, protocol='', headers=dict(), request=None) req = Request(protocol='HTTP/1.1', method='GET', timeout=1000, data='', url='http://ptsv2.com/t/lp5td-1586273836/post') data = client_mock.do_request(req) assert data is not None assert data.message == 'Thank you for this dump. ' \ 'I hope you have a lovely day!'
def test_prepare_headers(self): path = Path.cwd() / 'tests' / 'resources' / 'cookie.txt' with path.open('w') as f: f.write('hello') req = Request(method='GET', agent='Chrome', cookie_file=str(path), cookie='income=1', reference='https://vk.com/', headers=['Content-Length: 30', 'Pory: tau'], url='http://ptsv2.com/t/lp5td-1586273836/post', data='') assert {'Content-Length': '30', 'Pory': 'tau', 'Reference': 'https://vk.com/', 'Cookie': 'hello', 'User-Agent': 'Chrome', 'Host': 'ptsv2.com', 'Connection': 'close'} == req.headers
def test_redirect(self, mock_request): client_mock = mock_request() client_mock.do_request.return_value = \ Response(message='<!DOCTYPE html>', code=200, protocol='', charset='utf-8', headers=dict(), request=None) req = Request(protocol='HTTP/1.1', method='GET', timeout=1000, data='', url='https://vk.com/feed') client = Client(10) data = client_mock.do_request(req) assert data is not None assert data.code == 200 assert data.message.__contains__('<!DOCTYPE html>') assert data.charset.lower() == 'utf-8' client.max_hops = 0 self.assertRaises(ConnectException, client.do_request, req)
def do_request(self, request: Request): self._max_hops = self._const_max_hops self._response = self.get_response(request) while ((300 <= self._response.code < 400 or self._response.location != '') and self.max_hops): temp_request = Request(url=self._response.location, reference=request.reference, cookie=request.cookie, agent=request.user_agent, headers=request.headers, method=request.request_method, cookie_file=request.cookie_file, timeout=request.timeout, data=request.data, protocol=request.protocol) self._response = self.get_response(temp_request) self._max_hops -= 1 if (not self.max_hops or 300 <= self._response.code < 400 or self._response.location != ''): raise ConnectException(str(request.url)) return self._response
def test_check_post_request_from_file(self): path = Path.cwd() / 'tests' / 'resources' / 'test_text.txt' with path.open('w+') as f: f.write('abracadabra') assert 'abracadabra' == Request.prepare_data(data=str(path))
parser.add_argument('-g', '--count_redirect', type=int, help='Set max count redirect', default=20) return parser args = get_parser().parse_args() request = Request(protocol=args.protocol, timeout=args.timeout, headers=args.headers, method=args.request, agent=args.agent, cookie=args.cookie, cookie_file=args.cookie_file, reference=args.reference, url=args.url, data=args.data or args.file) try: client = Client(int(args.count_redirect)) response = client.do_request(request) get_response(args, response) except HTTPSClientException as e: sys.stderr.write(f'Error: {e.message}') sys.exit(1)