22

I have a controller that generates an exception from the following code with the following message:-

public HttpResponseMessage PutABook(Book bookToSave)
{
   return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "No Permission");
}

am testing this method with the following code:-

var response = controller.PutABook(new Book());
Assert.That(response.StatusCode,Is.EqualTo(HttpStatusCode.Forbidden));
Assert.That(response.Content,Is.EqualTo("No Permission"));

But am getting an error that the content is not "No Permission". It seems I can't cast the response to an HttpError either to get the message content "No Permission". The status code is returned fine. Just struggling to get the message content.

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
TDD Junkie
  • 223
  • 1
  • 2
  • 5
  • I seem to have some sort of solution to this now which is to use the response.Content.ReadAsAsync().Result to give me the message. Not sure if there is a better way though. – TDD Junkie Jun 13 '13 at 08:03

4 Answers4

20

As you figured in your comment, you could either use response.Content.ReadAsAsync<HttpError>() or you could also use response.TryGetContentValue<HttpError>(). In both these cases, the content is checked to see if its of type ObjectContent and the value is retrieved from it.

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • Ahh yes I hadn't tried the TryGetContentValue. Will give that a go too. Thanks :) – TDD Junkie Jun 13 '13 at 08:19
  • 4
    When I saw you mention TryGetContentValue I was puzzled because I had never seen that before and it isn't async. Now I realize that it is only really useful for dealing with responses on the server before they have been transmitted over the wire. I wonder how many people will try using that on the client and wonder why it never returns a valid result. – Darrel Miller Jun 14 '13 at 00:54
  • yeah...right...its useful only at the server...`TryGetContentValue` is present in `System.Web.Http` dll, so the clients shouldn't be seeing it in majority of the cases. – Kiran Jun 14 '13 at 17:55
6

Try this one. response.Content.ReadAsAsync<HttpError>().Result.Message;

aiapatag
  • 3,355
  • 20
  • 24
  • A bit necro-posty, but this gave the answer I needed in the most succinct form, thanks! – William T. Mallard Apr 11 '16 at 01:36
  • 1
    It would be better to await on the async for two reasons: 1. It opens up resources to be used to handle other requests while this is happening. 2. .Result is notorious for deadlock issues and it's really complicated to explain to a junior or intermediate dev when it is and isn't ok to use .Result, so general best practise is avoid at all costs. As such Nikoloz's answer is better: var errorContent = await response.Content.ReadAsAsync(); – k29 May 13 '21 at 13:11
3

You can try the following:

var errorContent = await response.Content.ReadAsAsync<HttpError>();
Assert.That(errorContent.Message,Is.EqualTo("No Permission"));
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
2

read error message this way.

var ErrMsg = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
Pr0mis PAtel
  • 310
  • 4
  • 12