1

I'm building an api that uses OAuth2. I've successfully unit tested all of the individual pieces but now I need to do some integration testing. Basically I want to be able to inspect the http responses from the server to make sure everything is in working order. Ideally I'd like to be able to spin up the web developement server in Visual Studio to host the site in and then make a bunch of requests to it and inspect the results.

What's the best approach to doing this and what tools should I be using?

2 Answers 2

2

I would recommend you deploying your application on a staging server (maybe even as a part of the build process so that this would be a single button click action) and then firing the HTTP client requests against this server, the way a real .NET client is supposed to use your API.

Sign up to request clarification or add additional context in comments.

Comments

2

Create a Continuous Integration Server

  1. install TeamCity
  2. install Ruby
  3. install Albacore

Have a rake script do the following 1. check out files from source control 2. build locally 3. deploy api to local iis 4. run integration tests against localhost api

This started to sound familiar. See here

Here is an example from my API integration tests. Let me know if you want more details.

I am using mspec.

I run it aganst localhost, our staging server, and our production server (a limited set of tests) to make sure all http connections are working.

public class _GET_no_criteria : specs_for_endpoint_test
{
    Establish context = () =>
    {
        Uri = C.Endpoint;
        Querystring = "";
        ExecuteJsonGetRequest();

        SetValidId();
    };

    It should_have_status_code_200_ok =()=>
        IsHttp_200OK();

    It should_have_categories = () =>
    {
        responseText.ShouldNotBeEmpty();
        PutsAll(responseText);
    };
}

From the base class

 public static void ExecuteGetRequest(string contentType)
        {
            httpcontext = HttpContext.Current;
            request = (HttpWebRequest)WebRequest.Create(BaseUri + Uri + Querystring);
            request.Method = C.HTTP_GET;          
            request.ContentType = contentType;
            request.Headers[C.AUTHORIZATION] = token;

            // GetResponse reaises an exception on http status code 400
            // We can pull response out of the exception and continue on our way            
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse) ex.Response;
            }

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                responseText = reader.ReadToEnd();
                reader.Close();
            } 
        }

        public static void ExecuteJsonGetRequest()
        {
            ExecuteGetRequest(C.CONTENT_JSON);
        }

4 Comments

I've never used mspec. Does it handle automatically deploying the site or spinning up a web server or something? Or is that still a manual process?
mspec is just a wrapper around nunit. not really necessary. i wasn't sure if you question was about how to actually unit test. if you are more interested in the automation process, I could post a different answer. bascially, the api is deployed to a url on the build server, and the unit tests can run against local host.
I definitely appreciate the answer and I'm going to check out mspec, but I'm looking to find out more about automating the deployment and running tests against it.
i have a framework that helps testing GET, POST, PUT and DELETE requests. It is a base calls that all the integration tests (unit tests) inherit from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.