-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathMockHttpRequest.cs
More file actions
241 lines (194 loc) · 8 KB
/
Copy pathMockHttpRequest.cs
File metadata and controls
241 lines (194 loc) · 8 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using ServiceStack.Configuration;
using ServiceStack.Host;
using ServiceStack.IO;
using ServiceStack.Web;
namespace ServiceStack.Testing
{
public class MockHttpRequest : IHttpRequest, IHasResolver, IHasVirtualFiles
#if NETCORE
, IHasServiceScope
#endif
{
private IResolver resolver;
public IResolver Resolver
{
get => resolver ?? Service.GlobalResolver;
set => resolver = value;
}
#if NETCORE
public Microsoft.Extensions.DependencyInjection.IServiceScope ServiceScope { get; set; }
#endif
public MockHttpRequest()
{
this.FormData = new NameValueCollection();
this.Headers = new NameValueCollection();
this.QueryString = new NameValueCollection();
this.Cookies = new Dictionary<string, Cookie>();
this.Items = new Dictionary<string, object>();
this.Response = new MockHttpResponse(this);
}
public MockHttpRequest(string operationName, string httpMethod,
string contentType, string pathInfo,
NameValueCollection queryString, Stream inputStream, NameValueCollection formData)
: this()
{
this.OperationName = operationName;
this.HttpMethod = httpMethod;
this.ContentType = contentType;
this.ResponseContentType = contentType;
this.PathInfo = pathInfo;
this.InputStream = inputStream;
this.QueryString = queryString;
this.FormData = formData;
}
public object OriginalRequest => null;
public IResponse Response { get; }
public T TryResolve<T>()
{
#if NETCORE
if (ServiceScope != null)
{
var instance = ServiceScope.ServiceProvider.GetService(typeof(T));
if (instance != null)
return (T)instance;
}
#endif
return this.TryResolveInternal<T>();
}
public object GetService(Type serviceType)
{
var mi = typeof(BasicRequest).GetMethod(nameof(TryResolve));
var genericMi = mi.MakeGenericMethod(serviceType);
return genericMi.Invoke(this, TypeConstants.EmptyObjectArray);
}
public AuthUserSession RemoveSession()
{
this.RemoveSession();
return this.GetSession() as AuthUserSession;
}
public AuthUserSession ReloadSession()
{
return this.GetSession() as AuthUserSession;
}
public string OperationName { get; set; }
public RequestAttributes RequestAttributes { get; set; }
private IRequestPreferences requestPreferences;
public IRequestPreferences RequestPreferences => requestPreferences ??= new RequestPreferences(this);
public object Dto { get; set; }
public string ContentType { get; set; }
public IHttpResponse HttpResponse { get; private set; }
public string UserAgent { get; set; }
public bool IsLocal { get; set; }
public string HttpMethod { get; set; }
public string Verb => HttpMethod;
public IDictionary<string, Cookie> Cookies { get; set; }
private string responseContentType;
public string ResponseContentType
{
get => responseContentType ?? this.ContentType ?? MimeTypes.Json;
set => responseContentType = value;
}
public bool HasExplicitResponseContentType { get; private set; }
public NameValueCollection Headers { get; set; }
public NameValueCollection QueryString { get; set; }
public NameValueCollection FormData { get; set; }
public bool UseBufferedStream { get; set; }
public Dictionary<string, object> Items { get; set; }
private string rawBody;
public string GetRawBody()
{
if (rawBody != null) return rawBody;
if (InputStream == null) return null;
//Keep the stream alive in-case it needs to be read twice (i.e. ContentLength)
rawBody = InputStream.ReadToEnd();
return rawBody;
}
public Task<string> GetRawBodyAsync() => Task.FromResult(GetRawBody());
public string RawUrl { get; set; }
public string AbsoluteUri => "http://localhost" + this.PathInfo;
public string UserHostAddress { get; set; }
public string XForwardedFor
{
get => string.IsNullOrEmpty(Headers[HttpHeaders.XForwardedFor]) ? null : Headers[HttpHeaders.XForwardedFor];
set => Headers[HttpHeaders.XForwardedFor] = value;
}
public int? XForwardedPort
{
get => string.IsNullOrEmpty(Headers[HttpHeaders.XForwardedPort])
? (int?) null
: int.Parse(Headers[HttpHeaders.XForwardedPort]);
set => Headers[HttpHeaders.XForwardedPort] = value?.ToString();
}
public string XForwardedProtocol
{
get => string.IsNullOrEmpty(Headers[HttpHeaders.XForwardedProtocol])
? null
: Headers[HttpHeaders.XForwardedProtocol];
set => Headers[HttpHeaders.XForwardedProtocol] = value;
}
public string XRealIp
{
get => string.IsNullOrEmpty(Headers[HttpHeaders.XRealIp]) ? null : Headers[HttpHeaders.XRealIp];
set => Headers[HttpHeaders.XRealIp] = value;
}
public string Accept
{
get => string.IsNullOrEmpty(Headers[HttpHeaders.Accept]) ? null : Headers[HttpHeaders.Accept];
set => Headers[HttpHeaders.Accept] = value;
}
private string remoteIp;
public string RemoteIp => remoteIp ??= XForwardedFor ?? (XRealIp ?? UserHostAddress);
public string Authorization
{
get => string.IsNullOrEmpty(Headers[HttpHeaders.Authorization])
? null
: Headers[HttpHeaders.Authorization];
set => Headers[HttpHeaders.Authorization] = value;
}
public bool IsSecureConnection
{
get => (RequestAttributes & RequestAttributes.Secure) == RequestAttributes.Secure;
set
{
if (value)
RequestAttributes |= RequestAttributes.Secure;
else
RequestAttributes &= ~RequestAttributes.Secure;
}
}
public string[] AcceptTypes { get; set; }
public string PathInfo { get; set; }
public string OriginalPathInfo { get; }
public Stream InputStream { get; set; }
public long ContentLength
{
get
{
var body = GetRawBody();
return body?.Length ?? 0;
}
}
public IHttpFile[] Files { get; set; }
public string ApplicationFilePath { get; set; }
public void AddSessionCookies()
{
var permSessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
this.Cookies[SessionFeature.PermanentSessionId] = new Cookie(SessionFeature.PermanentSessionId, permSessionId);
var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
this.Cookies[SessionFeature.SessionId] = new Cookie(SessionFeature.SessionId, sessionId);
}
public Uri UrlReferrer => null;
public IVirtualFile GetFile() => HostContext.VirtualFileSources.GetFile(PathInfo);
public IVirtualDirectory GetDirectory() => HostContext.VirtualFileSources.GetDirectory(PathInfo);
private bool? isDirectory;
public bool IsDirectory => isDirectory ?? (bool)(isDirectory = HostContext.VirtualFiles.DirectoryExists(PathInfo));
private bool? isFile;
public bool IsFile => isFile ?? (bool)(isFile = HostContext.VirtualFiles.FileExists(PathInfo));
}
}