root / trunk / hammock / src / net35 / Hammock / Web / Mocks / MockWebRequestFactory.cs @ 0eea575a
History | View | Annotate | Download (4.6 kB)
1 |
using System; |
---|---|
2 |
using System.Net; |
3 |
using System.Linq; |
4 |
using Hammock.Extensions; |
5 |
|
6 |
#if !SILVERLIGHT && !ClientProfiles && !MonoTouch |
7 |
#if !NETCF |
8 |
using System.Web; |
9 |
#endif |
10 |
using System.Collections.Specialized; |
11 |
#endif |
12 |
|
13 |
#if Silverlight |
14 |
using Hammock.Silverlight.Compat; |
15 |
#endif |
16 |
|
17 |
#if ClientProfiles || MonoTouch |
18 |
using System.Collections.Specialized; |
19 |
using System.Compat.Web; |
20 |
#endif |
21 |
|
22 |
#if SL3 || SL4 |
23 |
using System.Windows.Browser; |
24 |
#endif |
25 |
|
26 |
namespace Hammock.Web.Mocks |
27 |
{ |
28 |
internal class MockWebRequestFactory : IWebRequestCreate |
29 |
{ |
30 |
public const string MockScheme = "mockScheme"; |
31 |
public const string MockStatusCode = "mockStatusCode"; |
32 |
public const string MockStatusDescription = "mockStatusDescription"; |
33 |
public const string MockContent = "mockContent"; |
34 |
public const string MockContentType = "mockContentType"; |
35 |
public const string MockHeaderNames = "mockHeaderNames"; |
36 |
public const string MockHeaderValues = "mockHeaderValues"; |
37 |
public const string MockHttpMethod = "mockHttpMethod"; |
38 |
|
39 |
public WebRequest Create(Uri uri) |
40 |
{ |
41 |
#if !SILVERLIGHT && !MonoTouch && !NETCF |
42 |
var query = HttpUtility.ParseQueryString(uri.Query); |
43 |
#else |
44 |
var query = uri.Query.ParseQueryString(); |
45 |
#endif |
46 |
var scheme = query[MockScheme]; |
47 |
var statusCode = query[MockStatusCode]; |
48 |
var statusDescription = query[MockStatusDescription]; |
49 |
var content = query[MockContent]; |
50 |
var contentType = query[MockContentType]; |
51 |
var headerNames = query[MockHeaderNames]; |
52 |
var headerValues = query[MockHeaderValues]; |
53 |
|
54 |
// Remove mocks parameters |
55 |
var queryString = new NameValueCollection(); |
56 |
#if !SILVERLIGHT && !MonoTouch && !NETCF |
57 |
foreach(var key in query.AllKeys) |
58 |
#else |
59 |
foreach (var key in query.Keys) |
60 |
#endif |
61 |
{ |
62 |
if(key.EqualsAny( |
63 |
MockScheme, |
64 |
MockStatusCode, |
65 |
MockStatusDescription, |
66 |
MockContent, |
67 |
MockContentType, |
68 |
MockHeaderNames, |
69 |
MockHeaderValues, |
70 |
MockHttpMethod |
71 |
)) |
72 |
{ |
73 |
continue; |
74 |
} |
75 |
queryString.Add(key, query[key]); |
76 |
} |
77 |
|
78 |
// [DC] Silverlight does not have uri.Authority |
79 |
var uriQuery = queryString.ToQueryString(); |
80 |
var authority = "{0}{1}".FormatWith( |
81 |
uri.Host, |
82 |
(uri.Scheme.EqualsIgnoreCase("http") && uri.Port != 80 || |
83 |
uri.Scheme.EqualsIgnoreCase("https") && uri.Port != 443) |
84 |
? ":" + uri.Port |
85 |
: ""); |
86 |
|
87 |
var built = "{0}://{1}{2}{3}".FormatWithInvariantCulture( |
88 |
scheme, authority, uri.AbsolutePath, uriQuery |
89 |
); |
90 |
|
91 |
Uri mockUri; |
92 |
var request = Uri.TryCreate( |
93 |
built, UriKind.RelativeOrAbsolute, out mockUri |
94 |
) ? new MockHttpWebRequest(mockUri) |
95 |
: new MockHttpWebRequest( |
96 |
new Uri(uri.ToString().Replace( |
97 |
"mock", scheme)) |
98 |
); |
99 |
|
100 |
int statusCodeValue; |
101 |
#if !NETCF |
102 |
int.TryParse(statusCode, out statusCodeValue); |
103 |
#else |
104 |
try |
105 |
{ |
106 |
statusCodeValue = int.Parse(statusCode); |
107 |
} |
108 |
catch (Exception) |
109 |
{ |
110 |
statusCodeValue = 0; |
111 |
} |
112 |
#endif |
113 |
if (!statusCode.IsNullOrBlank()) request.ExpectStatusCode = (HttpStatusCode)statusCodeValue; |
114 |
if (!statusDescription.IsNullOrBlank()) request.ExpectStatusDescription = statusDescription; |
115 |
if (!content.IsNullOrBlank()) request.Content = content; |
116 |
if (!contentType.IsNullOrBlank()) request.ContentType = contentType; |
117 |
|
118 |
if(!headerNames.IsNullOrBlank() && !headerValues.IsNullOrBlank()) |
119 |
{ |
120 |
var headers = new NameValueCollection(); |
121 |
var names = headerNames.Split(',').Where(n => !n.IsNullOrBlank()).ToArray(); |
122 |
var values = headerValues.Split(',').Where(v => !v.IsNullOrBlank()).ToArray(); |
123 |
if(names.Count() == values.Count()) |
124 |
{ |
125 |
for(var i = 0; i < names.Count(); i++) |
126 |
{ |
127 |
headers.Add(names[i], values[i]); |
128 |
} |
129 |
} |
130 |
|
131 |
foreach(var key in headers.AllKeys) |
132 |
{ |
133 |
request.ExpectHeaders.Add(key, headers[key]); |
134 |
} |
135 |
} |
136 |
return request; |
137 |
} |
138 |
} |
139 |
} |