HttpClient.Helpers icon indicating copy to clipboard operation
HttpClient.Helpers copied to clipboard

Possibility to assert invoked Uris?

Open Kralizek opened this issue 6 years ago • 4 comments

Hi,

I'm using your library to author some tests and I got myself in the situation where my API client is forming a URL by adding many components both in the path and the querystring.

My test is targeting only a subsection of the requested URI and I would like to "test" it. Ideally on the used option.

To make a concrete example

var option = new HttpMessageOptions
{
    HttpMethod = HttpMethod.Get,
    HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(JsonConvert.SerializeObject(contact))
    },
    RequestUri = null 
};

var sut = CreateSystemUnderTest(option);

var response = await sut.GetByIdAsync(contactId);

Assert.That(option.InvokedUri, Contains.SubString($"/contacts/v1/contact/vid/{contactId}"));

Right now I am forced to do:

var options = new HttpMessageOptions
{
    HttpMethod = HttpMethod.Get,
    HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(JsonConvert.SerializeObject(contact))
    },
    RequestUri = new Uri($"https://api.hubapi.com/contacts/v1/contact/vid/{contactId}/profile?formSubmissionMode=all&propertyMode=value_and_history&showListMemberships=true")
};

which is really outside of the scope of my unit test.

Kralizek avatar Feb 13 '18 15:02 Kralizek

Hi again @Kralizek 👋

Hmm .. lets see here ...I first added this way back in #12 over here.

I just need to add some documentation to show how to do this.

In the mean time, here's an example using your code-example, above:

// Act.
var response = await sut.GetByIdAsync(contactId);

// Assert.
Assert.That(option.NumberOfTimesCalled, 1);

So what is happening is that we check that all the conditions of the Options was met and passed and the number of times it was met.

Does this help?

PureKrome avatar May 28 '19 03:05 PureKrome

I'm not sure it would help my case (although I barely remember what I was working on back then).

I guess this would go in hand with the default registration we are discussing in #32.

Let RequestUri be optional and allow the test on the actual call.

Back to the first snippet, you could read its assertion as "I will receive a GET request whose URI will contain a subscring matching this pattern".

Kralizek avatar May 28 '19 10:05 Kralizek

Currently, options.RequestUri is optional. 👍

"I will receive a GET request whose URI will contain a substring matching this pattern".

kewl! to do that, then you do exactly as you did, above ... and make then test that the option you just made, was called :)

which is the code I did above. Kewl!

PureKrome avatar May 28 '19 10:05 PureKrome

Thanks! I'll check later :)

Kralizek avatar May 28 '19 10:05 Kralizek