forked from aws-samples/serverless-test-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiRequestBuilder.cs
66 lines (57 loc) · 1.87 KB
/
ApiRequestBuilder.cs
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
using Amazon.Lambda.APIGatewayEvents;
using System.Text.Json;
namespace ApiTests.UnitTest;
public class ApiRequestBuilder
{
private readonly List<string> _path = new() { "dev" };
private string? _body;
private HttpMethod? _httpMethod;
private Dictionary<string, string>? _pathParams;
private Dictionary<string, string>? _headers;
public ApiRequestBuilder WithPathParameter(string name, string value)
{
_pathParams ??= new(StringComparer.OrdinalIgnoreCase);
_pathParams.Add(name, value);
_path.Add(value);
return this;
}
public ApiRequestBuilder WithHttpMethod(string httpMethod) =>
WithHttpMethod(new HttpMethod(httpMethod));
public ApiRequestBuilder WithHttpMethod(HttpMethod httpMethod)
{
_httpMethod = httpMethod;
return this;
}
public ApiRequestBuilder WithBody(string body)
{
_body = body;
return this;
}
public ApiRequestBuilder WithBody<T>(T body, JsonSerializerOptions? jsonOptions = default)
{
_body = JsonSerializer.Serialize(body, jsonOptions);
return this;
}
public ApiRequestBuilder WithHeader(string name, string value)
{
_headers ??= new(StringComparer.OrdinalIgnoreCase);
_headers[name] = value;
return this;
}
public APIGatewayHttpApiV2ProxyRequest Build() =>
new()
{
RequestContext = new APIGatewayHttpApiV2ProxyRequest.ProxyRequestContext()
{
DomainName = "localhost",
Http = new APIGatewayHttpApiV2ProxyRequest.HttpDescription()
{
Method = (_httpMethod ?? HttpMethod.Get).Method,
Path = string.Join('/', _path),
}
},
PathParameters = _pathParams,
Body = _body,
Headers = _headers,
};
}