title: How do I mock the HttpRequest on an "azure function"
body: <p>I have a function with an Http trigger</p>
  <pre><code>public async Task&lt;IActionResult&gt; Run([HttpTrigger(AuthorizationLevel.Function,&quot;post&quot;, Route = null)] HttpRequest req)
  </code></pre>
  <p>I want to mock the req. I have manage to do so just fine like this</p>
  <pre><code>private static Mock&lt;HttpRequest&gt; CreateMockRequest(object body)
  {            
      using var memoryStream= new MemoryStream();
      using var writer = new StreamWriter(memoryStream);
   
      var json = JsonConvert.SerializeObject(body);
   
      writer .Write(json);
      writer .Flush();
   
      memoryStream.Position = 0;
   
      var mockRequest = new Mock&lt;HttpRequest&gt;();
      mockRequest.Setup(x =&gt; x.Body).Returns(memoryStream);
      mockRequest.Setup(x =&gt; x.ContentType).Returns(&quot;application/json&quot;);
  
      return mockRequest;
  }
  </code></pre>
  <p>body, in the above code is just some json. I am then using this code to deserialize this the json stuffed in the body of the mockRequest</p>
  <pre><code>public static async Task&lt;HttpResponseBody&lt;T&gt;&gt; GetBodyAsync&lt;T&gt;(this HttpRequest req)
      using var stream = new StreamReader(req.Body);
      var bodyString = await stream.ReadToEndAsync();
  ...
  }
  </code></pre>
  <p>The bodyString is not valid json here because there seems to be escaping of the quotes in the json e.g.
  Original Json = <code>{&quot;x&quot;: &quot;somexvalue&quot;}</code>
  the value coming back = <code>{\&quot;x\&quot;: \&quot;somexvalue\&quot;</code></p>
  <p>Before you say that this is just visual studio debug inspector, it is not. I have checked. It seems the <code>StreamWriter</code> is doing this or the <code>StreamReader</code> is.</p>
  <p>The obvious solution was to just strip the \ out of the resulting json but this feels so wrong and work arroundy. Is there a way to fix this without having to change my function.</p>