forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestContextTests.cs
More file actions
152 lines (127 loc) · 3.9 KB
/
Copy pathRequestContextTests.cs
File metadata and controls
152 lines (127 loc) · 3.9 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Funq;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.Text;
using ServiceStack.Web;
using ServiceStack.WebHost.Endpoints.Tests.Support.Services;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[TestFixture]
public class RequestContextTests
{
private const string ListeningOn = "http://localhost:1337/";
public class HeadersAppHostHttpListener
: AppHostHttpListenerBase
{
public HeadersAppHostHttpListener()
: base("Request Filters Tests", typeof(HeadersService).Assembly) { }
public override void Configure(Container container)
{
HostContext.Config.GlobalResponseHeaders.Clear();
//Signal advanced web browsers what HTTP Methods you accept
base.SetConfig(new HostConfig
{
GlobalResponseHeaders =
{
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
},
});
this.GlobalRequestFilters.Add((req, res, dto) =>
{
var requestFilter = dto as RequestFilter;
if (requestFilter != null)
{
res.StatusCode = requestFilter.StatusCode;
if (!requestFilter.HeaderName.IsNullOrEmpty())
{
res.AddHeader(requestFilter.HeaderName, requestFilter.HeaderValue);
}
}
});
}
}
HeadersAppHostHttpListener appHost;
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
appHost = new HeadersAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
}
public static Dictionary<string, string> GetResponseHeaders(String url)
{
var webRequest = (HttpWebRequest)WebRequest.Create(url);
var webResponse = webRequest.GetResponse();
var map = new Dictionary<string, string>();
for (var i = 0; i < webResponse.Headers.Count; i++)
{
var header = webResponse.Headers.Keys[i];
map[header] = webResponse.Headers[header];
}
return map;
}
[Test]
public void Can_resolve_CustomHeader()
{
var webRequest = (HttpWebRequest)WebRequest.Create(
ListeningOn + "json/reply/Headers?Name=X-CustomHeader");
webRequest.Headers["X-CustomHeader"] = "CustomValue";
var response = JsonSerializer.DeserializeFromStream<HeadersResponse>(
webRequest.GetResponse().GetResponseStream());
Assert.That(response.Value, Is.EqualTo("CustomValue"));
}
[Test]
public void Does_Send_Global_Headers()
{
var headers = GetResponseHeaders(ListeningOn + "json/reply/Headers");
Assert.That(headers["Access-Control-Allow-Origin"], Is.EqualTo("*"));
Assert.That(headers["Access-Control-Allow-Methods"], Is.EqualTo("GET, POST, PUT, DELETE, OPTIONS"));
}
[Test]
public void Does_return_bare_401_StatusCode()
{
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(
ListeningOn + "json/reply/RequestFilter?StatusCode=401");
webRequest.GetResponse();
Assert.Fail("Should throw 401 WebException");
}
catch (WebException ex)
{
var httpResponse = (HttpWebResponse)ex.Response;
Assert.That(httpResponse.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
}
}
[Test]
public void Does_return_bare_401_with_AuthRequired_header()
{
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(ListeningOn
+ "json/reply/RequestFilter?StatusCode=401"
+ "&HeaderName=" + HttpHeaders.WwwAuthenticate
+ "&HeaderValue=" + "Basic realm=\"Auth Required\"".UrlEncode());
webRequest.GetResponse();
Assert.Fail("Should throw 401 WebException");
}
catch (WebException ex)
{
var httpResponse = (HttpWebResponse)ex.Response;
Assert.That(httpResponse.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
Assert.That(ex.Response.Headers[HttpHeaders.WwwAuthenticate],
Is.EqualTo("Basic realm=\"Auth Required\""));
}
}
}
}