mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-29 05:24:22 +00:00
Confirm we can override default settings
This commit is contained in:
1 parent
409da26764
commit
ed7ce3e297
3 files changed
+33
-21
No files matched your search
@@ -44,12 +44,12 @@ public final class HTTP2Parser: ChannelInboundHandler, ChannelOutboundHandler {
|
||||
|
||||
private var session: NGHTTP2Session!
|
||||
private let mode: ParserMode
|
||||
private let defaultSettings: [HTTP2Setting]
|
||||
private let initialSettings: [HTTP2Setting]
|
||||
|
||||
public init(mode: ParserMode, connectionManager: HTTP2ConnectionManager = .init(), defaultSettings: [HTTP2Setting] = nioDefaultSettings) {
|
||||
public init(mode: ParserMode, connectionManager: HTTP2ConnectionManager = .init(), initialSettings: [HTTP2Setting] = nioDefaultSettings) {
|
||||
self.mode = mode
|
||||
self.connectionManager = connectionManager
|
||||
self.defaultSettings = defaultSettings
|
||||
self.initialSettings = initialSettings
|
||||
}
|
||||
|
||||
public func handlerAdded(ctx: ChannelHandlerContext) {
|
||||
@@ -88,7 +88,7 @@ public final class HTTP2Parser: ChannelInboundHandler, ChannelOutboundHandler {
|
||||
}
|
||||
|
||||
private func flushPreamble(ctx: ChannelHandlerContext) {
|
||||
let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(self.defaultSettings))
|
||||
let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(self.initialSettings))
|
||||
self.session.feedOutput(allocator: ctx.channel.allocator, frame: frame, promise: nil)
|
||||
self.flush(ctx: ctx)
|
||||
}
|
||||
|
||||
@@ -369,4 +369,23 @@ class SimpleClientServerTests: XCTestCase {
|
||||
XCTAssertNoThrow(try self.clientChannel.finish())
|
||||
XCTAssertNoThrow(try self.serverChannel.finish())
|
||||
}
|
||||
|
||||
func testOverridingDefaultSettings() throws {
|
||||
let initialSettings = [
|
||||
HTTP2Setting(parameter: .maxHeaderListSize, value: 1000),
|
||||
HTTP2Setting(parameter: .initialWindowSize, value: 100),
|
||||
HTTP2Setting(parameter: .enablePush, value: 0)
|
||||
]
|
||||
XCTAssertNoThrow(try self.clientChannel.pipeline.add(handler: HTTP2Parser(mode: .client, initialSettings: initialSettings)).wait())
|
||||
XCTAssertNoThrow(try self.serverChannel.pipeline.add(handler: HTTP2Parser(mode: .server)).wait())
|
||||
try self.assertDoHandshake(client: self.clientChannel, server: self.serverChannel, clientSettings: initialSettings)
|
||||
|
||||
// There should be no frames here.
|
||||
self.clientChannel.assertNoFramesReceived()
|
||||
self.serverChannel.assertNoFramesReceived()
|
||||
|
||||
// No need to bother cleaning this up.
|
||||
XCTAssertNoThrow(try self.clientChannel.finish())
|
||||
XCTAssertNoThrow(try self.serverChannel.finish())
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,9 @@ extension XCTestCase {
|
||||
///
|
||||
/// If the handshake has not occurred, this will definitely call `XCTFail`. It may also throw if the
|
||||
/// channel is now in an indeterminate state.
|
||||
func assertDoHandshake(client: EmbeddedChannel, server: EmbeddedChannel, file: StaticString = #file, line: UInt = #line) throws {
|
||||
func assertDoHandshake(client: EmbeddedChannel, server: EmbeddedChannel,
|
||||
clientSettings: [HTTP2Setting] = nioDefaultSettings, serverSettings: [HTTP2Setting] = nioDefaultSettings,
|
||||
file: StaticString = #file, line: UInt = #line) throws {
|
||||
// First the channels need to interact.
|
||||
self.interactInMemory(client, server, file: file, line: line)
|
||||
|
||||
@@ -77,12 +79,11 @@ extension XCTestCase {
|
||||
let clientReceivedSettingsAck = try client.assertReceivedFrame(file: file, line: line)
|
||||
let serverReceivedSettingsAck = try server.assertReceivedFrame(file: file, line: line)
|
||||
|
||||
// Check that these SETTINGS frames are ok. Currently we don't actually set any values in here,
|
||||
// but when we do this function can be enhanced to tolerate it.
|
||||
clientReceivedSettings.assertSettingsFrame(ack: false, file: file, line: line)
|
||||
serverReceivedSettings.assertSettingsFrame(ack: false, file: file, line: line)
|
||||
clientReceivedSettingsAck.assertSettingsFrame(ack: true, file: file, line: line)
|
||||
serverReceivedSettingsAck.assertSettingsFrame(ack: true, file: file, line: line)
|
||||
// Check that these SETTINGS frames are ok.
|
||||
clientReceivedSettings.assertSettingsFrame(expectedSettings: serverSettings, ack: false, file: file, line: line)
|
||||
serverReceivedSettings.assertSettingsFrame(expectedSettings: clientSettings, ack: false, file: file, line: line)
|
||||
clientReceivedSettingsAck.assertSettingsFrame(expectedSettings: [], ack: true, file: file, line: line)
|
||||
serverReceivedSettingsAck.assertSettingsFrame(expectedSettings: [], ack: true, file: file, line: line)
|
||||
|
||||
client.assertNoFramesReceived(file: file, line: line)
|
||||
server.assertNoFramesReceived(file: file, line: line)
|
||||
@@ -141,10 +142,7 @@ extension EmbeddedChannel {
|
||||
|
||||
extension HTTP2Frame {
|
||||
/// Asserts that the given frame is a SETTINGS frame.
|
||||
///
|
||||
/// Currently this does not supoport SETTINGS frames with configurable settings in them,
|
||||
/// which won't work when we allow that.
|
||||
func assertSettingsFrame(ack: Bool, file: StaticString = #file, line: UInt = #line) {
|
||||
func assertSettingsFrame(expectedSettings: [HTTP2Setting], ack: Bool, file: StaticString = #file, line: UInt = #line) {
|
||||
guard case .settings(let values) = self.payload else {
|
||||
XCTFail("Expected SETTINGS frame, got \(self.payload) instead", file: file, line: line)
|
||||
return
|
||||
@@ -154,12 +152,7 @@ extension HTTP2Frame {
|
||||
file: file, line: line)
|
||||
XCTAssertEqual(self.ack, ack, "Got unexpected value for ack: expected \(ack), got \(self.ack)",
|
||||
file: file, line: line)
|
||||
XCTAssertEqual(values.count, self.ack ? 0 : nioDefaultSettings.count, "Got settings values \(values), expected one.", file: file, line: line)
|
||||
|
||||
// This is using the raw value, which it really shouldn't.
|
||||
if !self.ack {
|
||||
XCTAssertEqual(values, nioDefaultSettings, file: file, line: line)
|
||||
}
|
||||
XCTAssertEqual(values, expectedSettings, file: file, line: line)
|
||||
}
|
||||
|
||||
/// Asserts that this frame matches a give other frame.
|
||||
|
||||
Reference in new issue
Block a user