| /trunk/testhttp/src/TestHttp/WorkerRequestStub.cs r16 | /trunk/testhttp/src/TestHttp/WorkerRequestStub.cs r17 | ||
| 1 | using System; | 1 | using System; |
|---|---|---|---|
| 2 | using System.IO; | 2 | using System.IO; |
| 3 | using System.Text; | 3 | using System.Text; |
| 4 | using System.Web.Hosting; | 4 | using System.Web.Hosting; |
| 5 | 5 | ||
| 6 | namespace CodeBackpack.Test.Http | 6 | namespace CodeBackpack.Test.Http |
| 7 | { | 7 | { |
| 8 | /// <summary> | 8 | /// <summary> |
| 9 | /// A worker request stub that lets you fake out the HTTP request methods. | 9 | /// A worker request stub that lets you fake out the HTTP request methods. |
| 10 | /// </summary> | 10 | /// </summary> |
| 11 | /// <remarks> | ||
| 12 | /// Only few strategic methods were overriden. | ||
| 13 | /// </remarks> | ||
| 11 | internal class WorkerRequestStub : SimpleWorkerRequest | 14 | internal class WorkerRequestStub : SimpleWorkerRequest |
| 12 | { | 15 | { |
| 13 | private readonly String requestMethod; | 16 | private readonly String requestMethod; |
| 14 | private readonly String requestBody; | 17 | private readonly String requestBody; |
| 15 | private readonly Encoding requestEncoding; | 18 | private readonly Encoding requestEncoding; |
| 16 | private MemoryStream requestStream; | 19 | private MemoryStream requestStream; |
| 17 | 20 | ||
| 18 | public WorkerRequestStub(string requestMethod, string requestBody, Encoding requestEncoding, string page, string query, StringWriter output) | 21 | public WorkerRequestStub(String requestMethod, String requestBody, Encoding requestEncoding, String page, String query, StringWriter output) |
| 19 | : base("/it/does/not/matter", @"I:\t\does\not\matter", page, query, output) | 22 | : base("/it/does/not/matter", @"I:\t\does\not\matter", page, query, output) |
| 20 | { | 23 | { |
| 21 | this.requestMethod = requestMethod; | 24 | this.requestMethod = requestMethod; |
| 22 | this.requestBody = requestBody; | 25 | this.requestBody = requestBody; |
| 23 | this.requestEncoding = requestEncoding; | 26 | this.requestEncoding = requestEncoding; |
| 24 | } | 27 | } |
| 25 | 28 | ||
| 26 | public override string GetHttpVerbName() | 29 | public override String GetHttpVerbName() |
| 27 | { | 30 | { |
| 28 | return requestMethod; | 31 | return requestMethod; |
| 29 | } | 32 | } |
| 30 | 33 | ||
| 31 | public override int ReadEntityBody(byte[] buffer, int size) | 34 | public override int ReadEntityBody(byte[] buffer, int size) |
| 32 | { | 35 | { |
| 33 | if (requestBody == null) | ||
| 34 | { | ||
| 35 | throw new ArgumentNullException("You must supply non-null 'requestEntityBody' argument!"); | ||
| 36 | } | ||
| 37 | return GetRequestStream().Read(buffer, 0, size); | 36 | return GetRequestStream().Read(buffer, 0, size); |
| 38 | } | 37 | } |
| 39 | 38 | ||
| 40 | private MemoryStream GetRequestStream() | 39 | private MemoryStream GetRequestStream() |
| 41 | { | 40 | { |
| 42 | if (requestStream == null) | 41 | if (requestStream == null) |
| 43 | { | 42 | { |
| 43 | if (requestBody == null) | ||
| 44 | { | ||
| 45 | throw new ArgumentNullException("You must supply non-null 'requestBody' argument!"); | ||
| 46 | } | ||
| 44 | byte[] requestBytes = requestEncoding.GetBytes(requestBody); | 47 | byte[] requestBytes = requestEncoding.GetBytes(requestBody); |
| 45 | requestStream = new MemoryStream(requestBytes); | 48 | requestStream = new MemoryStream(requestBytes); |
| 46 | } | 49 | } |
| 47 | return requestStream; | 50 | return requestStream; |
| 48 | } | 51 | } |
| 49 | } | 52 | } |
| 50 | } | 53 | } |