Allow NIOHTTPServer to use NIOAsyncTestingChannel for tests#39
Merged
aryan-25 merged 9 commits intoswift-server:mainfrom Jan 21, 2026
Merged
Allow NIOHTTPServer to use NIOAsyncTestingChannel for tests#39aryan-25 merged 9 commits intoswift-server:mainfrom
aryan-25 merged 9 commits intoswift-server:mainfrom
Conversation
gjcairo
requested changes
Jan 19, 2026
| } | ||
| } | ||
|
|
||
| func serveInsecureHTTP1_1WithTestChannel( |
Collaborator
There was a problem hiding this comment.
Can we move this to some utility in the tests module?
| // So, we create a client channel, and use it to send requests and observe responses in terms of HTTP types. | ||
| let (clientTestChannel, clientAsyncChannel) = try await self.setUpClientConnection() | ||
|
|
||
| try await withThrowingTaskGroup { group in |
Collaborator
There was a problem hiding this comment.
I think this can be a throwingDiscardingTaskGroup, and we can get rid of the group.next() at the end.
| } | ||
| } | ||
|
|
||
| func serveSecureUpgradeWithTestChannel( |
Collaborator
There was a problem hiding this comment.
Same as for H1, can we move this to the test module?
| try await outbound.write(.head(.init(method: .get, scheme: "", authority: "", path: "/"))) | ||
| try await outbound.write(.end(nil)) | ||
|
|
||
| outerLoop: for try await response in inbound { |
Collaborator
There was a problem hiding this comment.
Can we manually drive an iterator for inbound so we can assert that we're getting each of the parts in order too?
| try await outbound.write(.head(.init(method: .get, scheme: "", authority: "", path: "/"))) | ||
| try await outbound.write(.end(nil)) | ||
|
|
||
| outerLoop: for try await response in inbound { |
Collaborator
There was a problem hiding this comment.
Same here as in the other test
gjcairo
approved these changes
Jan 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation:
Currently, we perform end-to-end tests by starting
NIOHTTPServeron localhost and setting up a HTTP client to send requests / observe responses.NIOHTTPServerinternally usesServerBootstrapto set up the server, butServerBootstrapdoes not expose the child channels it creates for each connection, makingNIOHTTPServerdifficult to test closely.We can improve this by refactoring
NIOHTTPServerto also (in tests) allow aNIOAsyncTestingChannelto be used as the channel the server runs on, and manually feed/extract data from it to test different expectations.Modifications:
serveInsecureHTTP1_1andserveSecureUpgrademethods to new files (NIOHTTPServer+HTTP1_1.swiftandNIOHTTPServer+SecureUpgrade.swift).serveInsecureHTTP1_1WithTestChannelandserveSecureUpgradeWithTestChannelmethods, which set up the inputNIOAsyncTestingChannelas the channel the server runs on.HTTP1ClientServerProviderandHTTPSecureUpgradeClientServerProvider, 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:):bodyclosure.func withConnectedClient(body:):NIOAsyncChannelto thebodyclosure which can be used to send requests / observe responses in terms of Swift HTTP types.withConnectedClientvends aNegotiatedConnectionenum, which, for HTTP/1.1 gives you the clientNIOAsyncChannel, or for HTTP/2, aHTTP2StreamManagerwhich has anopenStream()method returning a client stream channel.NIOHTTPServerEndToEndTests.swiftwhich use these new provider types.Result:
Infrastructure in place to write end-to-end tests.