def test_urllib_get(httpbin, headers, params, extra): url = httpbin.url + '/get' if params: query = quote('&'.join('%s=%s' % (k, v) for k, v in params.items())) url = url + '?' + query request = Request(url=url, headers=headers) result = get_and_assert_urllib_response(request) assert_curl_response(UrllibRequest(request), extra, result)
def test_requests_post(httpbin, headers, data): url = httpbin.url + '/post' r = requests.post(url, headers=headers, data=data) assert r.status_code == 200 result = json.loads(r.content.decode('utf-8')) log.debug('requets.post result:') log.debug(result) assert_curl_response(RequestsRequest(r.request), object(), result)
def test_requets_get(httpbin, headers, params, extra): url = httpbin.url + '/get' r = requests.get(url, headers=headers, params=params) assert r.status_code == 200 result = json.loads(r.content.decode('utf-8')) log.debug('requets.get result:') log.debug(result) assert_curl_response(RequestsRequest(r.request), extra, result)
def test_urllib_proxy(httpbin, method, proxies, headers, data): run_proxy_server() url = httpbin.url + '/' + method request = Request(url=url, headers=headers) if data: request.data = data.encode('utf-8') http_proxy = proxies.get('http') os.environ['HTTP_PROXY'] = http_proxy result = get_and_assert_urllib_response(request) req = UrllibRequest(request) del os.environ['HTTP_PROXY'] assert_curl_response(req, object(), result)
def test_requests_http_proxy(httpbin, method, proxies, headers, data): run_proxy_server() url = httpbin.url + '/' + method http_proxy = proxies.get('http') os.environ['HTTP_PROXY'] = http_proxy r = requests.request(method, url, headers=headers, data=data) req = RequestsRequest(r.request) del os.environ['HTTP_PROXY'] assert r.status_code == 200 result = json.loads(r.content.decode('utf-8')) log.debug('requets.%s via %s result:' % (method, http_proxy)) log.debug(result) assert_curl_response(req, object(), result)
def test_requests_multipart_post(httpbin, headers, data): # for some reason, requests would not handle arbitrary content-type # in multipart/form-data, so remove, but requests would add it headers = headers.copy() headers.pop('Content-Type') attachment_paths = [] data = data.copy() for key, values in data.items(): if values[0] is not None: attachment_paths.append(values[1]) data[key] = (values[0], open(values[1])) if attachment_paths: log.debug('attachment files:') log.debug(attachment_paths) url = httpbin.url + '/post' r = requests.post(url, headers=headers, files=data) assert r.status_code == 200 result = json.loads(r.content.decode('utf-8')) log.debug('requets.post with multipart result:') log.debug(result) # setup for path in attachment_paths: fname = basename(path) if not os.access(fname, os.F_OK): os.symlink(path, fname) assert_curl_response(RequestsRequest(r.request), object(), result) # tear down for path in attachment_paths: if os.access(fname, os.F_OK): os.remove(basename(path))
def test_urllib_post(httpbin, headers, data): url = httpbin.url + '/post' request = Request(url=url, headers=headers, data=data.encode('utf-8')) result = get_and_assert_urllib_response(request) assert_curl_response(UrllibRequest(request), object(), result)