-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a miscalculation in SSE's indices during parsing, by normalizing …
…the offset to 0, using `readableBytesView.startIndex`
- Loading branch information
Showing
2 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import XCTVapor | ||
import AsyncHTTPClient | ||
|
||
final class SSETests: XCTestCase { | ||
func testEndToEndUse() async throws { | ||
let app = Application() | ||
defer { app.shutdown() } | ||
|
||
let allEvents = ["hello\nworld", "1", "2", "3"] | ||
|
||
app.get("sse") { req in | ||
try await req.serverSentEvents { producer in | ||
for event in allEvents { | ||
try await producer.sendEvent(.init(data: SSEValue(string: event))) | ||
} | ||
} | ||
} | ||
|
||
app.environment.arguments = ["serve"] | ||
try app.boot() | ||
try app.start() | ||
|
||
guard | ||
let localAddress = app.http.server.shared.localAddress, | ||
let port = localAddress.port | ||
else { | ||
XCTFail("couldn't get port from \(app.http.server.shared.localAddress.debugDescription)") | ||
return | ||
} | ||
|
||
let client = HTTPClient(eventLoopGroupProvider: .createNew) | ||
|
||
let request = HTTPClientRequest(url: "http://localhost:\(port)/sse") | ||
defer { _ = try? client.syncShutdown() } | ||
|
||
let events = try await client.execute(request, timeout: .seconds(1)) | ||
.getServerSentEvents(allocator: app.allocator) | ||
|
||
var expectedEvents = allEvents | ||
for try await event in events { | ||
if expectedEvents.isEmpty { | ||
return XCTFail("Unexpected event received") | ||
} | ||
|
||
XCTAssertEqual(event.data.string, expectedEvents.removeFirst()) | ||
} | ||
|
||
XCTAssertTrue(expectedEvents.isEmpty) | ||
|
||
// retain util the end | ||
_ = app | ||
_ = client | ||
} | ||
} |