forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockHttpRequest.cs
More file actions
172 lines (140 loc) · 5.53 KB
/
Copy pathMockHttpRequest.cs
File metadata and controls
172 lines (140 loc) · 5.53 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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using Funq;
using ServiceStack.Auth;
using ServiceStack.Host;
using ServiceStack.Web;
namespace ServiceStack.Testing
{
public class MockHttpRequest : IHttpRequest
{
public Container Container { get; set; }
public MockHttpRequest()
{
this.FormData = PclExportClient.Instance.NewNameValueCollection();
this.Headers = PclExportClient.Instance.NewNameValueCollection();
this.QueryString = PclExportClient.Instance.NewNameValueCollection();
this.Cookies = new Dictionary<string, Cookie>();
this.Items = new Dictionary<string, object>();
this.Container = ServiceStackHost.Instance != null ? ServiceStackHost.Instance.Container : new Container();
this.Response = new MockHttpResponse();
}
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.InWrapper();
this.FormData = new NameValueCollectionWrapper(formData ?? new NameValueCollection());
}
public object OriginalRequest
{
get { return null; }
}
public IResponse Response { get; private set; }
public T TryResolve<T>()
{
return Container != null
? Container.TryResolve<T>()
: HostContext.TryResolve<T>();
}
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
{
get
{
if (requestPreferences == null)
{
requestPreferences = new RequestPreferences(this);
}
return requestPreferences;
}
}
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
{
get { return HttpMethod; }
}
public IDictionary<string, Cookie> Cookies { get; set; }
private string responseContentType;
public string ResponseContentType
{
get { return responseContentType ?? this.ContentType ?? MimeTypes.Json; }
set { responseContentType = value; }
}
public bool HasExplicitResponseContentType { get; private set; }
public INameValueCollection Headers { get; set; }
public INameValueCollection QueryString { get; set; }
public INameValueCollection 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 = new StreamReader(InputStream).ReadToEnd();
InputStream.Position = 0;
return rawBody;
}
public string RawUrl { get; set; }
public string AbsoluteUri
{
get { return "http://localhost" + this.PathInfo; }
}
public string UserHostAddress { get; set; }
public string RemoteIp { get; set; }
public string XForwardedFor { get; set; }
public int? XForwardedPort { get; set; }
public string XForwardedProtocol { get; set; }
public string XRealIp { get; set; }
public bool IsSecureConnection { get; set; }
public string[] AcceptTypes { get; set; }
public string PathInfo { get; set; }
public Stream InputStream { get; set; }
public long ContentLength
{
get
{
var body = GetRawBody();
return body != null ? 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 { get { return null; } }
}
}