-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_feature_requester.py
133 lines (107 loc) · 5.29 KB
/
test_feature_requester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pytest
from ldclient.config import Config
from ldclient.feature_requester import FeatureRequesterImpl
from ldclient.util import UnsuccessfulResponseException
from ldclient.version import VERSION
from ldclient.versioned_data_kind import FEATURES, SEGMENTS
from testing.http_util import start_server, BasicResponse, JsonResponse
from testing.proxy_test_util import do_proxy_tests
def test_get_all_data_returns_data():
with start_server() as server:
config = Config(sdk_key = 'sdk-key', base_uri = server.uri)
fr = FeatureRequesterImpl(config)
flags = { 'flag1': { 'key': 'flag1' } }
segments = { 'segment1': { 'key': 'segment1' } }
resp_data = { 'flags': flags, 'segments': segments }
expected_data = { FEATURES: flags, SEGMENTS: segments }
server.for_path('/sdk/latest-all', JsonResponse(resp_data))
result = fr.get_all_data()
assert result == expected_data
def test_get_all_data_sends_headers():
with start_server() as server:
config = Config(sdk_key = 'sdk-key', base_uri = server.uri)
fr = FeatureRequesterImpl(config)
resp_data = { 'flags': {}, 'segments': {} }
server.for_path('/sdk/latest-all', JsonResponse(resp_data))
fr.get_all_data()
req = server.require_request()
assert req.headers['Authorization'] == 'sdk-key'
assert req.headers['User-Agent'] == 'PythonClient/' + VERSION
assert req.headers.get('X-LaunchDarkly-Wrapper') is None
assert req.headers.get('X-LaunchDarkly-Tags') is None
def test_get_all_data_sends_wrapper_header():
with start_server() as server:
config = Config(sdk_key = 'sdk-key', base_uri = server.uri,
wrapper_name = 'Flask', wrapper_version = '0.1.0')
fr = FeatureRequesterImpl(config)
resp_data = { 'flags': {}, 'segments': {} }
server.for_path('/sdk/latest-all', JsonResponse(resp_data))
fr.get_all_data()
req = server.require_request()
assert req.headers.get('X-LaunchDarkly-Wrapper') == 'Flask/0.1.0'
def test_get_all_data_sends_wrapper_header_without_version():
with start_server() as server:
config = Config(sdk_key = 'sdk-key', base_uri = server.uri,
wrapper_name = 'Flask')
fr = FeatureRequesterImpl(config)
resp_data = { 'flags': {}, 'segments': {} }
server.for_path('/sdk/latest-all', JsonResponse(resp_data))
fr.get_all_data()
req = server.require_request()
assert req.headers.get('X-LaunchDarkly-Wrapper') == 'Flask'
def test_get_all_data_sends_tags_header():
with start_server() as server:
config = Config(sdk_key = 'sdk-key', base_uri = server.uri,
application = {"id": "my-id", "version": "my-version"})
fr = FeatureRequesterImpl(config)
resp_data = { 'flags': {}, 'segments': {} }
server.for_path('/sdk/latest-all', JsonResponse(resp_data))
fr.get_all_data()
req = server.require_request()
assert req.headers.get('X-LaunchDarkly-Tags') == 'application-id/my-id application-version/my-version'
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_http_proxy(monkeypatch):
def _feature_requester_proxy_test(server, config, secure):
resp_data = { 'flags': {}, 'segments': {} }
expected_data = { FEATURES: {}, SEGMENTS: {} }
server.for_path(config.base_uri + '/sdk/latest-all', JsonResponse(resp_data))
fr = FeatureRequesterImpl(config)
if secure:
try:
fr.get_all_data()
except:
pass # we expect this to fail because we don't have a real HTTPS proxy server
else:
result = fr.get_all_data()
assert result == expected_data
do_proxy_tests(_feature_requester_proxy_test, 'GET', monkeypatch)