Not sure if this is ideal, but if I understand your question, you're really looking for an integration test to make sure that a certain URI is available. You're not really looking to unit test the implementation of the service -- you want to make a request to the URI and inspect the response.
Here's the NUnit TestFixture
that I'd set up to run this. Please note that this was put together pretty quickly and can definitely be improved upon....
I used the WebRequest object to make the request and get the response back. When the request is made, it's wrapped in a try...catch
because if the request returns anything but a 200-type response, it will throw a WebException. So I catch the exception and get the WebResponse
object from the exception's Response
property. I set my StatusCode
variable at that point and continue evaluating the returned value.
Hopefully this helps. If I'm misunderstanding your question, please let me know and I'll update accordingly. Good luck!
TEST CODE:
[TestFixture]
public class WebRequestTests : AssertionHelper
{
[TestCase("http://www.cnn.com", 200)]
[TestCase("http://www.foobar.com", 403)]
[TestCase("http://www.cnn.com/xyz.htm", 404)]
public void when_i_request_a_url_i_should_get_the_appropriate_response_statuscode_returned(string url, int expectedStatusCode)
{
var webReq = (HttpWebRequest)WebRequest.Create(url);
webReq.Method = "GET";
HttpWebResponse webResp;
try
{
webResp = (HttpWebResponse)webReq.GetResponse();
//log a success in a file
}
catch (WebException wexc)
{
webResp = (HttpWebResponse)wexc.Response;
//log the wexc.Status and other properties that you want in a file
}
HttpStatusCode statusCode = webResp.StatusCode;
var answer = webResp.GetResponseStream();
var result = string.Empty;
if (answer != null)
{
using (var tempStream = new StreamReader(answer))
{
result = tempStream.ReadToEnd();
}
}
Expect(result.Length, Is.GreaterThan(0), "result was empty");
Expect(((int)statusCode), Is.EqualTo(expectedStatusCode), "status code not correct");
}
}