| /trunk/testhttp/src/TestHttp/WorkerRequestStub.cs r17 | /trunk/testhttp/src/TestHttp/WorkerRequestStub.cs r18 | ||
| 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> | 11 | /// <remarks> |
| 12 | /// Only few strategic methods were overriden. | 12 | /// Only few strategic methods were overriden. |
| 13 | /// </remarks> | 13 | /// </remarks> |
| 14 | internal class WorkerRequestStub : SimpleWorkerRequest | 14 | internal class WorkerRequestStub : SimpleWorkerRequest |
| 15 | { | 15 | { |
| 16 | private readonly String requestMethod; | 16 | private readonly String requestMethod; |
| 17 | private readonly String requestBody; | 17 | private readonly String requestBody; |
| 18 | private readonly Encoding requestEncoding; | 18 | private readonly Encoding requestEncoding; |
| 19 | private MemoryStream requestStream; | 19 | private byte[] entityBody; |
| 20 | 20 | ||
| 21 | 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) |
| 22 | : 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) |
| 23 | { | 23 | { |
| 24 | this.requestMethod = requestMethod; | 24 | this.requestMethod = requestMethod; |
| 25 | this.requestBody = requestBody; | 25 | this.requestBody = requestBody; |
| 26 | this.requestEncoding = requestEncoding; | 26 | this.requestEncoding = requestEncoding; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | public override String GetHttpVerbName() | 29 | public override String GetHttpVerbName() |
| 30 | { | 30 | { |
| 31 | return requestMethod; | 31 | return requestMethod; |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | public override int ReadEntityBody(byte[] buffer, int size) | 34 | public override bool IsEntireEntityBodyIsPreloaded() |
| 35 | { | 35 | { |
| 36 | return GetRequestStream().Read(buffer, 0, size); | 36 | return true; |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | private MemoryStream GetRequestStream() | 39 | public override byte[] GetPreloadedEntityBody() |
| 40 | { | 40 | { |
| 41 | if (requestStream == null) | 41 | if (entityBody == null) |
| 42 | { | 42 | { |
| 43 | if (requestBody == null) | 43 | if (requestBody == null) |
| 44 | { | 44 | { |
| 45 | throw new ArgumentNullException("You must supply non-null 'requestBody' argument!"); | 45 | throw new ArgumentNullException("requestBody", |
| 46 | } | 46 | "You must supply non-null 'requestBody' argument!"); |
| 47 | byte[] requestBytes = requestEncoding.GetBytes(requestBody); | 47 | } |
| 48 | requestStream = new MemoryStream(requestBytes); | 48 | if (requestEncoding == null) |
| 49 | } | 49 | { |
| 50 | return requestStream; | 50 | throw new ArgumentNullException("requestEncoding", |
| 51 | "You must supply non-null 'requestEncoding' argument!"); | ||
| 52 | } | ||
| 53 | entityBody = requestEncoding.GetBytes(requestBody); | ||
| 54 | } | ||
| 55 | return entityBody; | ||
| 51 | } | 56 | } |
| 52 | } | 57 | } |
| 53 | } | 58 | } |