icarus-consulting / Yaapii.Http

C# Library for Http Request
MIT License
7 stars 0 forks source link

Fix HttpMock not returning a stream from a fake wire more than once #142

Closed DFU398 closed 1 year ago

DFU398 commented 1 year ago

Expected Behavior

HttpMock can return the same response body multiple times.

Actual Behavior

If the same stream is used for more than one response, it is only copied to the response body the first time, because the stream position is not reset after copying, so when trying to copy it for the second time, the end of the stream is reached immediately.

Mention any other details that might be useful

The position of the stream in the response from the fake wire needs to be reset to 0 after copying it to the http response.

Steps to reproduce the behavior

        [Fact]
        public void ReturnsBodyMoreThanOnce()
        {
            var port = new AwaitedPort(new TestPort()).Value();
            using (var server =
                new HttpMock(port,
                    new FkWire("test")
                ).Value()
            )
            {
                var result = "";
                result +=
                    new TextBody.Of(
                        new Verified(
                            new AspNetCoreWire(
                                new AspNetCoreClients(),
                                new TimeSpan(0, 1, 0)
                            ),
                            new ExpectedStatus(200)
                        ).Response(
                            new Get($"http://localhost:{port}")
                        )
                    ).AsString();
                result +=
                    new TextBody.Of(
                        new Verified(
                            new AspNetCoreWire(
                                new AspNetCoreClients(),
                                new TimeSpan(0, 1, 0)
                            ),
                            new ExpectedStatus(200)
                        ).Response(
                            new Get($"http://localhost:{port}")
                        )
                    ).AsString(); // <-- exception thrown here
                Assert.Equal(
                    "testtest",
                    result
                );
            }
        }

The exception below is thrown when trying to get the body the second time. At that point, the debugger shows result contains "test", so the body was returned the first time.

The log given by the failure.

System.InvalidOperationException: "Failed to extract body as text. No body found."