Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@yorickpeterse
Copy link
Collaborator

@yorickpeterse yorickpeterse commented Oct 15, 2025

TODO

  • A mock HTTP server to make testing HTTP clients possible
  • Creating of std.net.http.server.Request values to make testing HTTP servers possible/easier

Sketch code that I'm using:

test.inko
import std.net.http.client (Client)
import std.net.http.server (Response)
import std.net.http.test (Server)
import std.test (Tests)
import std.uri (Uri)

type async Main {
  fn async main {
    let tests = Tests.new

    tests.test('Example test', fn (t) {
      let server = Server.new(t, fn (srv) {
        srv
          .post('/')
          .url_encoded_form(fn (f) {
            f.add('name', 'Alice')
            f.add('age', '42')
          })
          .then(fn { Response.new.string('created') })
      })

      let client = Client.new

      server.prepare_client(client)

      let body = ByteArray.new
      let form = client.post(Uri.parse('/').or_panic).url_encoded_form

      form.add('name', 'Alice')
      form.add('age', '42')

      let resp = form.send.or_panic
      let _ = resp.body.read_all(body).or_panic

      t.equal(body.to_string, 'created')
    })

    tests.run
  }
}

@yorickpeterse yorickpeterse added this to the 0.19.0 milestone Oct 15, 2025
@yorickpeterse yorickpeterse self-assigned this Oct 15, 2025
@yorickpeterse yorickpeterse added feature New things to add to Inko, such as a new standard library module std Changes related to the standard library HTTP Work related to the std.net.http module. labels Oct 15, 2025
@yorickpeterse yorickpeterse force-pushed the http-mocking branch 6 times, most recently from b76ff52 to 227c8e6 Compare October 18, 2025 00:09
This allows a ByteArray to be used as an in-memory buffer.

Changelog: added
This adds the module std.net.http.test, providing support for mocking
HTTP requests and testing servers.

Mocking is achieved by spinning up a std.net.http.server.Server per test
and defining mock responses for each expected request. This makes it
possible to test HTTP clients (e.g. std.net.http.client.Client
instances) without the need for changing application logic.

For testing HTTP servers one can use the
std.net.http.test.RequestBuilder type. This type is used to build a
regular std.net.http.server.Request and send it to a type that
implements std.net.http.server.Handle, allowing you to test HTTP
applications without the need for a real HTTP client or HTTP server.

This fixes #913.

Changelog: added
@yorickpeterse yorickpeterse marked this pull request as ready for review October 22, 2025 16:03
@yorickpeterse yorickpeterse merged commit 2c70371 into main Oct 22, 2025
46 checks passed
@yorickpeterse yorickpeterse deleted the http-mocking branch October 22, 2025 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New things to add to Inko, such as a new standard library module HTTP Work related to the std.net.http module. std Changes related to the standard library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant