How to insert custom input stream in HttpRequest.InputStream?

MODIFIED: The code uses HttpRequest.InputStream to read the request body, parse it, and save it. I would like to be able to unit test this class, but I cannot insert anything into the body. Is it possible? Perhaps I could somehow hack class A, and set the value of _body to some value.

ADDED: here is a simplified design:

class A
{
  private string _body;
  public string Body { get { return _body; } }

  public A(HttpRequest r)
  {
    {parse r and store data, i.e. query parameter, etc. }
    _body = {read body from r.InputStream };
  }
}

class B
{
  public void ProcessRequest(HttpRequest r)
  {
    var c = new C(r);
    ParseBody(c.Body);
  }
}

Everything is fine, I can really check ParseBody, even if it is private. However, I am a class A testing module and where the problem is. I can create the Body R / W property, but this is an encapsulation violation, which I hate because A is supposed to be invariant.

+3
source share
3 answers

InputStream - , , , unit test , . HttpRequest, , . , .

HttpRequest, HttpRequestBase, InputStream. , , .

+1

- HttpRequest.InputStream .

, , HttpRequest.InputStream .

+2

Test it outside: place the script somewhere locally and call it with whatever body you want it to consume. This is not exactly a unit test, but if you need a unit test that directly reads a read from HttpRequest.InputStream, consider sharing the connection part and reading the stream; You can feed the streaming device to any stream you like.

+2
source

Source: https://habr.com/ru/post/1788443/


All Articles