Testing REST API endpoints
<p>I have a number of REST api endpoints that I am calling via ajax from a web  client, and I want to write some automated tests to insure that they work properly outside of a web browser.</p>

<p>I am writing them as unit tests Tests and here is what I have so far:</p>

<pre><code>[TestClass]
public class ApiTests
{
    string local_host_address = "http://localhost:1234//";

    public async Task&lt;string&gt; Post(string path, IEnumerable&lt;KeyValuePair&lt;string, string&gt;&gt; parameters)
    {
        using (var client = new HttpClient())
        {
            client.Timeout = new TimeSpan(0,0,5);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response_message = await client.PostAsync(local_host_address + path, new FormUrlEncodedContent(parameters));
            var response = await response_message.Content.ReadAsStringAsync();

            if (response_message.IsSuccessStatusCode)
            {
                return response;
            }
            else
            {
                throw new Exception("Request failed");
            }
        }
    }

    [TestMethod]
    [TestCategory("ApiTests")]
    public void TestLogon()
    {
        var parameters = new Dictionary&lt;string, string&gt;();
        parameters["email"] = "bob@aol.com";
        parameters["password"] = "rosebud";

        Task.Run( () =&gt;
        {
            var output = Post("Default.aspx/Logon", parameters);
            Console.WriteLine(output.Result);
        }).Wait();
    }
}
</code></pre>

<p>...pretty basic, it just tries to call a specific endpoint, and return the results. Problem is, this call returns the basic default.aspx web page body, not the results generated by default.aspx/logon. I am doing something wrong, but I have been over it with a debugger and I cannot see my error. The default.aspx/logon endpoint exists and it works perfectly when I access it via website. Am I missing or overlooking something?</p>

<p>-TTM</p>

<p><strong>SOLUTION:</strong> </p>

<p>Bruno's alteration of my code snippet works quite nicely. Anyone else trying to solve the problem of testing a REST endpoint can just put that into a unit test and pass in a POCO and it will return the JSON response.  </p>