Testing ASP.NET APIs with Expecto

It has been a while since I last wrote down something here. However, I believe this to be one of the most useful “tricks” I have learned the past few weeks.

Running “end to end integration tests” on endpoints in F# have always been a struggle for me. While running tests, I want to keep the application both stateless and running the whole test suite concurrently.

Fortunately, Expecto provides an easy way to add fixtures to your tests. This means that we can leverage this feature to start a new web server for each test and guarantee that we always have a clean state at the beginning of a test.

Before starting it, let’s assume some things on our code:

let withApi test = task {
  let builder = Host.CreateDefaultBuilder()

  // passing a ~zero~ port to our app makes ASP.NET assign a random
  // port when initializing the service, this avoids conflicts while
  // running tests in parallel.
  let hostPort = 0
  let host = createHost hostPort builder

  let server = host.Services.GetRequiredService<IServer>()
  let addressFeature = server.Features.Get<IServerAddressesFeature>()

  do! host.StartAsync()

  let port =
    let appUri = addressFeature.Addresses |> Seq.head |> Uri

  let client = new HttpClient()
  client.BaseAddress <- Uri(sprintf "http://localhost:%d" port)

  do! test client
  do! host.StopAsync()

Consuming this fixture is rather easy:

testList "api tests" [
  yield! testFixtureTask withApi [
    "get user", fun client -> task {
      let! user = client.GetAsync("/user/admin")
      // ...

Articles from blogs I follow around the net

The four tenets of SOA revisited

Twenty years after. In the January 2004 issue of MSDN Magazine you can find an article by Don Box titled A Guide to Developing and Running Connected Systems with Indigo. Buried within the (now dated) discussion of the technology…

via ploeh blog March 4, 2024

Building a demo of the Bleichenbacher RSA attack in Rust

Recently while reading Real-World Cryptography, I got nerd sniped1 by the mention of Bleichenbacher's attack on RSA. This is cool, how does it work? I had to understand, and to understand something, I usually have to build it. Well, friends, that is what…

via ntietz.com blog March 4, 2024

How to unbreak Dolphin on SteamOS after the QT6 update

A recent update to Dolphin made it switch to QT6. This makes it crash with this error or something like it: dolphin-emu: symbol lookup error: dolphin-emu: undefined symbol: _Zls6QDebugRK11QDockWidget, version Qt_6 This is fix…

via Xe Iaso's blog March 3, 2024

Generated by openring