0

I was trying to put an assert for Response Code for my request, but i am having hard time to figure out, could you please help me on this. Here is my implementation and definition.

myTests.cs

var accessToken = await helper.SendRequestAsync<AccessToken>(baseUrl, body);

==> how to set assert here right after above statement to verify response status?

helpers.cs

public static async Task<T> SendRequestAsync<T>(string baseUrl, Dictionary<string, string> body)
{
    using (var flurl_client = new FlurlClient(baseurl))
    {
        try
        {
            var response = await flurl_client
                            .Request()
                            .PostUrlEncodedAsync(body)
                            .ReceiveJson<T>();
            return response;
        }
        catch (Exception ex)
        {
            Assert.Fail(ex.Message);
        }
        return default(T);
    }
}

======================================================

Data model for "AccessToken" is in Dto.cs

public class AccessToken
{
    public string token_type { get; set; }
    public string expires_in { get; set; }
    public string ext_expires_in { get; set; }
    public string expires_on { get; set; }
    public string not_before { get; set; }
    public string resource { get; set; }
    public string access_token { get; set; }
    public string refresh_token { get; set; }
    public object Status_Code { get; set; }
}
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
Kal
  • 3
  • 3

1 Answers1

0

If you're you looking for Flurl's testing features to help with this, I'm afraid it won't work. Those features are specifically designed for testing the behavior of your client-side code based on fake responses that you set up in your test. It looks like you want to assert the status code from a real call.

The best way I can think of is to drop the .ReceiveJson<T>() line in SendRequestAsync and change the method signature to return Task<HttpResponseMessage>:

using System.Net.Http;

public static async Task<HttpResponseMessage> SendRequestAsync(string baseUrl, Dictionary<string, string> body)
{
    using (var flurl_client = new FlurlClient(baseurl))
    {
        try
        {
            var response = await flurl_client
                .Request()
                .PostUrlEncodedAsync(body); // this returns Task<HttpResponseMessage>

            return response;
        }
        catch (Exception ex)
        {
            Assert.Fail(ex.Message);
        }
        return null;
    }
}

Then your test can do this:

var resp = await Helper.SendRequestAsync(...);
Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode);

Anything that needs the deserialized response body can do this:

var token = await Helper.SendRequestAsync(...).ReceiveJson<AccessToken>();
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
  • Thank you Todd for your answer, i am new to this api testing framework, Could you please explain where should write Task statement in method because it is trowing an error to me. – Kal Feb 08 '19 at 06:52
  • Because i would like to get response message and response code returned from same method, is that possible? – Kal Feb 08 '19 at 07:06
  • Answer updated. Note that I edited your question and changed your method name to `SendRequestAsync`, just to make it more readable for others. – Todd Menier Feb 08 '19 at 16:12
  • Is there a way that we can get both success response and un-success response returned when send good and bad data out of this method and along with response body, instead calling the statements 2 times as you recommended. here is how i am doing in catch block of method, please advise if there is a better way To returned bad response response in method catch (FlurlHttpException ex) { return ex.Call.Response; } To call var resp = await Helper.SendRequestAsync(...); string responseMessage = resp.Content.ReadAsStringAsync().Result; Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); – Kal Feb 16 '19 at 02:11