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.