11import XCTest
22import NIO
3+ import NIOTLS
34@testable import PostgresNIO
45
56class PSQLChannelHandlerTests : XCTestCase {
@@ -8,7 +9,7 @@ class PSQLChannelHandlerTests: XCTestCase {
89
910 func testHandlerAddedWithoutSSL( ) {
1011 let config = self . testConnectionConfiguration ( )
11- let handler = PSQLChannelHandler ( authentification: config. authentication)
12+ let handler = PSQLChannelHandler ( authentification: config. authentication, configureSSLCallback : nil )
1213 let embedded = EmbeddedChannel ( handler: handler)
1314 defer { XCTAssertNoThrow ( try embedded. finish ( ) ) }
1415
@@ -35,7 +36,6 @@ class PSQLChannelHandlerTests: XCTestCase {
3536 var addSSLCallbackIsHit = false
3637 let handler = PSQLChannelHandler ( authentification: config. authentication) { channel in
3738 addSSLCallbackIsHit = true
38- return channel. eventLoop. makeSucceededFuture ( ( ) )
3939 }
4040 let embedded = EmbeddedChannel ( handler: handler)
4141
@@ -48,14 +48,24 @@ class PSQLChannelHandlerTests: XCTestCase {
4848
4949 XCTAssertEqual ( request. code, 80877103 )
5050
51- // first we need to add an encoder, because NIOSSLHandler can only
52- // operate on ByteBuffer
53- let future = embedded. pipeline. addHandlers ( MessageToByteHandler ( PSQLFrontendMessage . Encoder. forTests) , position: . first)
54- XCTAssertNoThrow ( try future. wait ( ) )
5551 XCTAssertNoThrow ( try embedded. writeInbound ( PSQLBackendMessage . sslSupported) )
5652
5753 // a NIOSSLHandler has been added, after it SSL had been negotiated
5854 XCTAssertTrue ( addSSLCallbackIsHit)
55+
56+ // signal that the ssl connection has been established
57+ embedded. pipeline. fireUserInboundEventTriggered ( TLSUserEvent . handshakeCompleted ( negotiatedProtocol: " " ) )
58+
59+ // startup message should be issued
60+ var maybeStartupMessage : PSQLFrontendMessage ?
61+ XCTAssertNoThrow ( maybeStartupMessage = try embedded. readOutbound ( as: PSQLFrontendMessage . self) )
62+ guard case . startup( let startupMessage) = maybeStartupMessage else {
63+ return XCTFail ( " Unexpected message " )
64+ }
65+
66+ XCTAssertEqual ( startupMessage. parameters. user, config. authentication? . username)
67+ XCTAssertEqual ( startupMessage. parameters. database, config. authentication? . database)
68+ XCTAssertEqual ( startupMessage. parameters. replication, . false )
5969 }
6070
6171 func testSSLUnsupportedClosesConnection( ) {
@@ -64,7 +74,7 @@ class PSQLChannelHandlerTests: XCTestCase {
6474
6575 let handler = PSQLChannelHandler ( authentification: config. authentication) { channel in
6676 XCTFail ( " This callback should never be exectuded " )
67- return channel . eventLoop . makeFailedFuture ( PSQLError . sslUnsupported)
77+ throw PSQLError . sslUnsupported
6878 }
6979 let embedded = EmbeddedChannel ( handler: handler)
7080 let eventHandler = TestEventHandler ( )
@@ -94,7 +104,7 @@ class PSQLChannelHandlerTests: XCTestCase {
94104 database: config. authentication? . database
95105 )
96106 let state = ConnectionStateMachine ( . waitingToStartAuthentication)
97- let handler = PSQLChannelHandler ( authentification: config. authentication, state: state)
107+ let handler = PSQLChannelHandler ( authentification: config. authentication, state: state, configureSSLCallback : nil )
98108 let embedded = EmbeddedChannel ( handler: handler)
99109
100110 embedded. triggerUserOutboundEvent ( PSQLOutgoingEvent . authenticate ( authContext) , promise: nil )
@@ -119,7 +129,7 @@ class PSQLChannelHandlerTests: XCTestCase {
119129 database: config. authentication? . database
120130 )
121131 let state = ConnectionStateMachine ( . waitingToStartAuthentication)
122- let handler = PSQLChannelHandler ( authentification: config. authentication, state: state)
132+ let handler = PSQLChannelHandler ( authentification: config. authentication, state: state, configureSSLCallback : nil )
123133 let embedded = EmbeddedChannel ( handler: handler)
124134
125135 embedded. triggerUserOutboundEvent ( PSQLOutgoingEvent . authenticate ( authContext) , promise: nil )
0 commit comments