def test_custom_headers(httpserver: HTTPServer): headers_with_values_in_direct_order = {'Custom': 'Scheme key0="value0", key1="value1"'} httpserver.expect_request(uri='/', headers=headers_with_values_in_direct_order).respond_with_data('OK') response = requests.get(httpserver.url_for('/'), headers=headers_with_values_in_direct_order) assert response.status_code == 200 assert response.text == 'OK' # By default different order of items in header value dicts means different header values headers_with_values_in_modified_order = {'Custom': 'Scheme key1="value1", key0="value0"'} response = requests.get(httpserver.url_for('/'), headers=headers_with_values_in_modified_order) assert response.status_code == 500 # Define header_value_matcher that ignores the order of items in header value dict def custom_header_value_matcher(actual: str, expected: str) -> bool: actual_scheme, _, actual_dict_str = actual.partition(' ') expected_scheme, _, expected_dict_str = expected.partition(' ') actual_dict = parse_dict_header(actual_dict_str) expected_dict = parse_dict_header(expected_dict_str) return actual_scheme == expected_scheme and actual_dict == expected_dict matchers = HeaderValueMatcher.DEFAULT_MATCHERS.copy() # type: ignore matchers['Custom'] = custom_header_value_matcher header_value_matcher = HeaderValueMatcher(matchers) httpserver.handlers.clear() httpserver.expect_request( uri='/', headers=headers_with_values_in_direct_order, header_value_matcher=header_value_matcher ).respond_with_data('OK') response = requests.get(httpserver.url_for('/'), headers=headers_with_values_in_modified_order) assert response.status_code == 200 assert response.text == 'OK'
def test_get_session_info(httpserver: HTTPServer): expected_json = { "info": { "authenticated": "cookie", "authentication_db": "_users", "authentication_handlers": [ "cookie", "default" ] }, "ok": True, "userCtx": { "name": "root", "roles": [ "_admin" ] } } httpserver.expect_request("/_session", method="POST").respond_with_json(expected_json, headers={'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly'}) httpserver.expect_oneshot_request("/_session", method="GET").respond_with_json(expected_json, headers={'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly'}) couch = CouchDB(username="******", password="******", host="http://127.0.0.1", port=8000, auto_connect=True) response = couch.session.get_session_info() assert response == expected_json httpserver.expect_oneshot_request("/_session", method="GET").respond_with_json(expected_json, headers={'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly'}) response = couch.session.renew_session() assert response == expected_json for code in [401]: httpserver.expect_oneshot_request("/_session", method="GET").respond_with_json({}, status=code) response = couch.session.get_session_info() assert isinstance(response, CouchError) is True
def _build_server(tmpdir, host): ca = trustme.CA() ca_cert_path = str(tmpdir / "ca.pem") ca.cert_pem.write_to_path(ca_cert_path) server_cert = ca.issue_cert(host) context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) server_crt = server_cert.cert_chain_pems[0] server_key = server_cert.private_key_pem with server_crt.tempfile() as crt_file, server_key.tempfile() as key_file: context.load_cert_chain(crt_file, key_file) server = HTTPServer(ssl_context=context) # Fake what the client expects from Elasticsearch server.expect_request("/").respond_with_json( headers={ "x-elastic-product": "Elasticsearch", }, response_json={"version": { "number": "8.0.0", }}, ) server.start() yield server, ca, ca_cert_path server.clear() if server.is_running(): server.stop()
def test_get_packt_free_ebook(httpserver: HTTPServer, resources: Resources): # GIVEN PRODUCT_ID = "android-2099" TITLE = "Android 2099" ONE_LINER = "Good practices with Miguel O’Hara" COVER_IMAGE = "//test.jpg" path_offers = "/free-learning-v1/offers" Resources._BOOK_URL_OFFERS = httpserver.url_for(path_offers) httpserver.expect_request(path_offers, "GET").respond_with_json( {"data": [{ "productId": PRODUCT_ID }]}) path_summary = "/products/{}/summary" Resources._BOOK_URL_SUMMARY = httpserver.url_for(path_summary) httpserver.expect_request(path_summary.format(PRODUCT_ID), "GET").respond_with_json({ "title": TITLE, "oneLiner": ONE_LINER, "coverImage": COVER_IMAGE }) # WHEN book = resources.get_packt_free_book() # THEN assert book["name"] == TITLE assert book["summary"] == ONE_LINER assert book["cover"] == COVER_IMAGE assert book["expires"] > 0
def test_read_private_addres(reader_class: Type[FeedReader], httpserver: HTTPServer): httpserver.expect_request("/private-address").respond_with_json(0) url = httpserver.url_for("/private-address") with reader_class() as reader: response = reader.read(url) assert response.status == FeedResponseStatus.PRIVATE_ADDRESS_ERROR assert not response.content
def test_find_not_found(httpserver: HTTPServer): httpserver.expect_request('/404').respond_with_data('Not Found', status=404) start_url = httpserver.url_for('/404') finder, messages = _create_finder(start_url) with finder: found = finder.find() assert not found, f'messages={messages}'
def test_Identify_ExceedQueueSize_Flushed(httpserver: HTTPServer): httpserver.expect_request(re.compile('/users/track')).respond_with_data() p = BrazePlugin( 'My-Key', BrazeOptions(base_url=httpserver.url_for(''), flush_queue_size=2, flush_interval=timedelta_max)) p.load(plugin_load_options) p.identify("user-1", identify_properties) p.identify("user-2", identify_properties) time.sleep(time_short) requests = _get_cleaned_requests(httpserver) assert requests == [ { 'attributes': [ { 'external_id': 'user-1', 'item1': 'identify', 'item2': 2 }, { 'external_id': 'user-2', 'item1': 'identify', 'item2': 2 }, ], }, ] p.shutdown()
def test_get_node_server_stat(httpserver: test_server.HTTPServer): expected_json = { "value": { "min": 0, "max": 0, "arithmetic_mean": 0, "geometric_mean": 0, "harmonic_mean": 0, "median": 0, "variance": 0, "standard_deviation": 0, "skewness": 0, "kurtosis": 0, "percentile": [[50, 0], [75, 0], [90, 0], [95, 0], [99, 0], [999, 0]], "histogram": [[0, 0]], "n": 0 }, "type": "histogram", "desc": "length of a request inside CouchDB without MochiWeb" } httpserver.expect_request("/_node/_local/_stats/couchdb/request_time", method="GET").respond_with_json(expected_json) response = couch.server.node_stat(uri_segments={'node_name': '_local', 'stat': 'couchdb/request_time'}) assert response == expected_json response = couch.server.node_stat() assert response == expected_json
def test_get_refresh_token(httpserver: HTTPServer, auth_token: Token): response_json: dict = { "access_token": "ACCESS_TOKEN_TO_STORE", "token_type": "bearer", "expires_in": 3600, "refresh_token": "TOKEN_USED_TO_REFRESH_AUTHORIZATION" } # create a invalid http request httpserver.expect_oneshot_request("/HttpNoSuccess") # refresh token with an error with pytest.raises(HttpNoSuccess): auth_token.get_refresh_token(post_url=httpserver.url_for("/refresh")) # set up the server to serve /refresh with the json httpserver.expect_request("/refresh").respond_with_json(response_json) # refresh token auth_token.get_refresh_token(post_url=httpserver.url_for("/refresh")) # assert if token was refreshed assert auth_token.access_token == response_json["access_token"] assert auth_token.refresh_token == response_json["refresh_token"] sleep(1) # assert if token was saved auth_token_2: Optional[Token] = Token.get_token() assert isinstance(auth_token_2, Token) assert auth_token_2.access_token == auth_token.access_token assert auth_token_2.refresh_token == auth_token.refresh_token
def test_get_database_updates(httpserver: test_server.HTTPServer): expected_json = { "results": [ { "db_name": "mailbox", "type": "created", "seq": "1-g1AAAAFReJzLYWBg4MhgTmHgzcvPy09JdcjLz8gvLskBCjMlMiTJ____PyuDOZExFyjAnmJhkWaeaIquGIf2JAUgmWQPMiGRAZcaB5CaePxqEkBq6vGqyWMBkgwNQAqobD4h"}, { "db_name": "mailbox", "type": "deleted", "seq": "2-g1AAAAFReJzLYWBg4MhgTmHgzcvPy09JdcjLz8gvLskBCjMlMiTJ____PyuDOZEpFyjAnmJhkWaeaIquGIf2JAUgmWQPMiGRAZcaB5CaePxqEkBq6vGqyWMBkgwNQAqobD4hdQsg6vYTUncAou4-IXUPIOpA7ssCAIFHa60" } ], "last_seq": "2-g1AAAAFReJzLYWBg4MhgTmHgzcvPy09JdcjLz8gvLskBCjMlMiTJ____PyuDOZEpFyjAnmJhkWaeaIquGIf2JAUgmWQPMiGRAZcaB5CaePxqEkBq6vGqyWMBkgwNQAqobD4hdQsg6vYTUncAou4-IXUPIOpA7ssCAIFHa60" } httpserver.expect_oneshot_request("/_db_updates", method="POST").respond_with_json(expected_json) response = couch.server.database_updates() assert response == expected_json httpserver.expect_oneshot_request("/_db_updates", method="POST").respond_with_json({}, status=401) response = couch.server.database_updates() assert isinstance(response, couchapy.CouchError) is True assert response.status_code == 401 httpserver.expect_request("/_db_updates", method="POST").respond_with_json({}) for k in AllowedKeys.SERVER__DB_UPDATES__PARAMS: response = couch.server.database_updates(params={k: ['test']}) assert isinstance(response, couchapy.CouchError) is False with pytest.raises(couchapy.InvalidKeysException): couch.server.database_updates(params={'nonexisting_key': ''})
def test_error_dereferencing(api, users, location, es, httpserver: pytest_httpserver.HTTPServer): with api.test_request_context(): record = SWORDDeposit.create({}) object_version = ObjectVersion.create(bucket=record.bucket, key="some-file.txt") TagManager(object_version).update({ ObjectTagKey.ByReferenceURL: httpserver.url_for("some-file.txt"), # This one should get removed after dereferencing ObjectTagKey.ByReferenceNotDeleted: "true", ObjectTagKey.Packaging: PackagingFormat.SimpleZip, }) httpserver.expect_request("/some-file.txt").respond_with_data( b"", status=HTTPStatus.GONE) db.session.refresh(object_version) with pytest.raises(urllib.error.HTTPError): tasks.dereference_object(record.id, object_version.version_id) db.session.refresh(object_version) tags = TagManager(object_version) assert tags.get(ObjectTagKey.FileState) == FileState.Error
def podcastURL(httpserver: HTTPServer): with open('test/bbc-world-news.xml', 'r') as fd: httpserver.expect_request('/bbc-world-news.rss') \ .respond_with_data(fd.read(), content_type='application/rss+xml') httpserver.expect_request('/invalid') \ .respond_with_data(b'', status=404) return httpserver.url_for('/bbc-world-news.rss')
def test_authorization_headers(httpserver: HTTPServer): headers_with_values_in_direct_order = { 'Authorization': ('Digest username="******",' 'realm="*****@*****.**",' 'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",' 'uri="/dir/index.html",' 'qop=auth,' 'nc=00000001,' 'cnonce="0a4f113b",' 'response="6629fae49393a05397450978507c4ef1",' 'opaque="5ccc069c403ebaf9f0171e9517f40e41"') } httpserver.expect_request(uri='/', headers=headers_with_values_in_direct_order).respond_with_data('OK') response = requests.get(httpserver.url_for('/'), headers=headers_with_values_in_direct_order) assert response.status_code == 200 assert response.text == 'OK' headers_with_values_in_modified_order = { 'Authorization': ('Digest qop=auth,' 'username="******",' 'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",' 'uri="/dir/index.html",' 'nc=00000001,' 'realm="*****@*****.**",' 'response="6629fae49393a05397450978507c4ef1",' 'cnonce="0a4f113b",' 'opaque="5ccc069c403ebaf9f0171e9517f40e41"') } response = requests.get(httpserver.url_for('/'), headers=headers_with_values_in_modified_order) assert response.status_code == 200 assert response.text == 'OK'
def test_get_uptime(httpserver: HTTPServer): expected_json = {"uptime": 259, "memory": 1000} httpserver.expect_request("/_node/_local/_system", method="GET").respond_with_json(expected_json) response = couch.server.get_uptime(uri_segments={'node_name': '_local'}) assert response == expected_json['uptime']
def test_restart_node(httpserver: HTTPServer): expected_json = {} httpserver.expect_request("/_node/_local/_restart", method="POST").respond_with_json(expected_json) response = couch.server.restart_node(uri_segments={'node_name': '_local'}) assert isinstance(response, CouchError) is False
def test_build_fake(app, httpserver: HTTPServer): base_url = "/snapshots/targets/testtarget/testsubtarget" upstream_path = Path( "./tests/upstream/snapshots/targets/testtarget/testsubtarget/") expected_file_requests = [ "sha256sums.sig", "sha256sums", "openwrt-imagebuilder-testtarget-testsubtarget.Linux-x86_64.tar.xz", ] for f in expected_file_requests: httpserver.expect_request(f"{base_url}/{f}").respond_with_data( (upstream_path / f).read_bytes()) request_data = dict( version_data={ "branch": "master", "path": "snapshots", "pubkey": "RWSrHfFmlHslUcLbXFIRp+eEikWF9z1N77IJiX5Bt/nJd1a/x+L+SU89", }, target="testtarget/testsubtarget", store_path=app.config["STORE_PATH"], cache_path=app.config["CACHE_PATH"], upstream_url="http://localhost:8001", version="SNAPSHOT", profile="testprofile", packages=["test1", "test2"], ) result = build(request_data) assert result["id"] == "testprofile"
def test_mixpanel(httpserver: HTTPServer): httpserver.expect_request(re.compile('/(track|engage)')).respond_with_json({'status': 1}) options = MixpanelOptions( api_host=httpserver.url_for('').rstrip('/'), ) p = MixpanelPlugin('My-Key', options) assert p.id() == 'mixpanel' p.load(PluginLoadOptions(environment=Environment.DEVELOPMENT, logger=Logger.NONE)) p.identify("user-1", Properties(item1='value1', item2=2)) p.alias("user-1", "prev-user-1") p.track("user-2", Event('event-1', Properties(item1='value1', item2=1))) p.track("user-2", Event('event-2', Properties(item1='value2', item2=2))) p.flush() p.track("user-2", Event('event-3', Properties(item1='value3', item2=3))) p.track("user-2", Event('event-4', Properties(item1='value4', item2=4))) p.track("user-1", Event('event-5', Properties(item1='value5', item2=5))) p.shutdown() time.sleep(0.1) requests = _get_cleaned_requests(httpserver) httpserver.stop() assert requests == [ {'event': 'event-1', 'properties': {'distinct_id': 'user-2', 'item1': 'value1', 'item2': 1, 'mp_lib': 'python', 'token': 'My-Key'}}, {'event': 'event-2', 'properties': {'distinct_id': 'user-2', 'item1': 'value2', 'item2': 2, 'mp_lib': 'python', 'token': 'My-Key'}}, {'$distinct_id': 'user-1', '$set': {'item1': 'value1', 'item2': 2}, '$token': 'My-Key'}, {'event': 'event-3', 'properties': {'distinct_id': 'user-2', 'item1': 'value3', 'item2': 3, 'mp_lib': 'python', 'token': 'My-Key'}}, {'event': 'event-4', 'properties': {'distinct_id': 'user-2', 'item1': 'value4', 'item2': 4, 'mp_lib': 'python', 'token': 'My-Key'}}, {'event': 'event-5', 'properties': {'distinct_id': 'user-1', 'item1': 'value5', 'item2': 5, 'mp_lib': 'python', 'token': 'My-Key'}} ]
def test_json_matcher_with_none(httpserver: HTTPServer): httpserver.expect_request("/foo", json=None).respond_with_data("Hello world!") resp = requests.get(httpserver.url_for("/foo"), data=json.dumps(None), headers={"content-type": "application/json"}) assert resp.status_code == 200 assert resp.text == "Hello world!"
def test_querystring_str(httpserver: HTTPServer): httpserver.expect_request( "/foobar", query_string="foo=bar", method="GET").respond_with_data("example_response") response = requests.get(httpserver.url_for("/foobar?foo=bar")) httpserver.check_assertions() assert response.text == "example_response" assert response.status_code == 200
def test_read_status(reader_class: Type[FeedReader], httpserver: HTTPServer, status: int): options = dict(allow_non_webpage=True, allow_private_address=True) local_resp = WerkzeugResponse(str(status), status=status) httpserver.expect_request("/status").respond_with_response(local_resp) url = httpserver.url_for("/status") with reader_class(**options) as reader: response = reader.read(url) assert response.status == status assert response.content == str(status).encode()
def test_get_doc_info(httpserver: HTTPServer): expected = 'revidhere' httpserver.expect_request("/_local/testdoc", method="HEAD").respond_with_json( {}, headers={'ETag': 'revidhere'}) response = couch.db.get_doc_info(uri_segments={'docid': 'testdoc'}) assert response == expected
def test_dns_service_urllib3_private(httpserver: HTTPServer): httpserver.expect_request('/200').respond_with_data('200') url = httpserver.url_for('/200') # expect OK when use standard requests Session assert requests.get(url).ok # expect raise PrivateAddressError when use DNSService with pytest.raises(PrivateAddressError): with _requests_session() as sess: sess.get(url)
def test_request_post_case_insensitive_method(httpserver: HTTPServer): httpserver.expect_request( "/foobar", data='{"request": "example"}', method="post").respond_with_data("example_response") response = requests.post(httpserver.url_for("/foobar"), json={"request": "example"}) httpserver.check_assertions() assert response.text == "example_response" assert response.status_code == 200
def test_auto_renew_session_terminates_on_instance_destruction(httpserver: HTTPServer): expected_json = {"ok": True, "name": "root", "roles": ["_admin"]} httpserver.expect_request("/_session", method="POST").respond_with_json(expected_json, headers={'Set-Cookie': 'AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly'}) for k in range(1, 50): couch = CouchDB(username="******", password="******", host="http://127.0.0.1", port=8000, auto_connect=True, keep_alive=300) assert couch.session._keep_alive == 300 del couch # brittle means of getting the code coverage on destructor.
def test_replicate(httpserver: test_server.HTTPServer): expected_json = { "history": [ { "doc_write_failures": 0, "docs_read": 10, "docs_written": 10, "end_last_seq": 28, "end_time": "Sun, 11 Aug 2013 20:38:50 GMT", "missing_checked": 10, "missing_found": 10, "recorded_seq": 28, "session_id": "142a35854a08e205c47174d91b1f9628", "start_last_seq": 1, "start_time": "Sun, 11 Aug 2013 20:38:50 GMT" }, { "doc_write_failures": 0, "docs_read": 1, "docs_written": 1, "end_last_seq": 1, "end_time": "Sat, 10 Aug 2013 15:41:54 GMT", "missing_checked": 1, "missing_found": 1, "recorded_seq": 1, "session_id": "6314f35c51de3ac408af79d6ee0c1a09", "start_last_seq": 0, "start_time": "Sat, 10 Aug 2013 15:41:54 GMT" } ], "ok": True, "replication_id_version": 3, "session_id": "142a35854a08e205c47174d91b1f9628", "source_last_seq": 28 } httpserver.expect_oneshot_request("/_replicate", method="POST").respond_with_json(expected_json) response = couch.server.replicate() assert response == expected_json for code in [202]: httpserver.expect_oneshot_request("/_replicate", method="POST").respond_with_json({}, status=code) response = couch.server.replicate() assert isinstance(response, couchapy.CouchError) is False for code in [400, 401, 404, 500]: httpserver.expect_oneshot_request("/_replicate", method="POST").respond_with_json({}, status=code) response = couch.server.replicate() assert isinstance(response, couchapy.CouchError) is True httpserver.expect_request("/_replicate", method="POST").respond_with_json({}) for k in AllowedKeys.SERVER__REPLICATE__DATA: response = couch.server.replicate(data={k: ['test']}) assert isinstance(response, couchapy.CouchError) is False with pytest.raises(couchapy.InvalidKeysException): couch.server.replicate(data={'nonexisting_key': ''})
def test_json_matcher_without_content_type(httpserver: HTTPServer): httpserver.expect_request("/foo", json={ "foo": "bar" }).respond_with_data("Hello world!") assert requests.get(httpserver.url_for("/foo"), json={ "foo": "bar" }).status_code == 200 assert requests.get(httpserver.url_for("/foo"), data=json.dumps({"foo": "bar"})).status_code == 200
def test_read_non_webpage(reader_class: Type[FeedReader], httpserver: HTTPServer, mime_type: str): options = dict(allow_private_address=True) local_resp = WerkzeugResponse(b'xxxxxxxx', mimetype=mime_type) httpserver.expect_request("/non-webpage").respond_with_response(local_resp) url = httpserver.url_for("/non-webpage") with reader_class(**options) as reader: response = reader.read(url) assert response.status == FeedResponseStatus.CONTENT_TYPE_NOT_SUPPORT_ERROR assert not response.content
def test_object_with_eq(httpserver: HTTPServer): httpserver.expect_request(PrefixMatchEq("/foo")).respond_with_json( {"foo": "bar"}) assert requests.get(httpserver.url_for("/foo")).json() == {"foo": "bar"} assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"} assert requests.get(httpserver.url_for("/foobaz")).json() == {"foo": "bar"} assert requests.get(httpserver.url_for("/barfoo")).status_code == 500 assert len(httpserver.assertions) == 1
async def test_do_get_404(httpserver: pytest_httpserver.HTTPServer): httpserver.expect_request('/404.html').\ respond_with_data('Not Found', content_type='text/html', status=404) async with http.new_client() as session: response, error = await http.get(session, httpserver.url_for('/404.html')) assert response.text == 'Not Found' assert error == 'HTTP status code 404'
async def test_do_get_ok(httpserver: pytest_httpserver.HTTPServer): httpserver.expect_request('/index.html').\ respond_with_data('OK', content_type='text/html') async with http.new_client() as session: response, error = await http.get(session, httpserver.url_for('/index.html')) assert response.text == 'OK' assert error is None