forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationResponseTests.swift
62 lines (53 loc) · 2.45 KB
/
NotificationResponseTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import XCTest
import NIOCore
import NIOTestUtils
@testable import PostgresNIO
class NotificationResponseTests: XCTestCase {
func testDecode() {
let expected: [PostgresBackendMessage] = [
.notification(.init(backendPID: 123, channel: "test", payload: "hello")),
.notification(.init(backendPID: 123, channel: "test", payload: "world")),
.notification(.init(backendPID: 123, channel: "foo", payload: "bar"))
]
var buffer = ByteBuffer()
expected.forEach { message in
guard case .notification(let notification) = message else {
return XCTFail("Expected only to get notifications here!")
}
buffer.writeBackendMessage(id: .notificationResponse) { buffer in
buffer.writeInteger(notification.backendPID)
buffer.writeNullTerminatedString(notification.channel)
buffer.writeNullTerminatedString(notification.payload)
}
}
XCTAssertNoThrow(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(buffer, expected)],
decoderFactory: { PostgresBackendMessageDecoder(hasAlreadyReceivedBytes: true) }))
}
func testDecodeFailureBecauseOfMissingNullTermination() {
var buffer = ByteBuffer()
buffer.writeBackendMessage(id: .notificationResponse) { buffer in
buffer.writeInteger(Int32(123))
buffer.writeString("test")
buffer.writeString("hello")
}
XCTAssertThrowsError(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(buffer, [])],
decoderFactory: { PostgresBackendMessageDecoder(hasAlreadyReceivedBytes: true) })) {
XCTAssert($0 is PostgresMessageDecodingError)
}
}
func testDecodeFailureBecauseOfMissingNullTerminationInValue() {
var buffer = ByteBuffer()
buffer.writeBackendMessage(id: .notificationResponse) { buffer in
buffer.writeInteger(Int32(123))
buffer.writeNullTerminatedString("hello")
buffer.writeString("world")
}
XCTAssertThrowsError(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(buffer, [])],
decoderFactory: { PostgresBackendMessageDecoder(hasAlreadyReceivedBytes: true) })) {
XCTAssert($0 is PostgresMessageDecodingError)
}
}
}