0

So the two assertions I am doing is checking the status message which should give me "OK" And I am checking if the API returns 200. But my issue is each request is different of course and that response variable. I am trying to avoid writing the same three lines of Assertions and just call one simple static method

HttpWebResponse response = (HttpWebResponse)sd.GetDataType();
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
statusNumber = (int)response.StatusCode;
Assert.AreEqual(200, statusNumber);
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
Johnny_347
  • 53
  • 1
  • 7
  • I am trying not use the function method "sd.GetDataType()" inside the assertion method as they are different. I would have to create many of them – Johnny_347 Feb 12 '20 at 20:57

3 Answers3

0

All assert methods are static, so you can create your own static method and use Assert inside

public static class AssertExtensions
{
    public static void Assert(YourType sd)
    {
        HttpWebResponse response = (HttpWebResponse)sd.GetDataType();
        Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        statusNumber = (int)response.StatusCode;
        Assert.AreEqual(200, statusNumber);
    }
}

and then use AssertExtensions.Assert(response)

btw if you are using .Net Core there is already method response.EnsureSuccessStatusCode() which throws exception

LP13
  • 30,567
  • 53
  • 217
  • 400
0

HTTP Protocol only sends the following bytes: "200 OK"

Your code is doing is 2 things:

  • Assert that the response of the request is "200 OK" .

  • Assert that the library code is working, as the StatusCode enum gets the right value.

You might want to avoid doing that, we hope MS got you covered, (and also the LOCs that do that)

To avoid repeating that code, make a single method and use it, you could try with:

  • a method on an abstract "BaseTestClass" you inherit from
  • an Extension Method for the HttpWebResponse
  • an static method on some sort of util class.
  • just a private method on the same class.

I would do the first approach, but if you're new to programming you could start trying the last one which will probably be easier to understand.

EDIT

After looking a bit at the HttpResponseMessage reference I would use the IsSuccessStatusCode property:

Assert.IsTrue(response.IsSuccessStatusCode)

And that's it. But this will check if the response is in the range of success (200-299), so maybe you need to additionally check the code, then one of the originally suggested methods may come in handy.

Talk is cheap, show me the code:

public static bool IsOk(this HttpWebResponse response) {
    var isOk = response.IsSuccessStatusCode;
    isOk = isOk && response.StatusCode == 200;
    return isOk;
}

with this method on a static class in a namespace used by your class you could go with:

Assert.IsTrue(response.IsOk());

this code was written directly here, it may not work as is but, I hope you get the idea.

Pablo Recalde
  • 3,334
  • 1
  • 22
  • 47
0

I would make a helper class to test it:

public static class HttpWebResponseDebug
{
    public static void Assert(HttpWebResponse response)
    {
        Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        statusNumber = (int)response.StatusCode;
        Assert.AreEqual(200, statusNumber);        
    }
}

var response = (HttpWebResponse)sd.GetDataType();
HttpWebResponseDebug.Assert(response);
// do your thing..
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57