-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrankTcpConnectionTest.cs
266 lines (230 loc) · 7.99 KB
/
FrankTcpConnectionTest.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Linq;
using System.Net;
using System.Text;
using FluentAssertions;
using Frank.API.WebDevelopers;
using Frank.API.WebDevelopers.DTO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using RestSharp;
using static Frank.API.WebDevelopers.DTO.ResponseConstructors;
// ReSharper disable PossibleNullReferenceException
namespace Frank.EndToEndTests
{
[NonParallelizable]
public class FrankTcpConnectionTest
{
private IWebApplicationBuilder _builder;
private IWebApplication _webApplication;
private IRestResponse _response;
private int _port;
private bool _expectExceptionInTeardown;
private void MakeGetRequest(string path)
{
var request = new RestRequest(path, Method.GET);
var client = new RestClient($"http://127.0.0.1:{_port}/");
_response = client.Execute(request);
}
private IRestResponse TheResponse()
{
if (_response.StatusCode == HttpStatusCode.InternalServerError)
{
throw new Exception(
Encoding.UTF8.GetString(_response.RawBytes)
);
}
return _response;
}
private JObject TheResponseBody()
{
return JsonConvert.DeserializeObject<JObject>(TheResponse().Content);
}
[SetUp]
public void SetUp()
{
_port = 8019;
_builder = Server.Configure();
_expectExceptionInTeardown = false;
}
[TearDown]
public void TearDown()
{
var thrown = false;
try
{
StopFrank();
}
catch (Exception)
{
if (!_expectExceptionInTeardown) throw;
thrown = true;
}
if (_expectExceptionInTeardown && !thrown)
{
throw new Exception("Expected an exception, but one was not thrown.");
}
}
private void StartFrank()
{
_webApplication = _builder.StartListeningOn(_port);
}
private void StartFrankWithRoutes(Action<IRouteConfigurer> action)
{
_builder.OnRequest(action);
StartFrank();
}
private void StopFrank()
{
_webApplication.Stop();
}
[Test]
public void CanServeNotFoundTwice()
{
StartFrank();
MakeGetRequest("/");
TheResponse().StatusCode.Should().Be(404);
MakeGetRequest("/another-route");
TheResponse().StatusCode.Should().Be(404);
}
[TestCase("/custom", 200, "Custom Body", "X-Header-Here", "A Value")]
[TestCase("/error", 500, "An error occurred", "X-Error", "Yes")]
public void ResponseContainsARawBodyStatusAndHeaders(
string routePath, int status, string body, string headerKey, string headerValue
)
{
_builder
.OnRequest(route =>
{
route.Get(routePath).To(() =>
{
return NewResponseWithStatus(status)
.BodyFromString(body)
.WithHeader(headerKey, headerValue);
});
});
StartFrank();
var response = new RestClient("http://127.0.0.1:8019/").Execute(
new RestRequest(routePath, Method.GET)
);
response.Content.Should().Be(body);
response.StatusCode.Should().Be(status);
response.Headers.FirstOrDefault(h => h.Name == headerKey).Value.Should().Be(headerValue);
}
[TestCase("/success")]
[TestCase("/okay")]
public void CanServeOk(string route)
{
StartFrankWithRoutes(router => { router.Get(route).To(Ok); });
MakeGetRequest(route);
TheResponse().StatusCode.Should().Be(200);
}
[Test]
public void CanServeACreatedStatusCode()
{
_port = 8090;
StartFrankWithRoutes(router => { router.Get("/created").To(Created); });
MakeGetRequest("/created");
TheResponse().StatusCode.Should().Be(201);
}
[Test]
public void CanSerializeJsonResponseIntoHttpBody()
{
StartFrankWithRoutes(
router =>
{
router.Get("/foo/2")
.To(() => Ok().WithJsonBody(new {Id = 2}));
}
);
MakeGetRequest("/foo/2");
TheResponse().StatusCode.Should().Be(200);
TheResponseBody()["Id"].Value<int>().Should().Be(2);
}
[Test]
public void CanDeserializeIncomingGetRequest()
{
Request? processedRequest = null;
StartFrankWithRoutes(
router =>
{
router.Get("/foo/2")
.To(request =>
{
processedRequest = request;
return Ok();
}
);
}
);
new RestClient("http://127.0.0.1:8019/").Execute(
new RestRequest("/foo/2", Method.GET)
.AddParameter("bar", "123")
.AddHeader("X-Api-Key", "1234supersecure")
);
processedRequest.Should().NotBeNull();
processedRequest?.Path.Should().Be("/foo/2");
processedRequest?.QueryParameters["bar"].Should().Be("123");
processedRequest?.Body.Should().Be("");
processedRequest?.Headers["x-api-key"].Should().Be("1234supersecure");
}
[Test]
public void CanDeserializeIncomingPostRequest()
{
Request? processedRequest = null;
StartFrankWithRoutes(
router =>
{
router.Post("/foo/2")
.To(request =>
{
processedRequest = request;
return Ok();
}
);
}
);
new RestClient("http://127.0.0.1:8019/").Execute(
new RestRequest("/foo/2", Method.POST)
.AddQueryParameter("bar", "123")
.AddHeader("X-Api-Key", "1234supersecure")
.AddJsonBody("This is the body!!")
);
processedRequest.Should().NotBeNull();
processedRequest?.Path.Should().Be("/foo/2");
processedRequest?.QueryParameters["bar"].Should().Be("123");
processedRequest?.Body.Should().Be("\"This is the body!!\"");
processedRequest?.Headers["x-api-key"].Should().Be("1234supersecure");
}
[Test]
public void CanExecuteBeforeAndAfterHandlers()
{
var customContext = new LifecycleHooksSpy();
_builder
.Before(() =>
{
customContext.Before();
return customContext;
})
.After(context => { context.After(); })
.OnRequest(((route, context) =>
{
context.Request();
route.Get("/").To(() =>
{
context.RouteHandler();
throw new Exception();
});
}));
StartFrank();
new RestClient("http://127.0.0.1:8019/").Execute(
new RestRequest("/", Method.GET)
);
customContext.OrderThatMethodsWereCalled.Should().ContainInOrder(
"before", "request", "route-handler", "after"
);
_expectExceptionInTeardown = true;
}
}
}