@@ -31,6 +31,7 @@ import { Pool } from '../../src/pool'
31
31
import SimpleHostNameResolver from '../../src/channel/browser/browser-host-name-resolver'
32
32
import RoutingConnectionProvider from '../../src/connection-provider/connection-provider-routing'
33
33
import { DelegateConnection , Connection } from '../../src/connection'
34
+ import AuthenticationProvider from '../../src/connection-provider/authentication-provider'
34
35
35
36
const {
36
37
serverAddress : { ServerAddress } ,
@@ -3047,6 +3048,101 @@ describe.each([
3047
3048
} )
3048
3049
} )
3049
3050
3051
+ describe ( 'constructor' , ( ) => {
3052
+ describe ( 'newPool' , ( ) => {
3053
+ describe ( 'param.create' , ( ) => {
3054
+ it ( 'should create connection' , async ( ) => {
3055
+ const { create, createChannelConnectionHook, provider } = setup ( )
3056
+
3057
+ const connection = await create ( { } , server0 , undefined )
3058
+
3059
+ expect ( createChannelConnectionHook ) . toHaveBeenCalledWith ( server0 )
3060
+ expect ( provider . _openConnections [ connection . id ] ) . toBe ( connection )
3061
+ await expect ( createChannelConnectionHook . mock . results [ 0 ] . value ) . resolves . toBe ( connection )
3062
+ } )
3063
+
3064
+ it ( 'should register the release function into the connection' , async ( ) => {
3065
+ const { create } = setup ( )
3066
+ const releaseResult = { property : 'some property' }
3067
+ const release = jest . fn ( ( ) => releaseResult )
3068
+
3069
+ const connection = await create ( { } , server0 , release )
3070
+
3071
+ const released = connection . _release ( )
3072
+
3073
+ expect ( released ) . toBe ( releaseResult )
3074
+ expect ( release ) . toHaveBeenCalledWith ( server0 , connection )
3075
+ } )
3076
+
3077
+ it . each ( [
3078
+ null ,
3079
+ undefined ,
3080
+ { scheme : 'bearer' , credentials : 'token01' }
3081
+ ] ) ( 'should authenticate connection (auth = %o)' , async ( auth ) => {
3082
+ const { create, authenticationProviderHook } = setup ( )
3083
+
3084
+ const connection = await create ( { auth } , server0 )
3085
+
3086
+ expect ( authenticationProviderHook . authenticate ) . toHaveBeenCalledWith ( {
3087
+ connection,
3088
+ auth
3089
+ } )
3090
+ } )
3091
+
3092
+ it ( 'should handle create connection failures' , async ( ) => {
3093
+ const error = newError ( 'some error' )
3094
+ const createConnection = jest . fn ( ( ) => Promise . reject ( error ) )
3095
+ const { create, authenticationProviderHook, provider } = setup ( { createConnection } )
3096
+ const openConnections = { ...provider . _openConnections }
3097
+
3098
+ await expect ( create ( { } , server0 ) ) . rejects . toThrow ( error )
3099
+
3100
+ expect ( authenticationProviderHook . authenticate ) . not . toHaveBeenCalled ( )
3101
+ expect ( provider . _openConnections ) . toEqual ( openConnections )
3102
+ } )
3103
+
3104
+ it . each ( [
3105
+ null ,
3106
+ undefined ,
3107
+ { scheme : 'bearer' , credentials : 'token01' }
3108
+ ] ) ( 'should handle authentication failures (auth = %o)' , async ( auth ) => {
3109
+ const error = newError ( 'some error' )
3110
+ const authenticationProvider = jest . fn ( ( ) => Promise . reject ( error ) )
3111
+ const { create, authenticationProviderHook, createChannelConnectionHook, provider } = setup ( { authenticationProvider } )
3112
+ const openConnections = { ...provider . _openConnections }
3113
+
3114
+ await expect ( create ( { auth } , server0 ) ) . rejects . toThrow ( error )
3115
+
3116
+ const connection = await createChannelConnectionHook . mock . results [ 0 ] . value
3117
+ expect ( authenticationProviderHook . authenticate ) . toHaveBeenCalledWith ( { auth, connection } )
3118
+ expect ( provider . _openConnections ) . toEqual ( openConnections )
3119
+ expect ( connection . _closed ) . toBe ( true )
3120
+ } )
3121
+ } )
3122
+
3123
+ function setup ( { createConnection, authenticationProvider } = { } ) {
3124
+ const newPool = jest . fn ( ( ...args ) => new Pool ( ...args ) )
3125
+ const createChannelConnectionHook = createConnection || jest . fn ( async ( address ) => new FakeConnection ( address ) )
3126
+ const authenticationProviderHook = new AuthenticationProvider ( { } )
3127
+ jest . spyOn ( authenticationProviderHook , 'authenticate' )
3128
+ . mockImplementation ( authenticationProvider || jest . fn ( ( { connection } ) => Promise . resolve ( connection ) ) )
3129
+ const provider = new RoutingConnectionProvider ( {
3130
+ newPool,
3131
+ config : { } ,
3132
+ address : server01
3133
+ } )
3134
+ provider . _createChannelConnection = createChannelConnectionHook
3135
+ provider . _authenticationProvider = authenticationProviderHook
3136
+ return {
3137
+ provider,
3138
+ ...newPool . mock . calls [ 0 ] [ 0 ] ,
3139
+ createChannelConnectionHook,
3140
+ authenticationProviderHook
3141
+ }
3142
+ }
3143
+ } )
3144
+ } )
3145
+
3050
3146
function newPool ( { create, config } = { } ) {
3051
3147
const _create = ( address , release ) => {
3052
3148
if ( create ) {
@@ -3220,6 +3316,12 @@ class FakeConnection extends Connection {
3220
3316
this . resetAndFlush = jest . fn ( ( ) => Promise . resolve ( ) )
3221
3317
this . _server = server
3222
3318
this . _authToken = authToken
3319
+ this . _id = 1
3320
+ this . _closed = false
3321
+ }
3322
+
3323
+ get id ( ) {
3324
+ return this . _id
3223
3325
}
3224
3326
3225
3327
get authToken ( ) {
@@ -3243,7 +3345,7 @@ class FakeConnection extends Connection {
3243
3345
}
3244
3346
3245
3347
async close ( ) {
3246
-
3348
+ this . _closed = true
3247
3349
}
3248
3350
3249
3351
protocol ( ) {
0 commit comments