def test_curl_options_for_post_with_some_file_data(tmpdir): p = tmpdir.join("test.txt") p.write("content") prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="POST", data=p, ) curl_request = CURLRequest(prepared_request) curl_options = curl_request.options expected_headers = [] assert len(curl_options) == 7 assert curl_options[pycurl.URL] == "http://somefakeurl/" assert curl_options[pycurl.SSL_VERIFYHOST] == 0 assert curl_options[pycurl.SSL_VERIFYPEER] == 0 assert curl_options[pycurl.UPLOAD] is True assert curl_options[pycurl.CUSTOMREQUEST] == "POST" assert curl_options[pycurl.HTTPHEADER] == expected_headers # We actually call the function to test that it reads # the expected bytes assert curl_options[pycurl.READFUNCTION]() == "content"
def test_pool_send_configures_handler_correctly(): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) curl_handler = FakeCurlHandler() # Configure the handler to return some body and headers curl_handler.body = b"somebodydata" curl_handler.http_status = 200 curl_handler.header_lines = [ "HTTP/1.1 200 OK\n".encode("iso-8859-1"), "Content-Language: en-US\n".encode("iso-8859-1"), "Cache-Control: no-cache\n".encode("iso-8859-1"), ] pool = CURLHandlerPool(curl_factory=lambda: curl_handler) response = pool.send(curl_request) assert curl_handler.performed assert response.body.getvalue() == b"somebodydata" assert response.http_code == 200 assert response.headers == { "Cache-Control": "no-cache", "Content-Language": "en-US", } # Assert that the curl options from the requests were set to the handler for opt, val in curl_request.options.items(): assert curl_handler.options[opt] == val
def test_curl_response_with_cookies(): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) curl_response = CURLResponse(curl_request) curl_response.http_code = 200 header_lines = [ "HTTP/1.1 200 OK\n", "Content-Language: en-US\n", "Cache-Control: no-cache\n", "Set-Cookie: foo=123; SameSite=None; Secure; Max-Age=2592000\n", "Set-Cookie: bar=abc; HttpOnly\n", ] for header_line in header_lines: # We provide lines encoded as defined in http standard curl_response.add_header_from_raw_line( header_line.encode("iso-8859-1")) req_response = curl_response.to_requests_response() assert len(req_response.cookies) == 2 assert req_response.cookies.get("foo") == "123" assert req_response.cookies.get("bar") == "abc"
def test_curl_options_for_post_put_with_some_string_data( http_method, data, expected_data): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method=http_method, data=data, ) curl_request = CURLRequest(prepared_request) curl_options = curl_request.options expected_headers = [ f"Content-Length: {len(data)}", ] assert len(curl_options) == 7 assert curl_options[pycurl.URL] == "http://somefakeurl/" assert curl_options[pycurl.SSL_VERIFYHOST] == 0 assert curl_options[pycurl.SSL_VERIFYPEER] == 0 assert curl_options[pycurl.UPLOAD] is True assert curl_options[pycurl.CUSTOMREQUEST] == http_method assert curl_options[pycurl.HTTPHEADER] == expected_headers # We actually call the function to test that it reads # the expected bytes assert curl_options[pycurl.READFUNCTION]() == expected_data
def test_use_chunked_upload_is_false_for_json(): prepared_request = PreparedRequest() json_data = {"somekey": "somedata"} prepared_request.prepare( url="http://somefakeurl", json=json_data, ) curl_request = CURLRequest(prepared_request) assert curl_request.use_chunked_upload is False
def test_use_chunked_upload_is_false_for_string_data(): prepared_request = PreparedRequest() some_data = "this is some data as string" prepared_request.prepare( url="http://somefakeurl", data=some_data, ) curl_request = CURLRequest(prepared_request) assert curl_request.use_chunked_upload is False
def test_use_chunked_upload_is_true_for_streamed_data(): prepared_request = PreparedRequest() some_stream_data = six.StringIO("this is some data as string") prepared_request.prepare( url="http://somefakeurl", data=some_stream_data, ) curl_request = CURLRequest(prepared_request) assert curl_request.use_chunked_upload is True
def test_create_empty_curl_response(): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) response = CURLResponse(curl_request) assert response.curl_request is curl_request assert response.request is curl_request.request assert response.http_code == 200 assert not response.headers assert response.reason is None
def test_curl_response_parse_header_line(header_lines, expected_headers): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) response = CURLResponse(curl_request) for header_line in header_lines: # We provide lines encoded as defined in http standard response.add_header_from_raw_line(header_line.encode("iso-8859-1")) assert sorted(response.headers.items()) == sorted(expected_headers.items())
def test_pool_puts_back_handler_after_successful_send(): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) curl_handler = FakeCurlHandler() pool = CURLHandlerPool(curl_factory=lambda: curl_handler) pool.send(curl_request) # Since we have put back the handle, now we can ask for it and it should # be the same and only handle of this pool assert curl_handler is pool.get_handler_from_pool()
def test_empty_curl_response_to_request_response(): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) response = CURLResponse(curl_request) response.http_code = 200 req_response = response.to_requests_response() assert isinstance(req_response, Response) assert req_response.status_code == 200 assert not req_response.headers assert not req_response.text assert req_response.url == "http://somefakeurl/"
def test_curl_options_for_options(): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="OPTIONS", ) curl_request = CURLRequest(prepared_request) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: ["Content-Length: 0"], pycurl.SSL_VERIFYHOST: 0, pycurl.SSL_VERIFYPEER: 0, pycurl.CUSTOMREQUEST: "OPTIONS", } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_curl_options_for_get_with_verify_as_true(): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="GET", ) curl_request = CURLRequest(prepared_request, verify=True) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: [], pycurl.SSL_VERIFYHOST: 2, pycurl.SSL_VERIFYPEER: 2, pycurl.CAINFO: DEFAULT_CA_BUNDLE_PATH, } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_curl_option_with_no_data_nor_headers(): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", headers={}, method="GET", ) curl_request = CURLRequest(prepared_request) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: [], pycurl.SSL_VERIFYHOST: 0, pycurl.SSL_VERIFYPEER: 0, } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_curl_options_for_get_with_timeout_in_seconds(): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="GET", ) curl_request = CURLRequest(prepared_request, timeout=3.2) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: [], pycurl.SSL_VERIFYHOST: 0, pycurl.SSL_VERIFYPEER: 0, pycurl.TIMEOUT_MS: 3200, } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_burl_options_for_get_with_ca_path(tmpdir): pem_path = str(tmpdir.mkdir("some_path")) prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="GET", ) curl_request = CURLRequest(prepared_request, verify=pem_path) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: [], pycurl.SSL_VERIFYHOST: 2, pycurl.SSL_VERIFYPEER: 2, pycurl.CAPATH: pem_path, } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_curl_options_for_get_with_multi_timeout_values(timeout): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="GET", ) curl_request = CURLRequest(prepared_request, timeout=timeout) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: [], pycurl.SSL_VERIFYHOST: 0, pycurl.SSL_VERIFYPEER: 0, pycurl.TIMEOUT_MS: 2234, pycurl.CONNECTTIMEOUT_MS: 1234, } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_proxy_pool_configures_handler_with_proxy_user_and_password(mocker): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) curl_handler = FakeCurlHandler() # Configure the handler to return some body and headers curl_handler.body = b"somebodydata" curl_handler.http_status = 200 curl_handler.header_lines = [ "HTTP/1.1 200 OK\n".encode("iso-8859-1"), "Content-Language: en-US\n".encode("iso-8859-1"), "Cache-Control: no-cache\n".encode("iso-8859-1"), ] proxy_url = parse_url("http://*****:*****@localhost:8080") pool = ProxyCURLHandlerPool(proxy_url, curl_factory=lambda: curl_handler) response = pool.send(curl_request) assert curl_handler.performed assert response.body.getvalue() == b"somebodydata" assert response.http_code == 200 assert response.headers == { "Cache-Control": "no-cache", "Content-Language": "en-US", } # Assert that the curl options from the requests were set to the handler for opt, val in curl_request.options.items(): assert curl_handler.options[opt] == val # Finally, assert that the special proxy options were set assert curl_handler.options[pycurl.PROXY] == "localhost" assert curl_handler.options[pycurl.PROXYAUTH] == pycurl.HTTPAUTH_ANY assert curl_handler.options[pycurl.PROXYUSERPWD] == "user:pwd" assert curl_handler.options[pycurl.PROXYPORT] == 8080
def test_curl_response_with_data_and_headers_to_request_response(): prepared_request = PreparedRequest() prepared_request.prepare(url="http://somefakeurl", method="GET", headers={}) curl_request = CURLRequest(prepared_request) expected_headers = { "Content-Language": "en-US", "Cache-Control": "no-cache", } response = CURLResponse(curl_request) response.http_code = 200 response.headers = expected_headers response.body.write(b"someresponsedata") req_response = response.to_requests_response() assert sorted(req_response.headers.items()) == sorted( expected_headers.items())
def test_curl_options_for_get_with_cert_and_key(): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="GET", ) curl_request = CURLRequest(prepared_request, verify=True, cert=("/some/path", "key")) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: [], pycurl.SSL_VERIFYHOST: 2, pycurl.SSL_VERIFYPEER: 2, pycurl.CAINFO: DEFAULT_CA_BUNDLE_PATH, pycurl.SSLCERT: "/some/path", pycurl.SSLKEY: "key", } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_curl_options_for_head(): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="HEAD", headers={ "Cache-Control": "no-cache", }, ) curl_request = CURLRequest(prepared_request) expected_options = { pycurl.URL: "http://somefakeurl/", pycurl.HTTPHEADER: ["Cache-Control: no-cache"], pycurl.SSL_VERIFYHOST: 0, pycurl.SSL_VERIFYPEER: 0, pycurl.CUSTOMREQUEST: "HEAD", pycurl.NOBODY: True, } curl_options = curl_request.options assert sorted(curl_options.items()) == sorted(expected_options.items())
def test_curl_option_for_post_with_body_with_form_data(form, expected_encoded_form): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="POST", data=form, ) curl_request = CURLRequest(prepared_request) curl_options = curl_request.options expected_headers = sorted([ f"Content-Length: {len(expected_encoded_form)}", "Content-Type: application/x-www-form-urlencoded", ]) assert len(curl_options) == 6 assert curl_options[pycurl.URL] == "http://somefakeurl/" assert curl_options[pycurl.SSL_VERIFYHOST] == 0 assert curl_options[pycurl.SSL_VERIFYPEER] == 0 assert curl_options[pycurl.CUSTOMREQUEST] == "POST" assert sorted(curl_options[pycurl.HTTPHEADER]) == expected_headers assert curl_options[pycurl.POSTFIELDS] == expected_encoded_form
def test_curl_options_with_some_extra_headers(): prepared_request = PreparedRequest() prepared_request.prepare( url="http://somefakeurl", method="GET", headers={ "Content-Language": "en-US", "Cache-Control": "no-cache", }, ) curl_request = CURLRequest(prepared_request) curl_options = curl_request.options expected_headers = sorted([ "Cache-Control: no-cache", "Content-Language: en-US", ]) assert len(curl_options) == 4 assert curl_options[pycurl.URL] == "http://somefakeurl/" assert curl_options[pycurl.SSL_VERIFYHOST] == 0 assert curl_options[pycurl.SSL_VERIFYPEER] == 0 assert sorted(curl_options[pycurl.HTTPHEADER]) == expected_headers
def test_request_property(): prepared_request = PreparedRequest() curl_request = CURLRequest(prepared_request) assert curl_request.request is prepared_request