mardi 7 avril 2015

How can I write a unit test to check the http status code when HttpException is thrown?

I am coding an API client, which should throw a System.Web.HttpException with the appropriate HTTP status code when the request is not successful. I know that I can test that HttpException is thrown by using the [ExpectedException(typeof(HttpException))] attribute, but this won't tell me that the status code was correct. How can I assert that the status code is correct?


Here is my client:



public static async Task<HttpResponseMessage> SubmitRequest(string endPoint, string apiKey)
{
ServerResponse serverMessage = new ServerResponse();
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format( "{0}:", apiKey)));

using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost/api/v1/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", credentials );

// HTTP GET
HttpResponseMessage response = await client.GetAsync(endPoint);
// if response status code is in the range 200-299
if (response.IsSuccessStatusCode)
{
return response;
}

// request was not successful
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new HttpException(401, "Not authorized.");
}
}
}

Aucun commentaire:

Enregistrer un commentaire