Ejemplo n.º 1
0
def _verify_https_proxy_is_used(server, config):
    server.for_path(config.events_uri + '/bulk', BasicResponse(200))
    with DefaultEventProcessor(config) as ep:
        ep.send_event({'kind': 'identify', 'user': user})
        ep.flush()
        ep._wait_until_inactive()

        # Our simple stub server implementation can't really do HTTPS proxying, so the request will fail, but
        # it can still record that it *got* the request, which proves that the request went to the proxy.
        req = server.require_request()
        assert req.method == 'CONNECT'
def test_client_fails_to_start_in_streaming_mode_with_401_error():
    with start_server() as stream_server:
        stream_server.for_path('/all', BasicResponse(401))
        config = Config(sdk_key=sdk_key,
                        stream_uri=stream_server.uri,
                        send_events=False)

        with LDClient(config=config) as client:
            assert not client.is_initialized()
            assert client.variation(always_true_flag['key'], user,
                                    False) == False
Ejemplo n.º 3
0
def _verify_http_proxy_is_used(server, config):
    server.for_path(config.events_uri + '/bulk', BasicResponse(200))
    with DefaultEventProcessor(config) as ep:
        ep.send_event({'kind': 'identify', 'user': user})
        ep.flush()
        ep._wait_until_inactive()

        # For an insecure proxy request, our stub server behaves enough like the real thing to satisfy the
        # HTTP client, so we should be able to see the request go through. Note that the URI path will
        # actually be an absolute URI for a proxy request.
        req = server.require_request()
        assert req.method == 'POST'
def test_get_all_data_can_use_cached_data():
    with start_server() as server:
        config = Config(sdk_key='sdk-key', base_uri=server.uri)
        fr = FeatureRequesterImpl(config)

        etag1 = 'my-etag-1'
        etag2 = 'my-etag-2'
        resp_data1 = {'flags': {}, 'segments': {}}
        resp_data2 = {'flags': {'flag1': {'key': 'flag1'}}, 'segments': {}}
        expected_data1 = {FEATURES: {}, SEGMENTS: {}}
        expected_data2 = {FEATURES: {'flag1': {'key': 'flag1'}}, SEGMENTS: {}}
        req_path = '/sdk/latest-all'
        server.for_path(req_path, JsonResponse(resp_data1, {'Etag': etag1}))

        result = fr.get_all_data()
        assert result == expected_data1
        req = server.require_request()
        assert 'If-None-Match' not in req.headers.keys()

        server.for_path(req_path, BasicResponse(304, None, {'Etag': etag1}))

        result = fr.get_all_data()
        assert result == expected_data1
        req = server.require_request()
        assert req.headers['If-None-Match'] == etag1

        server.for_path(req_path, JsonResponse(resp_data2, {'Etag': etag2}))

        result = fr.get_all_data()
        assert result == expected_data2
        req = server.require_request()
        assert req.headers['If-None-Match'] == etag1

        server.for_path(req_path, BasicResponse(304, None, {'Etag': etag2}))

        result = fr.get_all_data()
        assert result == expected_data2
        req = server.require_request()
        assert req.headers['If-None-Match'] == etag2
def test_unrecoverable_http_error(status):
    error_handler = BasicResponse(status)
    store = InMemoryFeatureStore()
    ready = Event()
    with start_server() as server:
        with stream_content(make_put_event()) as stream:
            error_then_success = SequentialHandler(error_handler, stream)
            config = Config(sdk_key='sdk-key',
                            stream_uri=server.uri,
                            initial_reconnect_delay=brief_delay)
            server.for_path('/all', error_then_success)

            with StreamingUpdateProcessor(config, store, ready, None) as sp:
                sp.start()
                ready.wait(5)
                assert not sp.initialized()
                server.should_have_requests(1)
def test_client_sends_diagnostics():
    with start_server() as poll_server:
        with start_server() as events_server:
            poll_server.for_path('/sdk/latest-all',
                                 poll_content([always_true_flag]))
            events_server.for_path('/diagnostic', BasicResponse(202))

            config = Config(sdk_key=sdk_key,
                            base_uri=poll_server.uri,
                            events_uri=events_server.uri,
                            stream=False)
            with LDClient(config=config) as client:
                assert client.is_initialized()

                r = events_server.await_request()
                assert r.headers['Authorization'] == sdk_key
                data = json.loads(r.body)
                assert data['kind'] == 'diagnostic-init'
def test_client_retries_connection_in_streaming_mode_with_non_fatal_error():
    with start_server() as stream_server:
        with stream_content(make_put_event([always_true_flag
                                            ])) as stream_handler:
            error_then_success = SequentialHandler(BasicResponse(503),
                                                   stream_handler)
            stream_server.for_path('/all', error_then_success)
            config = Config(sdk_key=sdk_key,
                            stream_uri=stream_server.uri,
                            initial_reconnect_delay=0.001,
                            send_events=False)

            with LDClient(config=config) as client:
                assert client.is_initialized()
                assert client.variation(always_true_flag['key'], user,
                                        False) == True

                r = stream_server.await_request()
                assert r.headers['Authorization'] == sdk_key
def test_client_sends_event_without_diagnostics():
    with start_server() as poll_server:
        with start_server() as events_server:
            poll_server.for_path('/sdk/latest-all',
                                 poll_content([always_true_flag]))
            events_server.for_path('/bulk', BasicResponse(202))

            config = Config(sdk_key=sdk_key,
                            base_uri=poll_server.uri,
                            events_uri=events_server.uri,
                            stream=False,
                            diagnostic_opt_out=True)
            with LDClient(config=config) as client:
                assert client.is_initialized()
                client.identify(user)
                client.flush()

                r = events_server.await_request()
                assert r.headers['Authorization'] == sdk_key
                data = json.loads(r.body)
                assert len(data) == 1
                assert data[0]['kind'] == 'identify'
def test_records_diagnostic_on_stream_init_failure():
    store = InMemoryFeatureStore()
    ready = Event()
    with start_server() as server:
        with stream_content(make_put_event()) as stream:
            error_then_success = SequentialHandler(BasicResponse(503), stream)
            config = Config(sdk_key='sdk-key',
                            stream_uri=server.uri,
                            initial_reconnect_delay=brief_delay)
            server.for_path('/all', error_then_success)
            diag_accum = _DiagnosticAccumulator(1)

            with StreamingUpdateProcessor(config, store, ready,
                                          diag_accum) as sp:
                sp.start()
                ready.wait(start_wait)
                recorded_inits = diag_accum.create_event_and_reset(
                    0, 0)['streamInits']

                assert len(recorded_inits) == 2
                assert recorded_inits[0]['failed'] is True
                assert recorded_inits[1]['failed'] is False
def test_set_sdk_key_after_init():
    _reset_client()
    with start_server() as stream_server:
        with stream_content(make_put_event()) as stream_handler:
            try:
                stream_server.for_path('/all', BasicResponse(401))

                ldclient.set_config(
                    Config(stream_uri=stream_server.uri, send_events=False))
                assert ldclient.get().is_initialized() is False

                r = stream_server.await_request()
                assert r.headers['Authorization'] == ''

                stream_server.for_path('/all', stream_handler)

                ldclient.set_sdk_key(sdk_key)
                wait_until(ldclient.get().is_initialized, timeout=30)

                r = stream_server.await_request()
                assert r.headers['Authorization'] == sdk_key
            finally:
                _reset_client()