pact-foundation / pact-workshop-dotnet-core-v1

A workshop for Pact using .NET Core
Apache License 2.0
76 stars 209 forks source link

Provider Test - 500 response from Pact service #20

Closed jbrangwyn closed 3 years ago

jbrangwyn commented 3 years ago

Hi, thanks for making this tutorial, it's really helped my understanding.

I get a 500 error from the tests when running the provider test - the same result when I run my own solution, the completed solution, and I've now tried it on 2 machines (fresh repo each time).

I've managed to establish that the issue is in the middleware. By putting in a try catch the tests ran successfully, except for this test which I had to remove: "A invalid GET request for Date Validation with invalid date parameter", which was getting a 200 instead of a 400.

Presumably it has something with that test doing the "There is no data" provider state. I'll have another go this week at figuring out what the issue is. I just wanted to put this here in case anyone else ran into the same thing.

jbrangwyn commented 3 years ago

It seems to be failing to read the request body in the HandleProviderStatesRequest method: using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8)) { jsonRequestBody = reader.ReadToEnd(); }

jbrangwyn commented 3 years ago

Got the exception out of the middleware by logging it to a file:

Result:System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamReader.ReadBuffer() at System.IO.StreamReader.ReadToEnd()

Changed 'ReadToEnd' To 'ReadToEndAsync' and it is now working successfully. If I get a chance I'll do a PR.