1
1
import { strict as assert } from 'node:assert' ;
2
2
import testUtils , { GLOBAL , waitTillBeenCalled } from '../test-utils' ;
3
- import RedisClient , { RedisClientType } from '.' ;
3
+ import RedisClient , { RedisClientOptions , RedisClientType } from '.' ;
4
4
import { AbortError , ClientClosedError , ClientOfflineError , ConnectionTimeoutError , DisconnectsClientError , ErrorReply , MultiErrorReply , SocketClosedUnexpectedlyError , WatchError } from '../errors' ;
5
5
import { defineScript } from '../lua-script' ;
6
6
import { spy } from 'sinon' ;
@@ -25,36 +25,87 @@ export const SQUARE_SCRIPT = defineScript({
25
25
26
26
describe ( 'Client' , ( ) => {
27
27
describe ( 'parseURL' , ( ) => {
28
- it ( 'redis://user:secret@localhost:6379/0' , ( ) => {
29
- assert . deepEqual (
30
- RedisClient . parseURL ( 'redis://user:secret@localhost:6379/0' ) ,
31
- {
32
- socket : {
33
- host : 'localhost' ,
34
- port : 6379
35
- } ,
36
- username : 'user' ,
37
- password : 'secret' ,
38
- database : 0
28
+ it ( 'redis://user:secret@localhost:6379/0' , async ( ) => {
29
+ const result = RedisClient . parseURL ( 'redis://user:secret@localhost:6379/0' ) ;
30
+ const expected : RedisClientOptions = {
31
+ socket : {
32
+ host : 'localhost' ,
33
+ port : 6379
34
+ } ,
35
+ username : 'user' ,
36
+ password : 'secret' ,
37
+ database : 0 ,
38
+ credentialsProvider : {
39
+ type : 'async-credentials-provider' ,
40
+ credentials : async ( ) => ( {
41
+ password : 'secret' ,
42
+ username : 'user'
43
+ } )
39
44
}
40
- ) ;
45
+ } ;
46
+
47
+ // Compare everything except the credentials function
48
+ const { credentialsProvider : resultCredProvider , ...resultRest } = result ;
49
+ const { credentialsProvider : expectedCredProvider , ...expectedRest } = expected ;
50
+
51
+ // Compare non-function properties
52
+ assert . deepEqual ( resultRest , expectedRest ) ;
53
+
54
+ if ( result . credentialsProvider . type === 'async-credentials-provider'
55
+ && expected . credentialsProvider . type === 'async-credentials-provider' ) {
56
+
57
+ // Compare the actual output of the credentials functions
58
+ const resultCreds = await result . credentialsProvider . credentials ( ) ;
59
+ const expectedCreds = await expected . credentialsProvider . credentials ( ) ;
60
+ assert . deepEqual ( resultCreds , expectedCreds ) ;
61
+ } else {
62
+ assert . fail ( 'Credentials provider type mismatch' ) ;
63
+ }
64
+
65
+
41
66
} ) ;
42
67
43
- it ( 'rediss://user:secret@localhost:6379/0' , ( ) => {
44
- assert . deepEqual (
45
- RedisClient . parseURL ( 'rediss://user:secret@localhost:6379/0' ) ,
46
- {
47
- socket : {
48
- host : 'localhost' ,
49
- port : 6379 ,
50
- tls : true
51
- } ,
52
- username : 'user' ,
53
- password : 'secret' ,
54
- database : 0
68
+ it ( 'rediss://user:secret@localhost:6379/0' , async ( ) => {
69
+ const result = RedisClient . parseURL ( 'rediss://user:secret@localhost:6379/0' ) ;
70
+ const expected : RedisClientOptions = {
71
+ socket : {
72
+ host : 'localhost' ,
73
+ port : 6379 ,
74
+ tls : true
75
+ } ,
76
+ username : 'user' ,
77
+ password : 'secret' ,
78
+ database : 0 ,
79
+ credentialsProvider : {
80
+ credentials : async ( ) => ( {
81
+ password : 'secret' ,
82
+ username : 'user'
83
+ } ) ,
84
+ type : 'async-credentials-provider'
55
85
}
56
- ) ;
57
- } ) ;
86
+ } ;
87
+
88
+ // Compare everything except the credentials function
89
+ const { credentialsProvider : resultCredProvider , ...resultRest } = result ;
90
+ const { credentialsProvider : expectedCredProvider , ...expectedRest } = expected ;
91
+
92
+ // Compare non-function properties
93
+ assert . deepEqual ( resultRest , expectedRest ) ;
94
+ assert . equal ( resultCredProvider . type , expectedCredProvider . type ) ;
95
+
96
+ if ( result . credentialsProvider . type === 'async-credentials-provider' &&
97
+ expected . credentialsProvider . type === 'async-credentials-provider' ) {
98
+
99
+ // Compare the actual output of the credentials functions
100
+ const resultCreds = await result . credentialsProvider . credentials ( ) ;
101
+ const expectedCreds = await expected . credentialsProvider . credentials ( ) ;
102
+ assert . deepEqual ( resultCreds , expectedCreds ) ;
103
+
104
+ } else {
105
+ assert . fail ( 'Credentials provider type mismatch' ) ;
106
+ }
107
+
108
+ } )
58
109
59
110
it ( 'Invalid protocol' , ( ) => {
60
111
assert . throws (
@@ -90,6 +141,21 @@ describe('Client', () => {
90
141
) ;
91
142
} , GLOBAL . SERVERS . PASSWORD ) ;
92
143
144
+ testUtils . testWithClient ( 'Client can authenticate asynchronously ' , async client => {
145
+ assert . equal (
146
+ await client . ping ( ) ,
147
+ 'PONG'
148
+ ) ;
149
+ } , GLOBAL . SERVERS . ASYNC_BASIC_AUTH ) ;
150
+
151
+ testUtils . testWithClient ( 'Client can authenticate using the streaming credentials provider for initial token acquisition' ,
152
+ async client => {
153
+ assert . equal (
154
+ await client . ping ( ) ,
155
+ 'PONG'
156
+ ) ;
157
+ } , GLOBAL . SERVERS . STREAMING_AUTH ) ;
158
+
93
159
testUtils . testWithClient ( 'should execute AUTH before SELECT' , async client => {
94
160
assert . equal (
95
161
( await client . clientInfo ( ) ) . db ,
@@ -294,6 +360,7 @@ describe('Client', () => {
294
360
assert . equal ( err . replies . length , 2 ) ;
295
361
assert . deepEqual ( err . errorIndexes , [ 1 ] ) ;
296
362
assert . ok ( err . replies [ 1 ] instanceof ErrorReply ) ;
363
+ // @ts -ignore TS2802
297
364
assert . deepEqual ( [ ...err . errors ( ) ] , [ err . replies [ 1 ] ] ) ;
298
365
return true ;
299
366
}
0 commit comments