-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.cs
More file actions
63 lines (57 loc) · 2.21 KB
/
Server.cs
File metadata and controls
63 lines (57 loc) · 2.21 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
using WireMock;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Types;
using WireMock.Util;
namespace Octopus.OpenFeature.Provider.SpecificationTests;
public class Server : IDisposable
{
readonly WireMockServer _server;
readonly Dictionary<string, string> _responses = new();
public string Url => _server.Url!;
public Server()
{
_server = WireMockServer.Start();
_server
.Given(Request.Create().WithPath("/api/toggles/evaluations/v3/").UsingGet())
.RespondWith(Response.Create()
.WithTransformer()
.WithCallback(req =>
{
var authHeader = req.Headers?["Authorization"]?.FirstOrDefault();
if (authHeader != null && authHeader.StartsWith("Bearer "))
{
var token = authHeader["Bearer ".Length..];
if (_responses.TryGetValue(token, out var responseBody))
{
return new ResponseMessage
{
StatusCode = 200,
Headers = new Dictionary<string, WireMock.Types.WireMockList<string>>
{
["Content-Type"] = new WireMock.Types.WireMockList<string>("application/json"),
["ContentHash"] = new WireMock.Types.WireMockList<string>(Convert.ToBase64String([0x01]))
},
BodyData = new BodyData
{
BodyAsString = responseBody,
DetectedBodyType = BodyType.String
}
};
}
}
return new ResponseMessage { StatusCode = 401 };
}));
}
public string Configure(string json)
{
var token = Guid.NewGuid().ToString();
_responses[token] = json;
return token;
}
public void Dispose()
{
_server.Dispose();
}
}