forked from Sustainsys/Saml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandResultHttpTests.cs
184 lines (150 loc) · 5.5 KB
/
CommandResultHttpTests.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
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
using Sustainsys.Saml2.HttpModule;
using Sustainsys.Saml2.Metadata;
using Sustainsys.Saml2.WebSso;
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Security;
namespace Sustainsys.Saml2.HttpModule.Tests
{
public partial class CommandResultHttpTests
{
[TestMethod]
public void CommandResultHttp_Apply_ChecksResponseNull()
{
Action a = () => new CommandResult().Apply(null);
a.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("response");
}
[TestMethod]
public void CommandResultHttp_Apply_NullShouldThrow()
{
CommandResult obj = null;
Action a = () => obj.Apply(null);
a.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("commandResult");
}
[TestMethod]
public void CommandResultHttp_Apply_HttpStatusCode()
{
var response = Substitute.For<HttpResponseBase>();
new CommandResult()
{
HttpStatusCode = HttpStatusCode.PaymentRequired
}.Apply(response);
response.Received().StatusCode = (int)HttpStatusCode.PaymentRequired;
}
[TestMethod]
public void CommandResultHttp_Apply_SetsCookie()
{
var response = Substitute.For<HttpResponseBase>();
new CommandResult()
{
SetCookieName = "CookieName",
SetCookieSecureFlag = true,
RequestState = new StoredRequestState(
new EntityId("http://idp.example.com"),
null,
null,
null)
}.Apply(response);
response.Received().SetCookie(
Arg.Is<HttpCookie>(c =>
c.Name == "CookieName"
&& c.Value.All(ch => ch != '/' && ch != '+' && ch != '=')
&& new StoredRequestState(DecryptCookieData(c.Value)).Idp.Id == "http://idp.example.com"
&& c.HttpOnly == true
&& c.Secure == true));
}
private byte[] DecryptCookieData(string data)
{
return MachineKey.Unprotect(
HttpRequestData.GetBinaryData(data),
"Sustainsys.Saml2");
}
[TestMethod]
public void CommandResultHttp_Apply_ClearsCookie()
{
var response = Substitute.For<HttpResponseBase>();
new CommandResult()
{
ClearCookieName = "CookieName",
}.Apply(response);
response.Received().SetCookie(
Arg.Is<HttpCookie>(c =>
c.Name == "CookieName"
&& c.Expires == new DateTime(1970, 01, 01)));
}
[TestMethod]
public void CommandResultHttp_Apply_Cacheability()
{
var cache = Substitute.For<HttpCachePolicyBase>();
var response = Substitute.For<HttpResponseBase>();
response.Cache.Returns(cache);
new CommandResult()
{
Cacheability = Cacheability.ServerAndNoCache
}.Apply(response);
cache.Received().SetCacheability(HttpCacheability.ServerAndNoCache);
}
[TestMethod]
public void CommandResultHttp_Apply_HandleRedirect()
{
var response = Substitute.For<HttpResponseBase>();
var redirectTarget = "http://example.com/redirect/target?X=A%20B%3d";
new CommandResult()
{
Location = new Uri(redirectTarget),
HttpStatusCode = HttpStatusCode.SeeOther
}.Apply(response);
response.Received().Redirect(redirectTarget);
}
[TestMethod]
public void CommandResultHttp_Apply_ThrowsOnMissingLocation()
{
var response = Substitute.For<HttpResponseBase>();
Action a = () =>
new CommandResult()
{
HttpStatusCode = HttpStatusCode.SeeOther
}.Apply(response);
a.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void CommandResultHttp_Apply_ThrowsOnLocationWithoutRedirect()
{
var response = Substitute.For<HttpResponseBase>();
Action a = () =>
new CommandResult()
{
Location = new Uri("http://example.com")
}.Apply(response);
a.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void CommandResultHttp_Apply_Content()
{
var response = Substitute.For<HttpResponseBase>();
var commandResult = new CommandResult
{
Content = "Some Content!",
ContentType = "text"
};
commandResult.Apply(response);
response.Received().ContentType = "text";
response.Received().Write("Some Content!");
}
[TestMethod]
public void CommandResultHttp_Apply_Headers()
{
var response = Substitute.For<HttpResponseBase>();
var commandResult = new CommandResult();
commandResult.Headers.Add("header", "value");
commandResult.Apply(response);
response.Received().AddHeader("header", "value");
}
}
}