-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMockWebRequest.cs
105 lines (90 loc) · 3.02 KB
/
MockWebRequest.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
using System;
using System.Collections.Generic;
using System.Web;
namespace Inversion.Web {
/// <summary>
/// Implements a mock IWebRequest for use with MockWebContext
/// to facilitate testing.
/// </summary>
public class MockWebRequest : IWebRequest {
private readonly Dictionary<string, string> _params = new Dictionary<string, string>();
private readonly HashSet<string> _flags = new HashSet<string>();
private readonly Dictionary<string, string> _headers = new Dictionary<string, string>();
private string _method = "GET";
/// <summary>
/// Gives access to any files uploaded by the user agent
/// as part of this request.
/// </summary>
public IRequestFilesCollection Files {
get { throw new NotImplementedException("The HttpFilesCollection has not been mocked."); }
}
/// <summary>
/// Gives access to a url-info object that provides
/// info about the structure of the url of the request.
/// </summary>
public UrlInfo UrlInfo { get; set; }
/// <summary>
/// The http method of the request.
/// </summary>
public string Method {
get { return _method; }
set { _method = value; }
}
/// <summary>
/// Returns true if the http method of this request is GET; otherwise returns false.
/// </summary>
public bool IsGet { get; set; }
/// <summary>
/// Returns true if the http method of this request is POST; otherwise returns false.
/// </summary>
public bool IsPost { get; set; }
/// <summary>
/// Provides access to the request parameters from both the querystring
/// and those that are posted.
/// </summary>
/// <remarks>
/// First params are read from the querystring and then those posted which
/// will override any from the querystring.
/// </remarks>
public IDictionary<string, string> Params {
get { return _params; }
}
/// <summary>
/// Gives access to the payload if any of the request.
/// </summary>
public string Payload { get; set; }
/// <summary>
/// Gives access to any flags present in the querystring.
/// </summary>
/// <remarks>
/// Any querystring parameter that is a single value rather
/// that a key-value pair is regarded as a flag.
/// </remarks>
public IEnumerable<string> Flags { get { return _flags; } }
/// <summary>
/// Gives access to the headers of the reuqest.
/// </summary>
public IDictionary<string, string> Headers { get { return _headers; } }
/// <summary>
/// Gives access to the request cookies.
/// </summary>
public IRequestCookieCollection Cookies {
get { throw new NotImplementedException("The HttpCookieCollection has not been mocked."); }
}
/// <summary>
/// Instantiates a new mock web request
/// from the url provided.
/// </summary>
/// <param name="url">The url of the request to be mocked.</param>
public MockWebRequest(string url) {
this.UrlInfo = new UrlInfo(url);
foreach (KeyValuePair<string, string> entry in this.UrlInfo.Query) {
if (entry.Key == entry.Value) {
_flags.Add(entry.Key);
} else {
this.Params.Add(entry);
}
}
}
}
}