Skip to content

Document extended HttpClient features #422

@vbreuss

Description

@vbreuss

Description

The HttpClient section is missing documentation for several matchers and HTTP methods.

Missing Documentation

  • It.IsBinaryContent() matcher
  • .ForHttps(), .ForHttp() URI scheme filters
  • .WithBodyMatching() content body matching (appears in example but not explained)
  • PUT, DELETE, PATCH methods (only GET and POST shown)
  • CancellationToken support

Suggested Documentation

Expand the "Mocking HttpClient" section with subsections for:

  1. All HTTP Methods (PUT, DELETE, PATCH)
  2. Binary Content Matching
  3. URI Scheme Filtering
  4. Content Body Matching (explain the feature shown in example)
  5. CancellationToken Support

All HTTP Methods

Mockolate supports all standard HTTP methods:

// PUT
httpClient.SetupMock.Method.PutAsync(
    It.IsAny<string>(), It.IsAny<HttpContent>())
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

// DELETE
httpClient.SetupMock.Method.DeleteAsync(It.IsAny<string>())
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.NoContent));

// PATCH
httpClient.SetupMock.Method.PatchAsync(
    It.IsAny<string>(), It.IsAny<HttpContent>())
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

Binary Content Matching

httpClient.SetupMock.Method.PostAsync(
    It.IsAny<string>(),
    It.IsBinaryContent("application/octet-stream"))
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

URI Scheme Filtering

// Match only HTTPS requests
httpClient.VerifyMock.Invoked.GetAsync(
    It.IsUri("*api.example.com*").ForHttps())
    .Once();

// Match only HTTP requests
httpClient.VerifyMock.Invoked.GetAsync(
    It.IsUri("*localhost*").ForHttp())
    .Never();

Content Body Matching

httpClient.VerifyMock.Invoked.PostAsync(
    It.IsAny<string>(),
    It.IsStringContent("application/json")
        .WithBodyMatching("*\"type\": \"Dark\"*"))
    .Once();

CancellationToken Support

All HTTP methods support CancellationToken parameters:

var cts = new CancellationTokenSource();
httpClient.SetupMock.Method.GetAsync(
    It.IsAny<string>(), It.IsAny<CancellationToken>())
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

await httpClient.GetAsync("https://example.com", cts.Token);

Metadata

Metadata

Labels

documentationImprovements or additions to documentationstate: releasedThe issue is released

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions