You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was unittesting some REST API code and implemented a test-HttpMessageHandler to be able to return any desired (json/html/whatever) response and statuscode (200 OK, 401 Unauthorized, ...). This is the handler in simplified form:
This works fine; however, whenever I tried anything other than an OK result I got a NullReference exception.
Object reference not set to an instance of an object.
at Refit.DefaultApiExceptionFactory.<CreateExceptionAsync>d__4.MoveNext() in /_/Refit/RefitSettings.cs:line 183
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Refit.RequestBuilderImplementation.<>c__DisplayClass14_0`2.<<BuildCancellableTaskFuncForMethod>b__0>d.MoveNext() in /_/Refit/RequestBuilderImplementation.cs:line 313
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at RefitTest.Program.<Main>d__0.MoveNext() in C:\Users\Rob\Source\Repos\RefitTest\RefitTest\Program.cs:line 18
Long story short: I finally found it: the returned HttpResponseMessage needs a RequestMessage:
publicTestHandler(HttpStatusCode httpStatusCode)=> _response =newHttpResponseMessage(httpStatusCode){RequestMessage=new(),// <-- This one here...Content=newStringContent("Yay!")};
Again: I'm not sure if this is a bug, but we tracked it down to this line, where RequestMessage is null obviously. If anything, maybe a more explicit null check and more specific exception could be thrown?
The text was updated successfully, but these errors were encountered:
@RobThree I believe the built in protocol handlers always provide the incoming request message in the response. I'm not 100% sure of the usage contract in the API, but I've never seen it otherwise.
Rather than a code fix, I'd suggest a documentation update to clarify this. Would you mind submitting a PR to mention that in the docs in the right spot?
I don't mind at all. However, what docs are you referring to? The README.md? Those are all the docs I'm aware of? And I can't really find a good place to add it in the (current) docs. There's no explicit unittesting or messagehandling section so I'd have to mention it somewhere as an aside?
I'm not sure if this is by-design or a bug, but I ran into an issue recently and thought I'd report it.
For reference: I have discussed this on stackoverflow question.
I was unittesting some REST API code and implemented a test-
HttpMessageHandler
to be able to return any desired (json/html/whatever) response and statuscode (200 OK, 401 Unauthorized, ...). This is the handler in simplified form:Then a Refit interface:
Finally, a "test":
This works fine; however, whenever I tried anything other than an OK result I got a
NullReference
exception.Long story short: I finally found it: the returned
HttpResponseMessage
needs aRequestMessage
:Again: I'm not sure if this is a bug, but we tracked it down to this line, where
RequestMessage
is null obviously. If anything, maybe a more explicit null check and more specific exception could be thrown?The text was updated successfully, but these errors were encountered: