Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / Hammock / Web / Mocks / MockHttpWebRequest.cs @ 0eea575a

History | View | Annotate | Download (3.8 kB)

1
using System;
2
using System.IO;
3
using System.Net;
4

    
5
namespace Hammock.Web.Mocks
6
{
7
#if !SILVERLIGHT
8
    [Serializable]
9
#endif
10
    public class MockHttpWebRequest : WebRequest
11
    {
12
        private readonly Uri _requestUri;
13

    
14
        public virtual HttpStatusCode ExpectStatusCode { get; protected internal set; }
15
        public virtual string ExpectStatusDescription { get; protected internal set; }
16
        public virtual WebHeaderCollection ExpectHeaders { get; protected internal set; }
17
#if SILVERLIGHT
18
        // Need a wrapper around System.Net.WebHeaderCollection to allow headers in mocks   
19
#endif
20
        
21
        public virtual string Content { get; set; }
22

    
23
#if WindowsPhone 
24
        public long ContentLength { get; set; }
25
#elif !SILVERLIGHT
26
        public override long ContentLength { get; set; }
27
#elif !WindowsPhone
28
        public long ContentLength { get; set; }
29
#endif
30
        public override string ContentType { get; set; }
31

    
32
        public MockHttpWebRequest(Uri requestUri)
33
        {
34
            _requestUri = requestUri;
35
            Initialize();
36
        }
37

    
38
        private void Initialize()
39
        {
40
            Headers = new System.Net.WebHeaderCollection();
41
            ExpectHeaders = new WebHeaderCollection();
42
        }
43

    
44
#if !SILVERLIGHT
45
        public override WebResponse GetResponse()
46
        {
47
            return CreateResponse();
48
        }
49
#endif      
50
        public override void Abort()
51
        {
52
            
53
        }
54

    
55
        private WebResponse CreateResponse()
56
        {
57
            var response = new MockHttpWebResponse(_requestUri, ContentType)
58
            {
59
                StatusCode = ExpectStatusCode,
60
                StatusDescription = ExpectStatusDescription,
61
                Content = Content
62
            };
63

    
64
            foreach (var key in ExpectHeaders.AllKeys)
65
            {
66
                response.MockHeaders.Add(key, ExpectHeaders[key].Value);
67
            }
68

    
69
            return response;
70
        }
71

    
72
#if !SILVERLIGHT
73
        public override Stream GetRequestStream()
74
        {
75
            return new MemoryStream();
76
        }
77
#endif
78
        public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
79
        {
80
            // [DC]: Mock POSTs never write to the request
81
            return BeginGetResponse(callback, state);
82

    
83
            /* var result = new WebQueryAsyncResult
84
                             {
85
                                 AsyncState = new MemoryStream(),
86
                                 IsCompleted = true,
87
                                 CompletedSynchronously = true
88
                             };*/
89
        }
90

    
91
        public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
92
        {
93
            var response = CreateResponse();
94
            var result = new WebQueryAsyncResult
95
                             {
96
                                 AsyncState = response,
97
                                 IsCompleted = true,
98
                                 CompletedSynchronously = true
99
                             };
100

    
101
            return result;
102
        }
103

    
104
        public override Stream EndGetRequestStream(IAsyncResult asyncResult)
105
        {
106
            var result = (WebQueryAsyncResult) asyncResult;
107
            return result.AsyncState as MemoryStream;
108
        }
109

    
110
        public override WebResponse EndGetResponse(IAsyncResult asyncResult)
111
        {
112
            var result = (WebQueryAsyncResult)asyncResult;
113
            return result.AsyncState as WebResponse;
114
        }
115

    
116
        public override System.Net.WebHeaderCollection Headers { get; set; }
117
        public override string Method { get; set; }
118

    
119
        public override Uri RequestUri
120
        {
121
            get { return _requestUri; }
122
        }
123

    
124
#if !SILVERLIGHT
125
        public override int Timeout
126
        {
127
            get
128
            {
129
                return int.MaxValue;
130
            }
131
        }
132
#endif
133

    
134
    }
135
}