Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a nicer description for WebSocketFrame. #2862

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Sources/NIOWebSocket/WebSocketFrame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,46 @@ extension WebSocketFrame._Storage: Equatable {
lhs.data == rhs.data && lhs.extensionData == rhs.extensionData
}
}

extension WebSocketFrame: CustomStringConvertible {
/// A `String` describing this `WebSocketFrame`. Example:
///
/// WebSocketFrame {
/// maskKey: Optional(NIOWebSocket.WebSocketMaskingKey(_key: (187, 28, 185, 79))),
/// fin: true,
/// rsv1: true,
/// rsv2: true,
/// rsv3: true,
/// opcode: WebSocketOpcode.binary,
/// length: 0,
/// data: ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, capacity: 0, storageCapacity: 0, slice: _ByteBufferSlice { 0..<0 }, storage: 0x00006000028246b0 (0 bytes) },
/// extensionData: nil,
/// unmaskedData: ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, capacity: 0, storageCapacity: 0, slice: _ByteBufferSlice { 0..<0 }, storage: 0x0000600002824800 (0 bytes) },
/// unmaskedDataExtension: nil
/// }
///
/// The format of the description is not API.
///
/// - returns: A description of this `WebSocketFrame`.
public var description: String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please avoid adding a multiline string as a description here. description is often used in other interpolated strings such as log messages and adding line breaks can often break those formats. Instead can we add a single line for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, we should add \s to the end of the line (such that the result is just one line). I missed that currently we'll actually print multiple lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FranzBusch very good point, thanks! I will fix it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a multiline string from what I can see. Can you use " instead of """ unless I am missing something here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FranzBusch no it's not, it has \s everywhere but the last line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1> print(""" 
  2.     hello \ 
  3.     my \ 
  4.     beautiful \ 
  5.     world 
  6.     """)
hello my beautiful world

"""
maskKey: \(String(describing: self.maskKey)), \
fin: \(self.fin), \
rsv1: \(self.rsv1), \
rsv2: \(self.rsv2), \
rsv3: \(self.rsv3), \
opcode: \(self.opcode), \
length: \(self.length), \
data: \(String(describing: self.data)), \
extensionData: \(String(describing: self.extensionData)), \
unmaskedData: \(String(describing: self.unmaskedData)), \
unmaskedDataExtension: \(String(describing: self.unmaskedExtensionData))
"""
}
}

extension WebSocketFrame: CustomDebugStringConvertible {
public var debugDescription: String {
"(\(self.description))"
}
}
62 changes: 62 additions & 0 deletions Tests/NIOWebSocketTests/WebSocketFrameDecoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,66 @@ public final class WebSocketFrameDecoderTest: XCTestCase {
// We expect that an error frame will have been written out.
XCTAssertNoThrow(XCTAssertEqual([0x88, 0x02, 0x03, 0xF1], try self.decoderChannel.readAllOutboundBytes()))
}

func testWebSocketFrameDescription() {
let byteBuffer = ByteBuffer()
let webSocketFrame = WebSocketFrame(
fin: true,
rsv1: true,
rsv2: true,
rsv3: true,
opcode: .binary,
maskKey: nil,
data: byteBuffer,
extensionData: nil
)

let expectedOutput = """
maskKey: nil, \
fin: true, \
rsv1: true, \
rsv2: true, \
rsv3: true, \
opcode: WebSocketOpcode.binary, \
length: 0, \
data: \(String(describing: byteBuffer)), \
extensionData: nil, \
unmaskedData: \(String(describing: byteBuffer)), \
unmaskedDataExtension: nil
"""

XCTAssertEqual(expectedOutput, String(describing: webSocketFrame))
}

func testWebSocketFrameDebugDescription() {
let byteBuffer = ByteBuffer()
let webSocketFrame = WebSocketFrame(
fin: true,
rsv1: true,
rsv2: true,
rsv3: true,
opcode: .binary,
maskKey: nil,
data: byteBuffer,
extensionData: nil
)

let expectedOutput = """
(\
maskKey: nil, \
fin: true, \
rsv1: true, \
rsv2: true, \
rsv3: true, \
opcode: WebSocketOpcode.binary, \
length: 0, \
data: \(String(describing: byteBuffer)), \
extensionData: nil, \
unmaskedData: \(String(describing: byteBuffer)), \
unmaskedDataExtension: nil\
)
"""

XCTAssertEqual(expectedOutput, webSocketFrame.debugDescription)
}
}
Loading