forked from NancyFx/Nancy
/
NancyOwinHostFixture.cs
331 lines (272 loc) · 14.5 KB
/
NancyOwinHostFixture.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
namespace Nancy.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Bootstrapper;
using FakeItEasy;
using Hosting.Owin;
using Hosting.Owin.Tests.Fakes;
using Xunit;
using BodyDelegate = System.Func<System.Func<System.ArraySegment<byte>, // data
System.Action, // continuation
bool>, // continuation will be invoked
System.Action<System.Exception>, // onError
System.Action, // on Complete
System.Action>; // cancel
using ResponseCallBack = System.Action<string, System.Collections.Generic.IDictionary<string, string>, System.Func<System.Func<System.ArraySegment<byte>, System.Action, bool>, System.Action<System.Exception>, System.Action, System.Action>>;
public class NancyOwinHostFixture
{
private readonly NancyOwinHost host;
private readonly ResponseCallBack fakeResponseCallback;
private readonly Action<Exception> fakeErrorCallback;
private readonly Dictionary<string, object> environment;
private readonly INancyEngine fakeEngine;
private readonly INancyBootstrapper fakeBootstrapper;
public NancyOwinHostFixture()
{
this.fakeEngine = A.Fake<INancyEngine>();
this.fakeBootstrapper = A.Fake<INancyBootstrapper>();
A.CallTo(() => this.fakeBootstrapper.GetEngine()).Returns(this.fakeEngine);
this.host = new NancyOwinHost(fakeBootstrapper);
this.fakeResponseCallback = (status, headers, bodyDelegate) => { };
this.fakeErrorCallback = (ex) => { };
this.environment = new Dictionary<string, object>()
{
{ "owin.RequestMethod", "GET" },
{ "owin.RequestPath", "/test" },
{ "owin.RequestPathBase", "/root" },
{ "owin.RequestQueryString", "var=value" },
{ "owin.RequestHeaders", new Dictionary<string, string> { { "Host", "testserver" } } },
{ "owin.RequestBody", null },
{ "owin.RequestScheme", "http" },
{ "owin.Version", "1.0" }
};
}
[Fact]
public void Should_throw_if_owin_version_is_incorrect()
{
this.environment["owin.Version"] = "1.2";
var result = Record.Exception(
() => this.host.ProcessRequest(environment, fakeResponseCallback, fakeErrorCallback));
result.ShouldBeOfType(typeof(InvalidOperationException));
}
[Fact]
public void Should_immediately_invoke_nancy_if_no_request_body_delegate()
{
this.host.ProcessRequest(environment, fakeResponseCallback, fakeErrorCallback);
A.CallTo(() => this.fakeEngine.HandleRequest(A<Request>.Ignored, A<Action<NancyContext>>.Ignored, A<Action<Exception>>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
}
[Fact]
public void Should_invoke_request_body_delegate_if_one_exists()
{
var invoked = false;
BodyDelegate bodyDelegate = (onNext, onError, onComplete) => { invoked = true; return () => { }; };
this.environment["owin.RequestBody"] = bodyDelegate;
this.host.ProcessRequest(environment, fakeResponseCallback, fakeErrorCallback);
invoked.ShouldBeTrue();
}
[Fact]
public void Should_invoke_nancy_on_request_body_delegate_oncomplete()
{
Action complete = null;
BodyDelegate bodyDelegate = (onNext, onError, onComplete) => { complete = onComplete; return () => { }; };
this.environment["owin.RequestBody"] = bodyDelegate;
this.host.ProcessRequest(environment, fakeResponseCallback, fakeErrorCallback);
complete.Invoke();
A.CallTo(() => this.fakeEngine.HandleRequest(A<Request>.Ignored, A<Action<NancyContext>>.Ignored, A<Action<Exception>>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
}
[Fact]
public void Should_invoke_error_callback_if_nancy_invoke_throws()
{
var called = false;
A.CallTo(() => this.fakeEngine.HandleRequest(A<Request>.Ignored, A<Action<NancyContext>>.Ignored, A<Action<Exception>>.Ignored)).Throws(new NotSupportedException());
this.host.ProcessRequest(environment, fakeResponseCallback, (e) => called = true);
called.ShouldBeTrue();
}
[Fact]
public void Should_invoke_response_delegate_when_nancy_returns()
{
var fakeResponse = new Response() { StatusCode = HttpStatusCode.OK };
var fakeContext = new NancyContext() { Response = fakeResponse };
this.SetupFakeNancyCompleteCallback(fakeContext);
var called = false;
ResponseCallBack callback = (status, headers, bodyDelegate) => called = true;
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
called.ShouldBeTrue();
}
[Fact]
public void Should_invoke_view_delegate_to_get_response()
{
var called = false;
var fakeResponse = new Response()
{
StatusCode = HttpStatusCode.OK,
Contents = s => called = true
};
var fakeContext = new NancyContext() { Response = fakeResponse };
this.SetupFakeNancyCompleteCallback(fakeContext);
var fakeConsumer = new FakeConsumer(false);
ResponseCallBack callback = (r, h, b) => fakeConsumer.InvokeBodyDelegate(b);
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
called.ShouldBeTrue();
}
[Fact]
public void Should_set_return_code_in_response_callback()
{
var fakeResponse = new Response()
{
StatusCode = HttpStatusCode.OK,
Contents = s => { }
};
var fakeContext = new NancyContext() { Response = fakeResponse };
this.SetupFakeNancyCompleteCallback(fakeContext);
string statusCode = "";
ResponseCallBack callback = (r, h, b) => statusCode = r;
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
statusCode.ShouldEqual("200 OK");
}
[Fact]
public void Should_set_headers_in_response_callback()
{
var fakeResponse = new Response()
{
StatusCode = HttpStatusCode.OK,
Headers = new Dictionary<string, string>() { { "TestHeader", "TestValue" } },
Contents = s => { }
};
var fakeContext = new NancyContext() { Response = fakeResponse };
this.SetupFakeNancyCompleteCallback(fakeContext);
IDictionary<string, string> headers = null;
ResponseCallBack callback = (r, h, b) => headers = h;
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
headers.Count.ShouldEqual(1);
headers["TestHeader"].ShouldEqual("TestValue");
}
[Fact]
public void Should_send_null_continuation()
{
var fakeResponse = new Response()
{
StatusCode = HttpStatusCode.OK,
Contents = s => s.WriteByte(12)
};
var fakeContext = new NancyContext() { Response = fakeResponse };
this.SetupFakeNancyCompleteCallback(fakeContext);
var fakeConsumer = new FakeConsumer(false);
ResponseCallBack callback = (r, h, b) => fakeConsumer.InvokeBodyDelegate(b);
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
fakeConsumer.ContinuationSent.ShouldBeFalse();
}
[Fact]
public void Should_send_entire_body()
{
var data1 = Encoding.ASCII.GetBytes("Some content");
var data2 = Encoding.ASCII.GetBytes("Some more content");
var fakeResponse = new Response()
{
StatusCode = HttpStatusCode.OK,
Contents = s =>
{
s.Write(data1, 0, data1.Length);
s.Write(data2, 0, data2.Length);
}
};
var fakeContext = new NancyContext() { Response = fakeResponse };
this.SetupFakeNancyCompleteCallback(fakeContext);
var fakeConsumer = new FakeConsumer(false);
ResponseCallBack callback = (r, h, b) => fakeConsumer.InvokeBodyDelegate(b);
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
fakeConsumer.ConsumedData.SequenceEqual(data1.Concat(data2)).ShouldBeTrue();
}
[Fact]
public void Should_dispose_context_on_completion_of_body_delegate()
{
var data1 = Encoding.ASCII.GetBytes("Some content");
var fakeResponse = new Response()
{
StatusCode = HttpStatusCode.OK,
Contents = s => s.Write(data1, 0, data1.Length)
};
var fakeContext = new NancyContext() { Response = fakeResponse };
var mockDisposable = A.Fake<IDisposable>();
fakeContext.Items.Add("Test", mockDisposable);
this.SetupFakeNancyCompleteCallback(fakeContext);
var fakeConsumer = new FakeConsumer(false);
ResponseCallBack callback = (r, h, b) => fakeConsumer.InvokeBodyDelegate(b);
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
A.CallTo(() => mockDisposable.Dispose()).MustHaveHappened(Repeated.Exactly.Once);
}
[Fact]
public void Should_dispose_context_if_body_delegate_throws()
{
var fakeResponse = new Response()
{
StatusCode = HttpStatusCode.OK,
Contents = s => { throw new InvalidOperationException(); }
};
var fakeContext = new NancyContext() { Response = fakeResponse };
var mockDisposable = A.Fake<IDisposable>();
fakeContext.Items.Add("Test", mockDisposable);
this.SetupFakeNancyCompleteCallback(fakeContext);
var fakeConsumer = new FakeConsumer(false);
ResponseCallBack callback = (r, h, b) => fakeConsumer.InvokeBodyDelegate(b);
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
A.CallTo(() => mockDisposable.Dispose()).MustHaveHappened(Repeated.Exactly.Once);
}
[Fact]
public void Should_read_entire_request_body_when_theres_no_continuation()
{
var requestBody = Encoding.ASCII.GetBytes("This is some request body content");
var fakeRequestBodyDelegate = new FakeProducer(false, requestBody, 5, false);
this.environment["owin.RequestBody"] = (BodyDelegate)fakeRequestBodyDelegate;
Request request = null;
A.CallTo(() => this.fakeEngine.HandleRequest(A<Request>.Ignored, A<Action<NancyContext>>.Ignored, A<Action<Exception>>.Ignored))
.Invokes(i => request = (Request)i.Arguments[0]);
this.host.ProcessRequest(environment, fakeResponseCallback, fakeErrorCallback);
fakeRequestBodyDelegate.SendAll();
var read = new StreamReader(request.Body);
var output = read.ReadToEnd();
output.ShouldEqual("This is some request body content");
}
[Fact]
public void Should_read_entire_request_body_when_there_is_a_continuation()
{
var requestBody = Encoding.ASCII.GetBytes("This is some request body content");
var fakeRequestBodyDelegate = new FakeProducer(true, requestBody, 5, false);
this.environment["owin.RequestBody"] = (BodyDelegate)fakeRequestBodyDelegate;
Request request = null;
A.CallTo(() => this.fakeEngine.HandleRequest(A<Request>.Ignored, A<Action<NancyContext>>.Ignored, A<Action<Exception>>.Ignored))
.Invokes(i => request = (Request)i.Arguments[0]);
this.host.ProcessRequest(environment, fakeResponseCallback, fakeErrorCallback);
fakeRequestBodyDelegate.SendAll();
var read = new StreamReader(request.Body);
var output = read.ReadToEnd();
output.ShouldEqual("This is some request body content");
}
[Fact]
public void Should_set_cookie_with_valid_header()
{
var fakeResponse = new Response() { StatusCode = HttpStatusCode.OK };
fakeResponse.AddCookie("test", "testvalue");
var fakeContext = new NancyContext() { Response = fakeResponse };
this.SetupFakeNancyCompleteCallback(fakeContext);
var respHeaders = new Dictionary<string, string>();
ResponseCallBack callback = (status, headers, bodyDelegate) =>respHeaders=(Dictionary<string,string>)headers;
this.host.ProcessRequest(environment, callback, fakeErrorCallback);
respHeaders.ContainsKey("Set-Cookie").ShouldBeTrue();
(respHeaders["Set-Cookie"]=="test=testvalue; path=/").ShouldBeTrue();
}
/// <summary>
/// Sets the fake nancy engine to execute the complete callback with the given context
/// </summary>
/// <param name="context">Context to return</param>
private void SetupFakeNancyCompleteCallback(NancyContext context)
{
A.CallTo(() => this.fakeEngine.HandleRequest(A<Request>.Ignored, A<Action<NancyContext>>.Ignored, A<Action<Exception>>.Ignored))
.Invokes((i => ((Action<NancyContext>)i.Arguments[1]).Invoke(context)));
}
}
}