forked from NancyFx/Nancy
/
DefaultRequestTraceFactoryFixture.cs
127 lines (109 loc) · 3.84 KB
/
DefaultRequestTraceFactoryFixture.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
namespace Nancy.Tests.Unit.Diagnostics
{
using System.Collections.Generic;
using System.IO;
using Nancy.Diagnostics;
using Nancy.IO;
using Nancy.Testing;
using Xunit;
public class DefaultRequestTraceFactoryFixture
{
private readonly DefaultRequestTraceFactory factory;
private readonly Request request;
public DefaultRequestTraceFactoryFixture()
{
this.factory = new DefaultRequestTraceFactory();
this.request = CreateRequest();
}
[Fact]
public void Should_create_default_trace_log_instance_when_error_tracing_is_activated()
{
using (new StaticConfigurationContext(x => x.DisableErrorTraces = false))
{
// Given
// When
var trace = this.factory.Create(this.request);
// Then
trace.TraceLog.ShouldNotBeNull();
trace.TraceLog.ShouldBeOfType<DefaultTraceLog>();
}
}
[Fact]
public void Should_create_null_trace_log_instance_when_error_tracing_is_deactivated()
{
using (new StaticConfigurationContext(x => x.DisableErrorTraces = true))
{
// Given
// When
var trace = this.factory.Create(this.request);
// Then
trace.TraceLog.ShouldNotBeNull();
trace.TraceLog.ShouldBeOfType<NullLog>();
}
}
[Fact]
public void Should_initialize_case_insensitive_items_collection_when_case_sensitivity_is_disabled()
{
using (new StaticConfigurationContext(x => x.CaseSensitive = false))
{
// Given
var trace = this.factory.Create(this.request);
// When
trace.Items.Add("foo", "bar");
// Then
Assert.DoesNotThrow(() =>
{
var value = trace.Items["FOO"];
});
}
}
[Fact]
public void Should_initialize_case_sensitive_items_collection_when_case_sensitivity_is_enabled()
{
using (new StaticConfigurationContext(x => x.CaseSensitive = true))
{
// Given
var trace = this.factory.Create(this.request);
// When
trace.Items.Add("foo", "bar");
// Then
Assert.Throws<KeyNotFoundException>(() =>
{
var value = trace.Items["FOO"];
});
}
}
[Fact]
public void Should_create_request_info_from_request()
{
// Given
// When
var trace = this.factory.Create(this.request);
// Then
trace.RequestData.ContentType.ShouldEqual(request.Headers.ContentType);
trace.RequestData.Headers.ShouldBeSameAs(request.Headers);
trace.RequestData.Method.ShouldEqual(request.Method);
trace.RequestData.Url.ShouldBeSameAs(request.Url);
}
[Fact]
public void Should_not_create_response_info()
{
// Given
// When
var trace = this.factory.Create(this.request);
// Then
trace.ResponseData.ShouldBeNull();
}
private static Request CreateRequest()
{
return new Request(
"GET",
new Url(),
RequestStream.FromStream(new MemoryStream()),
new Dictionary<string, IEnumerable<string>>
{
{"Content-Type", new[] {"text/plain"}}
});
}
}
}