MockHttpRequest.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Web;
  5. using System.Web.Routing;
  6. using Masuit.Tools.Systems;
  7. namespace Masuit.Tools.Test.Mvc.Mocks
  8. {
  9. public class MockHttpRequest : HttpRequestBase
  10. {
  11. public override NameValueCollection Headers => _headers;
  12. public override HttpFileCollectionBase Files { get; }
  13. public override RequestContext RequestContext => _context;
  14. public override string ApplicationPath => _applicationPath;
  15. public override System.IO.Stream InputStream
  16. {
  17. get
  18. {
  19. if (TestInput != null)
  20. {
  21. var stream = new PooledMemoryStream();
  22. var chars = TestInput.ToCharArray();
  23. foreach (var c in chars)
  24. {
  25. stream.WriteByte(Convert.ToByte(c));
  26. }
  27. return stream;
  28. }
  29. return new PooledMemoryStream();
  30. }
  31. }
  32. public override string HttpMethod => TestHttpMethod;
  33. private readonly NameValueCollection _headers = new NameValueCollection();
  34. private readonly MockRequestContext _context = new MockRequestContext();
  35. private string _applicationPath;
  36. public string TestInput;
  37. public string TestHttpMethod;
  38. public MockHttpRequest(MockHttpFilesCollection filesMock)
  39. {
  40. Files = filesMock;
  41. }
  42. public MockHttpRequest SetHeader(string header, string val)
  43. {
  44. _headers[header] = val;
  45. return this;
  46. }
  47. public MockHttpRequest SetApplicationPath(string path)
  48. {
  49. _applicationPath = path;
  50. return this;
  51. }
  52. }
  53. }