@@ -8,13 +8,14 @@ import okhttp3.OkHttpClient
8
8
import okhttp3.Request
9
9
import okhttp3.RequestBody.Companion.toRequestBody
10
10
11
- class TestFlagsSetter {
12
- private val baseurl = " http://local.airbyte.dev /api/v1/feature-flags"
11
+ class TestFlagsSetter ( baseUrl : String ) {
12
+ private val basePath = " /api/v1/feature-flags"
13
13
private val httpClient = OkHttpClient ().newBuilder().build()
14
+ private val urlPrefix = if (baseUrl.endsWith(" /" )) " ${baseUrl.trimEnd(' /' )}$basePath " else " $baseUrl$basePath "
14
15
15
16
class FlagOverride <T >(
16
17
private val flag : Flag <T >,
17
- context : Context ,
18
+ context : Context ? = null ,
18
19
value : T ,
19
20
private val testFlags : TestFlagsSetter ,
20
21
) : AutoCloseable {
@@ -27,61 +28,122 @@ class TestFlagsSetter {
27
28
}
28
29
}
29
30
31
+ class FlagRuleOverride <T >(
32
+ private val flag : Flag <T >,
33
+ private val context : Context ,
34
+ private val value : T ,
35
+ private val testFlags : TestFlagsSetter ,
36
+ ) : AutoCloseable {
37
+ init {
38
+ testFlags.setRule(flag, context, value)
39
+ }
40
+
41
+ override fun close () {
42
+ testFlags.deleteRule(flag, context)
43
+ }
44
+ }
45
+
30
46
fun <T > withFlag (
31
47
flag : Flag <T >,
32
- context : Context ,
33
48
value : T ,
49
+ context : Context ? = null,
34
50
) = FlagOverride (flag, context, value, this )
35
51
36
52
fun <T > deleteFlag (flag : Flag <T >) {
37
53
httpClient.newCall(
38
54
Request .Builder ()
39
- .url(" $baseurl /${flag.key} " )
55
+ .url(" $urlPrefix /${flag.key} " )
40
56
.delete()
41
57
.build(),
42
58
).execute()
43
59
}
44
60
45
- fun <T > setFlag (
61
+ fun <T > withRule (
46
62
flag : Flag <T >,
47
63
context : Context ,
48
64
value : T ,
65
+ ) = FlagRuleOverride (flag, context, value, this )
66
+
67
+ fun <T > setFlag (
68
+ flag : Flag <T >,
69
+ context : Context ? = null,
70
+ value : T ,
49
71
) {
50
72
val requestFlag =
51
73
ApiFeatureFlag (
52
74
key = flag.key,
53
75
default = flag.default.toString(),
54
76
rules =
55
- listOf (
56
- ApiRule (
57
- context = ApiContext (kind = context.kind, value = context.key),
58
- value = value.toString(),
59
- ),
60
- ),
77
+ if (context != null ) {
78
+ listOf (
79
+ ApiRule (
80
+ context = ApiContext (kind = context.kind, value = context.key),
81
+ value = value.toString(),
82
+ ),
83
+ )
84
+ } else {
85
+ emptyList()
86
+ },
61
87
)
62
- httpClient.newCall(
88
+ val response =
89
+ httpClient.newCall(
90
+ Request .Builder ()
91
+ .url(urlPrefix)
92
+ .put(Jsons .serialize(requestFlag).toRequestBody(" application/json" .toMediaType()))
93
+ .build(),
94
+ ).execute()
95
+ assert (response.code == 200 , { " Failed to update the feature flag ${requestFlag.key} , error: ${response.code} : ${response.body?.string()} " })
96
+ }
97
+
98
+ fun <T > getFlag (flag : Flag <T >): String? {
99
+ return httpClient.newCall(
63
100
Request .Builder ()
64
- .url(baseurl)
65
- .put(Jsons .serialize(requestFlag).toRequestBody(" application/json" .toMediaType()))
101
+ .url(" $urlPrefix /${flag.key} " )
66
102
.build(),
67
103
).execute()
104
+ .body?.string()
68
105
}
69
106
70
- fun <T > getFlag (flag : Flag <T >) {
71
- httpClient.newCall(
107
+ fun <T > evalFlag (
108
+ flag : Flag <T >,
109
+ context : Context ,
110
+ ): String? {
111
+ return httpClient.newCall(
72
112
Request .Builder ()
73
- .url(" $baseurl /${flag.key} " )
113
+ .url(" $urlPrefix /${flag.key} /evaluate?kind= ${context.kind} &value= ${context .key}" )
74
114
.build(),
75
- ).execute()
115
+ ).execute().body?.string()
76
116
}
77
117
78
- fun <T > evalFlag (
118
+ fun <T > setRule (
119
+ flag : Flag <T >,
120
+ context : Context ,
121
+ value : T ,
122
+ ) {
123
+ val requestRule =
124
+ ApiRule (
125
+ context = ApiContext (kind = context.kind, value = context.key),
126
+ value = value.toString(),
127
+ )
128
+ val response =
129
+ httpClient.newCall(
130
+ Request .Builder ()
131
+ .url(" $urlPrefix /${flag.key} /rules" )
132
+ .post(Jsons .serialize(requestRule).toRequestBody(" application/json" .toMediaType()))
133
+ .build(),
134
+ ).execute()
135
+ assert (response.code == 200 , { " Failed to update the feature flag ${flag.key} , error: ${response.code} : ${response.body?.string()} " })
136
+ }
137
+
138
+ fun <T > deleteRule (
79
139
flag : Flag <T >,
80
140
context : Context ,
81
141
) {
142
+ val requestContext = ApiContext (kind = context.kind, value = context.key)
82
143
httpClient.newCall(
83
144
Request .Builder ()
84
- .url(" $baseurl /${flag.key} /evaluate?kind=${context.kind} &value=${context.key} " )
145
+ .url(" $urlPrefix /${flag.key} /rules" )
146
+ .delete(Jsons .serialize(requestContext).toRequestBody(" application/json" .toMediaType()))
85
147
.build(),
86
148
).execute()
87
149
}
0 commit comments