MockHttpRequest.cs 1.7 KB

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