Allow NIOHTTPServer to use NIOAsyncTestingChannel for tests #39
+789
−182
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.