This repository was archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathNancySelfHostFixture.cs
333 lines (275 loc) · 10.5 KB
/
NancySelfHostFixture.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#if !MONO
namespace Nancy.Hosting.Self.Tests
{
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using FakeItEasy;
using Nancy.Bootstrapper;
using Nancy.Tests;
using Nancy.Tests.xUnitExtensions;
using Xunit;
/// <remarks>
/// These tests attempt to listen on port 1234, and so require either administrative
/// privileges or that a command similar to the following has been run with
/// administrative privileges:
/// <code>netsh http add urlacl url=http://+:1234/base user=DOMAIN\user</code>
/// See http://msdn.microsoft.com/en-us/library/ms733768.aspx for more information.
/// </remarks>
public class NancySelfHostFixture
{
private static readonly Uri BaseUri = new Uri("http://localhost:1234/base/");
[SkippableFact]
public void Should_be_get_an_exception_indicating_a_conflict_when_trying_to_listen_on_a_used_prefix()
{
Exception ex;
// Given
using (CreateAndOpenSelfHost())
{
// When
ex = Record.Exception(() =>
{
using (var host = new NancyHost(BaseUri))
{
host.Start();
}
});
}
// Then
ex.Message.ShouldContain("conflict");
}
[SkippableFact]
public void Should_be_able_to_get_any_header_from_selfhost()
{
// Given
using (CreateAndOpenSelfHost())
{
// When
var request = WebRequest.Create(new Uri(BaseUri, "rel/header/?query=value"));
request.Method = "GET";
// Then
request.GetResponse().Headers["X-Some-Header"].ShouldEqual("Some value");
}
}
[SkippableFact]
public void Should_set_query_string_and_uri_correctly()
{
// Given
Request nancyRequest = null;
var fakeEngine = A.Fake<INancyEngine>();
A.CallTo(() => fakeEngine.HandleRequest(A<Request>.Ignored, A<Func<NancyContext, NancyContext>>.Ignored,A<CancellationToken>.Ignored))
.Invokes(f => nancyRequest = (Request)f.Arguments[0])
.ReturnsLazily(c => Task.FromResult(new NancyContext { Request = (Request)c.Arguments[0], Response = new Response() }));
var fakeBootstrapper = A.Fake<INancyBootstrapper>();
A.CallTo(() => fakeBootstrapper.GetEngine()).Returns(fakeEngine);
// When
using (CreateAndOpenSelfHost(fakeBootstrapper))
{
var request = WebRequest.Create(new Uri(BaseUri, "test/stuff?query=value&query2=value2"));
request.Method = "GET";
try
{
request.GetResponse();
}
catch (WebException)
{
// Will throw because it returns 404 - don't care.
}
}
// Then
nancyRequest.Path.ShouldEqual("/test/stuff");
Assert.True(nancyRequest.Query.query.HasValue);
Assert.True(nancyRequest.Query.query2.HasValue);
}
[SkippableFact]
public void Should_be_able_to_get_from_selfhost()
{
using (CreateAndOpenSelfHost())
{
var reader =
new StreamReader(WebRequest.Create(new Uri(BaseUri, "rel")).GetResponse().GetResponseStream());
var response = reader.ReadToEnd();
response.ShouldEqual("This is the site route");
}
}
[SkippableFact]
public void Should_be_able_to_get_from_chunked_selfhost()
{
using (CreateAndOpenSelfHost())
{
var response = WebRequest.Create(new Uri(BaseUri, "rel")).GetResponse();
Assert.Equal("chunked", response.Headers["Transfer-Encoding"]);
Assert.Null(response.Headers["Content-Length"]);
using (var reader = new StreamReader(response.GetResponseStream()))
{
var contents = reader.ReadToEnd();
contents.ShouldEqual("This is the site route");
}
}
}
[SkippableFact]
public void Should_be_able_to_get_from_contentlength_selfhost()
{
HostConfiguration configuration = new HostConfiguration()
{
AllowChunkedEncoding = false
};
using (CreateAndOpenSelfHost(null, configuration))
{
var response = WebRequest.Create(new Uri(BaseUri, "rel")).GetResponse();
Assert.Null(response.Headers["Transfer-Encoding"]);
Assert.Equal(22, Convert.ToInt32(response.Headers["Content-Length"]));
using (var reader = new StreamReader(response.GetResponseStream()))
{
var contents = reader.ReadToEnd();
contents.ShouldEqual("This is the site route");
}
}
}
[SkippableFact]
public void Should_be_able_to_post_body_to_selfhost()
{
using (CreateAndOpenSelfHost())
{
const string testBody = "This is the body of the request";
var request =
WebRequest.Create(new Uri(BaseUri, "rel"));
request.Method = "POST";
var writer =
new StreamWriter(request.GetRequestStream()) { AutoFlush = true };
writer.Write(testBody);
var responseBody =
new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
responseBody.ShouldEqual(testBody);
}
}
[SkippableFact]
public void Should_be_able_to_get_from_selfhost_with_slashless_uri()
{
using (CreateAndOpenSelfHost())
{
var reader =
new StreamReader(WebRequest.Create(BaseUri.ToString().TrimEnd('/')).GetResponse().GetResponseStream());
var response = reader.ReadToEnd();
response.ShouldEqual("This is the site home");
}
}
private static NancyHostWrapper CreateAndOpenSelfHost(INancyBootstrapper nancyBootstrapper = null, HostConfiguration configuration = null)
{
if (nancyBootstrapper == null)
{
nancyBootstrapper = new DefaultNancyBootstrapper();
}
var host = new NancyHost(
nancyBootstrapper,
configuration,
BaseUri);
try
{
host.Start();
}
catch
{
throw new SkipException("Skipped due to no Administrator access - please see test fixture for more information.");
}
return new NancyHostWrapper(host);
}
[SkippableFact]
public void Should_be_able_to_recover_from_rendering_exception()
{
using (CreateAndOpenSelfHost())
{
var reader =
new StreamReader(WebRequest.Create(new Uri(BaseUri, "exception")).GetResponse().GetResponseStream());
var response = reader.ReadToEnd();
response.ShouldEqual("Content");
}
}
[SkippableFact]
public void Should_be_serializable()
{
var type = typeof(NancyHost);
Assert.True(type.Attributes.ToString().Contains("Serializable"));
}
[Fact]
public void Should_include_default_port_in_uri_prefixes()
{
// Given
var host = new NancyHost(new Uri("http://localhost/"));
// When
var prefix = host.GetPrefixes().Single();
// Then
prefix.ShouldEqual("http://+:80/");
}
[Fact]
public void Should_use_strong_wildcard_in_url_prefixes_by_default()
{
// Given
var host = new NancyHost(
new HostConfiguration(),
new Uri("http://localhost/")
);
// When
var prefix = host.GetPrefixes().Single();
// Then
prefix.ShouldStartWith("http://+:");
}
[Fact]
public void Should_use_strong_wildcard_in_url_prefixes_when_configured()
{
// Given
var host = new NancyHost(
new HostConfiguration() {
UseWeakWildcard = false,
},
new Uri("http://localhost/")
);
// When
var prefix = host.GetPrefixes().Single();
// Then
prefix.ShouldStartWith("http://+:");
}
[Fact]
public void Should_use_weak_wildcard_in_url_prefixes_when_configured()
{
// Given
var host = new NancyHost(
new HostConfiguration() {
UseWeakWildcard = true,
},
new Uri("http://localhost/")
);
// When
var prefix = host.GetPrefixes().Single();
// Then
prefix.ShouldStartWith("http://*:");
}
[Fact]
public void Should_not_throw_when_disposed_without_starting()
{
// Given
var bootstrapperMock = A.Fake<INancyBootstrapper>();
var host = new NancyHost(new Uri("http://localhost/"), bootstrapperMock);
// When
host.Dispose();
// Then
A.CallTo(() => bootstrapperMock.Dispose()).MustHaveHappened();
}
private class NancyHostWrapper : IDisposable
{
private readonly NancyHost host;
public NancyHostWrapper(NancyHost host)
{
this.host = host;
}
public void Dispose()
{
host.Stop();
}
}
}
}
#endif