Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnzhou committed Jan 6, 2025
1 parent a25f4ec commit 0ce88ca
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Sources/NIOWebSocket/NIOWebSocketClientUpgrader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private func _upgrade<UpgradeResult>(
)
if enableAutomaticErrorHandling {
let errorHandler = WebSocketProtocolErrorHandler()
errorHandler.isServer = false
errorHandler.setIsServer(false)
try channel.pipeline.syncOperations.addHandler(errorHandler)
}
}
Expand Down
10 changes: 9 additions & 1 deletion Sources/NIOWebSocket/WebSocketProtocolErrorHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class WebSocketProtocolErrorHandler: ChannelInboundHandler {
public typealias OutboundOut = WebSocketFrame

/// Indicate that this `ChannelHandeler` is used by a WebSocket server or client. Default is true.
public var isServer: Bool
public private(set) var isServer: Bool

public init() {
self.isServer = true
Expand Down Expand Up @@ -56,6 +56,14 @@ public final class WebSocketProtocolErrorHandler: ChannelInboundHandler {
// A server *must not* mask any frames that it sends to the client
self.isServer ? nil : .random()
}

/// Configure this `ChannelHandler` to be used by a WebSocket server or client.
///
/// - Parameters:
/// - isServer: indicate whether this `ChannelHandler` is used by a WebSocket server or client.
public func setIsServer(_ isServer: Bool) {
self.isServer = isServer
}
}

@available(*, unavailable)
Expand Down
53 changes: 0 additions & 53 deletions Tests/NIOWebSocketTests/WebSocketFrameDecoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -625,59 +625,6 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual([0x88, 0x02, 0x03, 0xF1], try self.decoderChannel.readAllOutboundBytes()))
}

func testErrorHandlerMaskFrameForClient() throws {
// We need to insert a decoder that doesn't do error handling, and then a separate error
// handler.
self.swapDecoder(for: ByteToMessageHandler(WebSocketFrameDecoder()))

let errorHandler = WebSocketProtocolErrorHandler()
errorHandler.isServer = false
XCTAssertNoThrow(
try self.decoderChannel.pipeline.syncOperations.addHandler(errorHandler)
)

// A fake frame header that claims that the length of the frame is 16385 bytes,
// larger than the frame max.
self.buffer.writeBytes([0x81, 0xFE, 0x40, 0x01])

XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
XCTAssertEqual(.invalidFrameLength, error as? NIOWebSocketError)
}

let frame = try self.decoderChannel.readOutbound(as: WebSocketFrame.self)
guard let frame = frame else {
// We expect that an error frame will have been written out.
XCTFail("WebSocketFrame should have been written out.")
return
}
XCTAssertNotNil(frame.maskKey)
}

func testErrorHandlerNotMaskFrameForServer() throws {
// We need to insert a decoder that doesn't do error handling, and then a separate error
// handler.
self.swapDecoder(for: ByteToMessageHandler(WebSocketFrameDecoder()))
XCTAssertNoThrow(
try self.decoderChannel.pipeline.syncOperations.addHandler(WebSocketProtocolErrorHandler())
)

// A fake frame header that claims that the length of the frame is 16385 bytes,
// larger than the frame max.
self.buffer.writeBytes([0x81, 0xFE, 0x40, 0x01])

XCTAssertThrowsError(try self.decoderChannel.writeInbound(self.buffer)) { error in
XCTAssertEqual(.invalidFrameLength, error as? NIOWebSocketError)
}

let frame = try self.decoderChannel.readOutbound(as: WebSocketFrame.self)
guard let frame = frame else {
// We expect that an error frame will have been written out.
XCTFail("WebSocketFrame should have been written out.")
return
}
XCTAssertNil(frame.maskKey)
}

func testWebSocketFrameDescription() {
let byteBuffer = ByteBuffer()
let webSocketFrame = WebSocketFrame(
Expand Down

0 comments on commit 0ce88ca

Please sign in to comment.