MockHttpRequest.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Web;
  5. using System.Web.Routing;
  6. using Mvc.Stream.Tests.Mocks;
  7. namespace Masuit.Tools.UnitTest.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 MemoryStream();
  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 MemoryStream();
  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. }