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
123 lines (101 loc) · 3.9 KB
/
MockHttpRequest.cs
File metadata and controls
123 lines (101 loc) · 3.9 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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using Funq;
using ServiceStack.ServiceHost;
namespace ServiceStack.ServiceInterface.Testing
{
public class MockHttpRequest : IHttpRequest
{
public MockHttpRequest()
{
this.FormData = new NameValueCollection();
this.Headers = new NameValueCollection();
this.Cookies = new Dictionary<string, Cookie>();
this.Items = new Dictionary<string, object>();
this.Container = new Container();
}
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 ?? new NameValueCollection();
}
public object OriginalRequest
{
get { return null; }
}
public T TryResolve<T>()
{
return Container.TryResolve<T>();
}
public Container Container { get; set; }
public string OperationName { get; set; }
public string ContentType { get; set; }
public string HttpMethod { get; set; }
public string UserAgent { get; set; }
public IDictionary<string, Cookie> Cookies { get; set; }
private string responseContentType;
public string ResponseContentType
{
get { return responseContentType ?? this.ContentType; }
set { responseContentType = value; }
}
public NameValueCollection Headers { get; set; }
public NameValueCollection QueryString { get; set; }
public NameValueCollection FormData { get; set; }
public Dictionary<string, object> Items
{
get;
private 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 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 IFile[] 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);
}
}
}