alba icon indicating copy to clipboard operation
alba copied to clipboard

HttpResponseBody.ReadAsText() hangs (IScenarioResult)

Open PhilipRRWFM opened this issue 2 years ago • 1 comments

The HttpResponseBody(IScenarioResult).ReadAsText method hangs when using AlbaHost in the context of SpecFlow, with an async step implementation. Specifically, when attempting to copy the response body stream (of type ResponseBodyReaderStream), marked in the source snippet below.

The implementation of ResponseBodyReaderStream.Read method in turn contains a call with .GetAwaiter().GetResult();. This may be the reason for the copy operation hanging.

Solved outside the AlbaHost source base for now, by calling the Context.Response.Body.CopyToAsync(stream); method instead.

    ```
    /// <summary>
    /// Read the contents of the HttpResponse.Body as text
    /// </summary>
    /// <returns></returns>
    public string ReadAsText()
    {
        return Read(s => s.ReadAllText());
    }

    public T Read<T>(Func<Stream, T> read)
    {
        if (Context.Response.Body.CanSeek || Context.Response.Body is MemoryStream)
        {
            Context.Response.Body.Position = 0;
        }
        else
        {
            var stream = new MemoryStream();
            Context.Response.Body.CopyTo(stream); <--- hangs
            stream.Position = 0;
            Context.Response.Body = stream;
        }
        
        return read(Context.Response.Body);
    }

PhilipRRWFM avatar May 30 '22 11:05 PhilipRRWFM

Also seeing this issue, fixed it in our codebase for now

carlosrfernandez avatar Sep 06 '22 13:09 carlosrfernandez

Blech, I was checked out of Alba for awhile. I'll see what I can do the next time I'm in the code.

jeremydmiller avatar Oct 31 '22 19:10 jeremydmiller

@PhilipRRWFM I guess we could make a PR for this?

jmezach avatar Nov 02 '22 07:11 jmezach