+ 1

[C#] DefaultHttpContext.Response is null, why?

Hi :) I have been learning how to create an api and I did an Global Exceptions Handler. After that i have done unit tests for this. I did this (not copy-paste) but i wrote line by line how it was on this website http://anthonygiretti.com/2018/09/04/asp-net-core-2-1-middlewares-part2-unit-test-a-custom-middleware/ and my context.Response.Body is null because context.Response is null. I tried to find sth in the internet but i couldn't :( could somebody help me? private DefaultHttpContext GetHttpContext() { var context = new DefaultHttpContext(); context.Response.Body = new MemoryStream(); return context; } [Fact] public async void A() { //Arrange var middleware = new ExceptionMiddleware((HttpContext innerHttpContext) => { throw new FakeApiException(); }); var context = GetHttpContext(); //Act await middleware.InvokeAsync(context); context.Response.Body.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(context.Response.Body); string streamText = await reader.ReadToEndAsync(); ErrorDetails objResponse = JsonConvert.DeserializeObject<ErrorDetails>(streamText); //Assert Assert.Equal("Internal Server Error from the custom middleware", objResponse.Message); } I have been larning english and can you tell me, where (if they are exists) mistakes? Thanks in advance :)

20th May 2019, 4:10 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
13 Respostas
0
Can you show use the code ?
20th May 2019, 6:24 PM
sneeze
sneeze - avatar
0
Ok. I do not understand a whole lot. But where do you call this method ? And which website are you reading ?
20th May 2019, 8:49 PM
sneeze
sneeze - avatar
0
http://anthonygiretti.com/2018/09/04/asp-net-core-2-1-middlewares-part2-unit-test-a-custom-middleware/ this website, and in GetHttpContext there is context.Response.Body = new MemoryStream(); and this Response is null
20th May 2019, 8:51 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
0
context.Response.Body = new MemoryStream(); new MemoryStream() creates a empty MemoryStream so it is logical that this results in "null" So where is the memorystream filled ? Using google I found this link does it help you ? https://stackoverflow.com/questions/56225883/how-to-fix-defaulthttpcontext-response-throwing-nullreferenceexception-in-c-shar
20th May 2019, 9:01 PM
sneeze
sneeze - avatar
0
It my question but on stackoverflow hahaha. You dont understand... context.Response is null
20th May 2019, 9:02 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
0
Hihi. Sorry I can't help you.
20th May 2019, 9:03 PM
sneeze
sneeze - avatar
0
Why
20th May 2019, 9:04 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
0
Because there is to little information in your question. You english is understandable though. The code snippet is to small to understand where the data should come from I do not mean from which site you got the example but I meant the site where you want to read data from. I am a c# specialist but I do not use asp.net, so I was just guessing based on common programmers knowledge.
20th May 2019, 9:13 PM
sneeze
sneeze - avatar
0
It is unit test
20th May 2019, 9:14 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
0
But where is the input parameter of your unit test ?
20th May 2019, 9:18 PM
sneeze
sneeze - avatar
0
Input parameter? What do you mean? Had you read this article or not?
20th May 2019, 9:21 PM
Kacper Piotrowski
Kacper Piotrowski - avatar
0
I will admit, I did not read the article first. So I did and I understood. That DefaultHttpContext does not need a site. It gives you some default data so you can test without having a link. My next guess is the method name GetHttpContext is already in use. Your method : private DefaultHttpContext GetHttpContext() ASP.net method GetHttpContext is a method in Microsoft.AspNetCore.SignalR https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.gethttpcontextextensions.gethttpcontext?view=aspnetcore-2.2 This method returns null if not associoated with HTTPRequest So I think the compiler confused and executes the ASP.net method. Try private DefaultHttpContext MyGetHttpContext() and var context = MyGetHttpContext(); Unfortunately I have not been able to test it. Hope it works.
21st May 2019, 8:01 PM
sneeze
sneeze - avatar
0
Still the same :/ DefaultHttpContext was created for testing instead of HttpContext besauce this second class is almost impossible to testing. I did what you wote, I was interested when Repsonse is not null and.... I expanded the context in debug mode and EVERYTHING is null, any property :/ I'm using .Net Core 3 preview 5, maybe this is a reason why this class isn't work properly?
22nd May 2019, 7:06 AM
Kacper Piotrowski
Kacper Piotrowski - avatar