summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Messaging/HttpRequestInfoTests.cs
blob: dc1f633fe0bddf0af2c6e723123cc29ce3902a55 (plain)
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
//-----------------------------------------------------------------------
// <copyright file="HttpRequestInfoTests.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Messaging {
	using System;
	using System.Collections.Specialized;
	using System.Web;
	using DotNetOpenAuth.Messaging;
	using NUnit.Framework;

	[TestFixture]
	public class HttpRequestInfoTests : TestBase {
		[TestCase]
		public void CtorDefault() {
			HttpRequestInfo info = new HttpRequestInfo();
			Assert.AreEqual("GET", info.HttpMethod);
		}

		[TestCase]
		public void CtorRequest() {
			HttpRequest request = new HttpRequest("file", "http://someserver?a=b", "a=b");
			////request.Headers["headername"] = "headervalue"; // PlatformNotSupportedException prevents us mocking this up
			HttpRequestInfo info = new HttpRequestInfo(request);
			Assert.AreEqual(request.Headers["headername"], info.Headers["headername"]);
			Assert.AreEqual(request.Url.Query, info.Query);
			Assert.AreEqual(request.QueryString["a"], info.QueryString["a"]);
			Assert.AreEqual(request.Url, info.Url);
			Assert.AreEqual(request.Url, info.UrlBeforeRewriting);
			Assert.AreEqual(request.HttpMethod, info.HttpMethod);
		}

		// All these tests are ineffective because ServerVariables[] cannot be set.
		////[TestCase]
		////public void CtorRequestWithDifferentPublicHttpHost() {
		////    HttpRequest request = new HttpRequest("file", "http://someserver?a=b", "a=b");
		////    request.ServerVariables["HTTP_HOST"] = "publichost";
		////    HttpRequestInfo info = new HttpRequestInfo(request);
		////    Assert.AreEqual("publichost", info.UrlBeforeRewriting.Host);
		////    Assert.AreEqual(80, info.UrlBeforeRewriting.Port);
		////    Assert.AreEqual(request.Url.Query, info.Query);
		////    Assert.AreEqual(request.QueryString["a"], info.QueryString["a"]);
		////}

		////[TestCase]
		////public void CtorRequestWithDifferentPublicHttpsHost() {
		////    HttpRequest request = new HttpRequest("file", "https://someserver?a=b", "a=b");
		////    request.ServerVariables["HTTP_HOST"] = "publichost";
		////    HttpRequestInfo info = new HttpRequestInfo(request);
		////    Assert.AreEqual("publichost", info.UrlBeforeRewriting.Host);
		////    Assert.AreEqual(443, info.UrlBeforeRewriting.Port);
		////    Assert.AreEqual(request.Url.Query, info.Query);
		////    Assert.AreEqual(request.QueryString["a"], info.QueryString["a"]);
		////}

		////[TestCase]
		////public void CtorRequestWithDifferentPublicHostNonstandardPort() {
		////    HttpRequest request = new HttpRequest("file", "http://someserver?a=b", "a=b");
		////    request.ServerVariables["HTTP_HOST"] = "publichost:550";
		////    HttpRequestInfo info = new HttpRequestInfo(request);
		////    Assert.AreEqual("publichost", info.UrlBeforeRewriting.Host);
		////    Assert.AreEqual(550, info.UrlBeforeRewriting.Port);
		////    Assert.AreEqual(request.Url.Query, info.Query);
		////    Assert.AreEqual(request.QueryString["a"], info.QueryString["a"]);
		////}

		////[TestCase]
		////public void CtorRequestWithDifferentPublicIPv6Host() {
		////    HttpRequest request = new HttpRequest("file", "http://[fe80::587e:c6e5:d3aa:657a]:8089/v3.1/", "");
		////    request.ServerVariables["HTTP_HOST"] = "[fe80::587e:c6e5:d3aa:657b]:8089";
		////    HttpRequestInfo info = new HttpRequestInfo(request);
		////    Assert.AreEqual("[fe80::587e:c6e5:d3aa:657b]", info.UrlBeforeRewriting.Host);
		////    Assert.AreEqual(8089, info.UrlBeforeRewriting.Port);
		////    Assert.AreEqual(request.Url.Query, info.Query);
		////}

		/// <summary>
		/// Checks that a property dependent on another null property
		/// doesn't generate a NullReferenceException.
		/// </summary>
		[TestCase]
		public void QueryBeforeSettingUrl() {
			HttpRequestInfo info = new HttpRequestInfo();
			Assert.IsNull(info.Query);
		}

		/// <summary>
		/// Verifies that looking up a querystring variable is gracefully handled without a query in the URL.
		/// </summary>
		[TestCase]
		public void QueryStringLookupWithoutQuery() {
			HttpRequestInfo info = new HttpRequestInfo();
			Assert.IsNull(info.QueryString["hi"]);
		}

		/// <summary>
		/// Verifies SSL forwarders are correctly handled when they supply X_FORWARDED_PROTO and HOST
		/// </summary>
		[TestCase]
		public void GetPublicFacingUrlSSLForwarder1() {
			HttpRequest req = new HttpRequest("a.aspx", "http://someinternalhost/a.aspx?a=b", "a=b");
			var serverVariables = new NameValueCollection();
			serverVariables["HTTP_X_FORWARDED_PROTO"] = "https";
			serverVariables["HTTP_HOST"] = "somehost";
			Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables);
			Uri expected = new Uri("https://somehost/a.aspx?a=b");
			Assert.AreEqual(expected, actual);
		}

		/// <summary>
		/// Verifies SSL forwarders are correctly handled when they supply X_FORWARDED_PROTO and HOST:port
		/// </summary>
		[TestCase]
		public void GetPublicFacingUrlSSLForwarder2() {
			HttpRequest req = new HttpRequest("a.aspx", "http://someinternalhost/a.aspx?a=b", "a=b");
			var serverVariables = new NameValueCollection();
			serverVariables["HTTP_X_FORWARDED_PROTO"] = "https";
			serverVariables["HTTP_HOST"] = "somehost:999";
			Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables);
			Uri expected = new Uri("https://somehost:999/a.aspx?a=b");
			Assert.AreEqual(expected, actual);
		}

		/// <summary>
		/// Verifies SSL forwarders are correctly handled when they supply just HOST
		/// </summary>
		[TestCase]
		public void GetPublicFacingUrlSSLForwarder3() {
			HttpRequest req = new HttpRequest("a.aspx", "http://someinternalhost/a.aspx?a=b", "a=b");
			var serverVariables = new NameValueCollection();
			serverVariables["HTTP_HOST"] = "somehost";
			Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables);
			Uri expected = new Uri("http://somehost/a.aspx?a=b");
			Assert.AreEqual(expected, actual);
		}

		/// <summary>
		/// Verifies SSL forwarders are correctly handled when they supply just HOST:port
		/// </summary>
		[TestCase]
		public void GetPublicFacingUrlSSLForwarder4() {
			HttpRequest req = new HttpRequest("a.aspx", "http://someinternalhost/a.aspx?a=b", "a=b");
			var serverVariables = new NameValueCollection();
			serverVariables["HTTP_HOST"] = "somehost:79";
			Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables);
			Uri expected = new Uri("http://somehost:79/a.aspx?a=b");
			Assert.AreEqual(expected, actual);
		}
	}
}