@@ -56,6 +56,47 @@ describe("cluster", () => {
5656 } ) . to . throw ( / I n v a l i d r o l e / ) ;
5757 } ) ;
5858 } ) ;
59+
60+
61+ describe ( "natMapper" , ( ) => {
62+ it ( "returns the original nodeKey if no NAT mapping is provided" , ( ) => {
63+ const cluster = new Cluster ( [ ] ) ;
64+ const nodeKey = { host : "127.0.0.1" , port : 6379 } ;
65+ const result = cluster [ "natMapper" ] ( nodeKey ) ;
66+
67+ expect ( result ) . to . eql ( nodeKey ) ;
68+ } ) ;
69+
70+ it ( "maps external IP to internal IP using NAT mapping object" , ( ) => {
71+ const natMap = { "203.0.113.1:6379" : { host : "127.0.0.1" , port : 30000 } } ;
72+ const cluster = new Cluster ( [ ] , { natMap } ) ;
73+ const nodeKey = "203.0.113.1:6379" ;
74+ const result = cluster [ "natMapper" ] ( nodeKey ) ;
75+ expect ( result ) . to . eql ( { host : "127.0.0.1" , port : 30000 } ) ;
76+ } ) ;
77+
78+ it ( "maps external IP to internal IP using NAT mapping function" , ( ) => {
79+ const natMap = ( key ) => {
80+ if ( key === "203.0.113.1:6379" ) {
81+ return { host : "127.0.0.1" , port : 30000 } ;
82+ }
83+ return null ;
84+ } ;
85+ const cluster = new Cluster ( [ ] , { natMap } ) ;
86+ const nodeKey = "203.0.113.1:6379" ;
87+ const result = cluster [ "natMapper" ] ( nodeKey ) ;
88+ expect ( result ) . to . eql ( { host : "127.0.0.1" , port : 30000 } ) ;
89+ } ) ;
90+
91+ it ( "returns the original nodeKey if NAT mapping is invalid" , ( ) => {
92+ const natMap = { "invalid:key" : { host : "127.0.0.1" , port : 30000 } } ;
93+ const cluster = new Cluster ( [ ] , { natMap } ) ;
94+ const nodeKey = "203.0.113.1:6379" ;
95+ const result = cluster [ "natMapper" ] ( nodeKey ) ;
96+ expect ( result ) . to . eql ( { host : "203.0.113.1" , port : 6379 } ) ;
97+ } ) ;
98+ } ) ;
99+
59100} ) ;
60101
61102describe ( "nodeKeyToRedisOptions()" , ( ) => {
0 commit comments