-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathHttpExtensions.cs
More file actions
202 lines (162 loc) · 7.89 KB
/
HttpExtensions.cs
File metadata and controls
202 lines (162 loc) · 7.89 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
namespace ServiceControl.AcceptanceTesting
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
public static class HttpExtensions
{
public static async Task Put<T>(this IAcceptanceTestInfrastructureProvider provider, string url, T payload = null, Func<HttpStatusCode, bool> requestHasFailed = null) where T : class
{
requestHasFailed ??= statusCode => statusCode is not HttpStatusCode.OK and not HttpStatusCode.Accepted;
var httpClient = provider.HttpClient;
var response = await httpClient.PutAsJsonAsync(url, payload, provider.SerializerOptions);
if (requestHasFailed(response.StatusCode))
{
throw new Exception($"Expected status code not received, instead got {response.StatusCode}.");
}
}
public static Task<HttpResponseMessage> GetRaw(this IAcceptanceTestInfrastructureProvider provider, string url)
{
var httpClient = provider.HttpClient;
return httpClient.GetAsync(url);
}
public static Task<HttpResponseMessage> Options(this IAcceptanceTestInfrastructureProvider provider, string url)
{
var httpClient = provider.HttpClient;
var request = new HttpRequestMessage(HttpMethod.Options, url);
return httpClient.SendAsync(request);
}
public static async Task<ManyResult<T>> TryGetMany<T>(this IAcceptanceTestInfrastructureProvider provider, string url, Predicate<T> condition = null) where T : class
{
condition ??= _ => true;
var response = await provider.GetInternal<List<T>>(url);
if (response == null || !response.Any(m => condition(m)))
{
return ManyResult<T>.Empty;
}
return ManyResult<T>.New(true, response.Where(m => condition(m)).ToList());
}
public static async Task<HttpStatusCode> Patch<T>(this IAcceptanceTestInfrastructureProvider provider, string url, T payload = null) where T : class
{
var httpClient = provider.HttpClient;
var response = await httpClient.PatchAsJsonAsync(url, payload, provider.SerializerOptions);
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();
throw new InvalidOperationException($"Call failed: {(int)response.StatusCode} - {response.ReasonPhrase} - {body}");
}
return response.StatusCode;
}
public static async Task<SingleResult<T>> TryGet<T>(this IAcceptanceTestInfrastructureProvider provider, string url, Predicate<T> condition = null) where T : class
{
condition ??= _ => true;
var response = await provider.GetInternal<T>(url);
if (response == null || !condition(response))
{
return SingleResult<T>.Empty;
}
return SingleResult<T>.New(response);
}
public static async Task<SingleResult<T>> TryGet<T>(this IAcceptanceTestInfrastructureProvider provider, string url, Func<T, Task<bool>> condition) where T : class
{
var response = await provider.GetInternal<T>(url);
if (response == null || !await condition(response))
{
return SingleResult<T>.Empty;
}
return SingleResult<T>.New(response);
}
public static async Task<SingleResult<T>> TryGetSingle<T>(this IAcceptanceTestInfrastructureProvider provider, string url, Predicate<T> condition = null) where T : class
{
condition ??= _ => true;
var response = await provider.GetInternal<List<T>>(url);
T item = null;
if (response != null)
{
var items = response.Where(i => condition(i)).ToList();
if (items.Count > 1)
{
throw new InvalidOperationException("More than one matching element found");
}
item = items.SingleOrDefault();
}
if (item != null)
{
return SingleResult<T>.New(item);
}
return SingleResult<T>.Empty;
}
public static async Task<HttpStatusCode> Get(this IAcceptanceTestInfrastructureProvider provider, string url)
{
var httpClient = provider.HttpClient;
var response = await httpClient.GetAsync(url);
return response.StatusCode;
}
public static async Task Post<T>(this IAcceptanceTestInfrastructureProvider provider, string url, T payload = null, Func<HttpStatusCode, bool> requestHasFailed = null) where T : class
{
var httpClient = provider.HttpClient;
var response = await httpClient.PostAsJsonAsync(url, payload, provider.SerializerOptions);
if (requestHasFailed != null)
{
if (requestHasFailed(response.StatusCode))
{
throw new Exception($"Expected status code not received, instead got {response.StatusCode}.");
}
return;
}
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();
throw new InvalidOperationException($"Call failed: {(int)response.StatusCode} - {response.ReasonPhrase} - {body}");
}
}
public static async Task Delete(this IAcceptanceTestInfrastructureProvider provider, string url)
{
var httpClient = provider.HttpClient;
var response = await httpClient.DeleteAsync(url);
if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();
throw new InvalidOperationException($"Call failed: {(int)response.StatusCode} - {response.ReasonPhrase} - {body}");
}
}
public static async Task<byte[]> DownloadData(this IAcceptanceTestInfrastructureProvider provider, string url, HttpStatusCode successCode = HttpStatusCode.OK)
{
var httpClient = provider.HttpClient;
var response = await httpClient.GetAsync(url);
if (response.StatusCode != successCode)
{
throw new Exception($"Expected status code of {successCode}, but instead got {response.StatusCode}.");
}
return await response.Content.ReadAsByteArrayAsync();
}
static async Task<T> GetInternal<T>(this IAcceptanceTestInfrastructureProvider provider, string url) where T : class
{
var response = await provider.GetRaw(url);
//for now
if (response.StatusCode is HttpStatusCode.NotFound or HttpStatusCode.NoContent or HttpStatusCode.ServiceUnavailable)
{
LogRequest();
return null;
}
if (response.StatusCode != HttpStatusCode.OK)
{
var content = await response.Content.ReadAsStringAsync();
LogRequest(response.ReasonPhrase + content);
throw new InvalidOperationException($"Call failed: {(int)response.StatusCode} - {response.ReasonPhrase} {Environment.NewLine} {content}");
}
var payload = await response.Content.ReadFromJsonAsync<T>(provider.SerializerOptions);
LogRequest();
return payload;
void LogRequest(string additionalInfo = null)
{
var additionalInfoString = additionalInfo != null ? ": " + additionalInfo : string.Empty;
Console.WriteLine($"{response.RequestMessage.Method} - {url} - {(int)response.StatusCode}{additionalInfoString}");
}
}
}
}