1
+ import sinon from "sinon" ;
2
+ import { config } from "../../src/config" ;
3
+ import assert from "assert" ;
4
+ const mode = "production" ;
5
+ let stub : sinon . SinonStub ;
6
+ let stub2 : sinon . SinonStub ;
7
+ import { createRequest } from "../mocks/mockExpressRequest" ;
8
+ import { getIP } from "../../src/utils/getIP" ;
9
+
10
+ const v4RequestOptions = {
11
+ headers : {
12
+ "x-forwarded-for" : "127.0.1.1" ,
13
+ "cf-connecting-ip" : "127.0.1.2" ,
14
+ "x-real-ip" : "127.0.1.3" ,
15
+ } ,
16
+ ip : "127.0.1.5" ,
17
+ socket : {
18
+ remoteAddress : "127.0.1.4"
19
+ }
20
+ } ;
21
+ const v6RequestOptions = {
22
+ headers : {
23
+ "x-forwarded-for" : "[100::1]" ,
24
+ "cf-connecting-ip" : "[100::2]" ,
25
+ "x-real-ip" : "[100::3]" ,
26
+ } ,
27
+ ip : "[100::5]" ,
28
+ socket : {
29
+ remoteAddress : "[100::4]"
30
+ }
31
+ } ;
32
+ const v4MockRequest = createRequest ( v4RequestOptions ) ;
33
+ const v6MockRequest = createRequest ( v6RequestOptions ) ;
34
+
35
+ const expectedIP4 = {
36
+ "X-Forwarded-For" : "127.0.1.1" ,
37
+ "Cloudflare" : "127.0.1.2" ,
38
+ "X-Real-IP" : "127.0.1.3" ,
39
+ "default" : "127.0.1.4" ,
40
+ } ;
41
+
42
+ const expectedIP6 = {
43
+ "X-Forwarded-For" : "[100::1]" ,
44
+ "Cloudflare" : "[100::2]" ,
45
+ "X-Real-IP" : "[100::3]" ,
46
+ "default" : "[100::4]" ,
47
+ } ;
48
+
49
+ describe ( "getIP stubs" , ( ) => {
50
+ before ( ( ) => stub = sinon . stub ( config , "mode" ) . value ( mode ) ) ;
51
+ after ( ( ) => stub . restore ( ) ) ;
52
+
53
+ it ( "Should return production mode if stub worked" , ( done ) => {
54
+ assert . strictEqual ( config . mode , mode ) ;
55
+ done ( ) ;
56
+ } ) ;
57
+ } ) ;
58
+
59
+ describe ( "getIP array tests" , ( ) => {
60
+ beforeEach ( ( ) => stub = sinon . stub ( config , "mode" ) . value ( mode ) ) ;
61
+ afterEach ( ( ) => {
62
+ stub . restore ( ) ;
63
+ stub2 . restore ( ) ;
64
+ } ) ;
65
+
66
+ for ( const [ key , value ] of Object . entries ( expectedIP4 ) ) {
67
+ it ( `Should return correct IPv4 from ${ key } ` , ( done ) => {
68
+ stub2 = sinon . stub ( config , "behindProxy" ) . value ( key ) ;
69
+ const ip = getIP ( v4MockRequest ) ;
70
+ assert . strictEqual ( config . behindProxy , key ) ;
71
+ assert . strictEqual ( ip , value ) ;
72
+ done ( ) ;
73
+ } ) ;
74
+ }
75
+
76
+ for ( const [ key , value ] of Object . entries ( expectedIP6 ) ) {
77
+ it ( `Should return correct IPv6 from ${ key } ` , ( done ) => {
78
+ stub2 = sinon . stub ( config , "behindProxy" ) . value ( key ) ;
79
+ const ip = getIP ( v6MockRequest ) ;
80
+ assert . strictEqual ( config . behindProxy , key ) ;
81
+ assert . strictEqual ( ip , value ) ;
82
+ done ( ) ;
83
+ } ) ;
84
+ }
85
+ } ) ;
86
+
87
+ describe ( "getIP true tests" , ( ) => {
88
+ before ( ( ) => stub = sinon . stub ( config , "mode" ) . value ( mode ) ) ;
89
+ after ( ( ) => {
90
+ stub . restore ( ) ;
91
+ stub2 . restore ( ) ;
92
+ } ) ;
93
+
94
+ it ( `Should return correct IPv4 from with bool true` , ( done ) => {
95
+ stub2 = sinon . stub ( config , "behindProxy" ) . value ( true ) ;
96
+ const ip = getIP ( v4MockRequest ) ;
97
+ assert . strictEqual ( config . behindProxy , "X-Forwarded-For" ) ;
98
+ assert . strictEqual ( ip , expectedIP4 [ "X-Forwarded-For" ] ) ;
99
+ done ( ) ;
100
+ } ) ;
101
+
102
+ it ( `Should return correct IPv4 from with string true` , ( done ) => {
103
+ stub2 = sinon . stub ( config , "behindProxy" ) . value ( "true" ) ;
104
+ const ip = getIP ( v4MockRequest ) ;
105
+ assert . strictEqual ( config . behindProxy , "X-Forwarded-For" ) ;
106
+ assert . strictEqual ( ip , expectedIP4 [ "X-Forwarded-For" ] ) ;
107
+ done ( ) ;
108
+ } ) ;
109
+ } ) ;
0 commit comments