-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFakeHttpMessageHandlerExtensions.cs
More file actions
165 lines (142 loc) · 6.63 KB
/
Copy pathFakeHttpMessageHandlerExtensions.cs
File metadata and controls
165 lines (142 loc) · 6.63 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Net;
using System.Net.Http.Headers;
using System.Text.Json;
using PostHog.Api;
using PostHog.Json;
/// <summary>
/// Extensions of <see cref="FakeHttpMessageHandler"/> specific to PostHog
/// </summary>
internal static class FakeHttpMessageHandlerExtensions
{
static readonly Uri FlagsUrl = new("https://us.i.posthog.com/flags/?v=2");
public static FakeHttpMessageHandler.RequestHandler AddCaptureResponse(this FakeHttpMessageHandler handler) =>
handler.AddIngestionResponse("capture");
public static FakeHttpMessageHandler.RequestHandler AddBatchResponse(this FakeHttpMessageHandler handler) =>
handler.AddIngestionResponse("batch");
static FakeHttpMessageHandler.RequestHandler AddIngestionResponse(
this FakeHttpMessageHandler handler,
string endpoint) =>
handler.AddResponse(
new Uri($"https://us.i.posthog.com/{endpoint}"),
HttpMethod.Post,
responseBody: new { status = 1 });
public static FakeHttpMessageHandler.RequestHandler AddFlagsResponseException(
this FakeHttpMessageHandler handler,
Exception exception)
=> handler.AddResponseException(FlagsUrl, HttpMethod.Post, exception);
public static FakeHttpMessageHandler.RequestHandler AddFlagsResponse(
this FakeHttpMessageHandler handler,
Func<Dictionary<string, object>, bool> requestPredicate,
string responseBody)
=> handler.AddFlagsResponse(
requestPredicate,
responseBody: Deserialize<FlagsApiResult>(responseBody));
public static FakeHttpMessageHandler.RequestHandler AddFlagsResponse(
this FakeHttpMessageHandler handler,
string responseBody)
=> handler.AddFlagsResponse(_ => true, responseBody);
public static FakeHttpMessageHandler.RequestHandler AddFlagsResponse(
this FakeHttpMessageHandler handler,
FlagsApiResult responseBody)
=> handler.AddFlagsResponse(
_ => true,
responseBody: responseBody);
public static FakeHttpMessageHandler.RequestHandler AddFlagsResponse(
this FakeHttpMessageHandler handler,
Func<Dictionary<string, object>, bool> requestPredicate,
FlagsApiResult responseBody)
=> handler.AddResponse(
FlagsUrl,
HttpMethod.Post,
requestPredicate,
responseBody);
public static void AddRepeatedFlagsResponse(this FakeHttpMessageHandler handler, int count, Func<int, string> responseBodyFunc)
=> handler.AddRepeatedResponses(
count,
FlagsUrl,
HttpMethod.Post,
responseBodyFunc: responseBodyFunc);
public static void AddRepeatedFlagsResponse(this FakeHttpMessageHandler handler, int count, string responseBody)
=> handler.AddRepeatedFlagsResponse(count, _ => responseBody);
internal static readonly Uri LocalEvaluationUrl = new("https://us.i.posthog.com/flags/definitions?token=fake-project-token&send_cohorts");
public static FakeHttpMessageHandler.RequestHandler AddLocalEvaluationResponse(
this FakeHttpMessageHandler handler,
string responseBody)
=> handler.AddLocalEvaluationResponse(Deserialize<LocalEvaluationApiResult>(responseBody));
public static FakeHttpMessageHandler.RequestHandler AddLocalEvaluationResponse(
this FakeHttpMessageHandler handler,
LocalEvaluationApiResult responseBody) =>
handler.AddResponse(
LocalEvaluationUrl,
HttpMethod.Get,
responseBody: responseBody);
/// <summary>
/// Adds a local evaluation response with an ETag header.
/// </summary>
public static FakeHttpMessageHandler.RequestHandler AddLocalEvaluationResponseWithETag(
this FakeHttpMessageHandler handler,
string responseBody,
string etag)
{
#pragma warning disable CA2000 // HttpResponseMessage is disposed by the handler
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(responseBody, System.Text.Encoding.UTF8, "application/json")
};
#pragma warning restore CA2000
response.Headers.ETag = new EntityTagHeaderValue(etag);
return handler.AddResponse(LocalEvaluationUrl, HttpMethod.Get, response);
}
/// <summary>
/// Adds a 304 Not Modified response for local evaluation.
/// </summary>
public static FakeHttpMessageHandler.RequestHandler AddLocalEvaluationNotModifiedResponse(
this FakeHttpMessageHandler handler,
string? etag = null)
{
#pragma warning disable CA2000 // HttpResponseMessage is disposed by the handler
var response = new HttpResponseMessage(HttpStatusCode.NotModified);
#pragma warning restore CA2000
if (etag is not null)
{
response.Headers.ETag = new EntityTagHeaderValue(etag);
}
return handler.AddResponse(LocalEvaluationUrl, HttpMethod.Get, response);
}
/// <summary>
/// Adds a quota_limited error response for local evaluation.
/// </summary>
public static FakeHttpMessageHandler.RequestHandler AddLocalEvaluationQuotaLimitedResponse(
this FakeHttpMessageHandler handler)
{
const string quotaLimitedBody = """
{
"type": "quota_limited",
"detail": "You have exceeded your feature flag request quota",
"code": "payment_required"
}
""";
#pragma warning disable CA2000 // HttpResponseMessage is disposed by the handler
var response = new HttpResponseMessage(HttpStatusCode.PaymentRequired)
{
Content = new StringContent(quotaLimitedBody, System.Text.Encoding.UTF8, "application/json")
};
#pragma warning restore CA2000
return handler.AddResponse(LocalEvaluationUrl, HttpMethod.Get, response);
}
public static FakeHttpMessageHandler.RequestHandler AddRemoteConfigResponse(
this FakeHttpMessageHandler handler,
string key,
string responseBody) =>
handler.AddResponse(
new Uri($"https://us.i.posthog.com/api/projects/@current/feature_flags/{key}/remote_config?token=fake-project-token"),
HttpMethod.Get,
responseBody: responseBody);
public static FakeHttpMessageHandler.RequestHandler AddDecryptedPayloadResponse(
this FakeHttpMessageHandler handler,
string key,
string responseBody) =>
handler.AddRemoteConfigResponse(key, responseBody);
static T Deserialize<T>(string json) => JsonSerializer.Deserialize<T>(json, JsonSerializerHelper.Options)
?? throw new ArgumentException("Json is invalid and deserializes to null", nameof(json));
}