Skip to content

Conversation

@aryan-25
Copy link
Collaborator

@aryan-25 aryan-25 commented Jan 8, 2026

Motivation:

Currently, we perform end-to-end tests by starting NIOHTTPServer on localhost and setting up a HTTP client to send requests / observe responses. NIOHTTPServer internally uses ServerBootstrap to set up the server, but ServerBootstrap does not expose the child channels it creates for each connection, making NIOHTTPServer difficult to test closely.

We can improve this by refactoring NIOHTTPServer to also (in tests) allow a NIOAsyncTestingChannel to be used as the channel the server runs on, and manually feed/extract data from it to test different expectations.

Modifications:

  • Moved the serveInsecureHTTP1_1 and serveSecureUpgrade methods to new files (NIOHTTPServer+HTTP1_1.swift and NIOHTTPServer+SecureUpgrade.swift).
    • Split the server channel and child channel setup into different methods.
    • Added serveInsecureHTTP1_1WithTestChannel and serveSecureUpgradeWithTestChannel methods, which set up the input NIOAsyncTestingChannel as the channel the server runs on.
  • Added HTTP1ClientServerProvider and HTTPSecureUpgradeClientServerProvider, which can be used in tests to send requests / observe responses. These types currently have the following methods (this is very much open for discussion):
    • static func withProvider(handler:body:):
      • Sets up the server with the provided request handler closure, and vends a provider instance to the body closure.
    • func withConnectedClient(body:):
      • Sets up a connection and vends the client NIOAsyncChannel to the body closure which can be used to send requests / observe responses in terms of Swift HTTP types.
      • For Secure Upgrade, withConnectedClient vends a NegotiatedConnection enum, which, for HTTP/1.1 gives you the client NIOAsyncChannel, or for HTTP/2, a HTTP2StreamManager which has an openStream() method returning a client stream channel.
  • Added two example test-cases in NIOHTTPServerEndToEndTests.swift which use these new provider types.

Result:

Infrastructure in place to write end-to-end tests.

@aryan-25 aryan-25 marked this pull request as draft January 8, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant