Skip to content

Fix HTTPBody length calculation for utf8 strings. #60

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

Merged
merged 9 commits into from
Oct 23, 2023
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
7 changes: 2 additions & 5 deletions Sources/OpenAPIRuntime/Interface/HTTPBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public final class HTTPBody: @unchecked Sendable {
public let iterationBehavior: IterationBehavior

/// Describes the total length of the body, if known.
public enum Length: Sendable {
public enum Length: Sendable, Equatable {

/// Total length not known yet.
case unknown
Expand Down Expand Up @@ -542,10 +542,7 @@ extension HTTPBody {
@inlinable public convenience init(
_ string: some StringProtocol & Sendable
) {
self.init(
ByteChunk(string),
length: .known(string.count)
)
self.init(ByteChunk(string))
}

/// Creates a new body with the provided async throwing stream of strings.
Expand Down
7 changes: 7 additions & 0 deletions Tests/OpenAPIRuntimeTests/Interface/Test_HTTPBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ final class Test_Body: Test_Runtime {
XCTAssertEqual(chunks, ["hel", "lo"].map { Array($0.utf8)[...] })
}

func testUTF8String() async throws {
XCTAssertEqual(HTTPBody("abc").length, .known(3))
XCTAssertEqual(HTTPBody("🤘").length, .known(4))
XCTAssertEqual(HTTPBody("\u{1f603}").length, .known(4))
XCTAssertEqual(HTTPBody("árvíztűrő tükörfúrógép").length, .known(31))
}

func testIterationBehavior_single() async throws {
let sequence = AsyncStream(
String.self,
Expand Down