TGViewer
Programming & AI Tips 💡 Programming & AI Tips 💡 @programmingtip · 46.4K subscribers
Post #700 37.3K
WireMock.NET 😮

WireMock.NET is a .NET library for stubbing and mocking HTTP services. With WireMock.NET, you can define the expected responses for particular requests, and the library will intercept and manage those requests for you. ✔️

This allows for easy testing of the code that makes HTTP requests, without having to rely on the actual external service being available and without hacking HttpClient. 🆒

Sample code snippet:

[Test]
public async Task sample_WireMock_usage()
{
// Setup WireMock.Net server
using var wireMock = WireMockServer.StartWithAdminInterface(port: 1080, ssl: false);

// Setup WebApplicationFactory
await using var appFactory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration(configurationBuilder =>
{
// Override downstream service addresses pointing to WireMock address
configurationBuilder.AddInMemoryCollection(new Dictionary<string, string>
{
["ExternalServices:WeatherService"] = "http://localhost:1080"
});
});
});

// Prepare stub for outgoing request
wireMock
.Given(
Request.Create()
.WithPath("/api/v1.0/weather")
.WithParam("lat", "10.99")
.WithParam("lon", "44.34")
.UsingGet()
)
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json; charset=utf-8")
.WithBodyAsJson(new
{
temp = 298.48,
feels_like = 298.74,
temp_min = 297.56,
temp_max = 300.05,
pressure = 1015,
humidity = 64
})
);

// Automate tested app
}


[ Blog ] : https://cezarypiatek.github.io/post/mocking-outgoing-http-requests-p1

〰️〰️〰️〰️〰️〰️
#UnitTest #DotNet #CSharp
@ProgrammingTip
Telegram Programming Tips Resources
More from @programmingtip
  1. Oct 2, 2026GPT-6 Astra gets an Ultrafast API tier 🚀 OpenAI added an Ultrafast speed tier for GPT-6 A…
  2. Oct 1, 2026Cloudflare shipped an agentic CLI for its API ⚡️ Cloudflare launched cf, an agentic CLI fo…
  3. Sep 30, 2026OpenAI introduced Dots, always-on agents 🚀 OpenAI introduced Dots, its take on always-on…
  4. Sep 29, 2026Claude Sonnet 5.5 is built for coding agents 🚀 Anthropic released Claude Sonnet 5.5 for t…
  5. Sep 26, 2026Claude Opus 5.5 is available in the API 🚀 Anthropic released Claude Opus 5.5, and it is a…
  6. Sep 24, 2026Claude Opus 5.5 is now on the Claude Platform 🚀 Anthropic introduced Claude Opus 5.5, a l…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →