@@ -17,6 +17,7 @@ package apiutil
1717import (
1818 "bytes"
1919 "io"
20+ "net/http"
2021 "net/http/httptest"
2122 "testing"
2223
@@ -68,3 +69,141 @@ func TestJsonRespondErrorBadInput(t *testing.T) {
6869 re .Equal (400 , result .StatusCode )
6970 }
7071}
72+
73+ func TestGetIPPortFromHTTPRequest (t * testing.T ) {
74+ t .Parallel ()
75+ re := require .New (t )
76+
77+ testCases := []struct {
78+ r * http.Request
79+ ip string
80+ port string
81+ err error
82+ }{
83+ // IPv4 "X-Forwarded-For" with port
84+ {
85+ r : & http.Request {
86+ Header : map [string ][]string {
87+ XForwardedForHeader : {"127.0.0.1:5299" },
88+ },
89+ },
90+ ip : "127.0.0.1" ,
91+ port : "5299" ,
92+ },
93+ // IPv4 "X-Forwarded-For" without port
94+ {
95+ r : & http.Request {
96+ Header : map [string ][]string {
97+ XForwardedForHeader : {"127.0.0.1" },
98+ XForwardedPortHeader : {"5299" },
99+ },
100+ },
101+ ip : "127.0.0.1" ,
102+ port : "5299" ,
103+ },
104+ // IPv4 "X-Real-IP" with port
105+ {
106+ r : & http.Request {
107+ Header : map [string ][]string {
108+ XRealIPHeader : {"127.0.0.1:5299" },
109+ },
110+ },
111+ ip : "127.0.0.1" ,
112+ port : "5299" ,
113+ },
114+ // IPv4 "X-Real-IP" without port
115+ {
116+ r : & http.Request {
117+ Header : map [string ][]string {
118+ XForwardedForHeader : {"127.0.0.1" },
119+ XForwardedPortHeader : {"5299" },
120+ },
121+ },
122+ ip : "127.0.0.1" ,
123+ port : "5299" ,
124+ },
125+ // IPv4 RemoteAddr with port
126+ {
127+ r : & http.Request {
128+ RemoteAddr : "127.0.0.1:5299" ,
129+ },
130+ ip : "127.0.0.1" ,
131+ port : "5299" ,
132+ },
133+ // IPv4 RemoteAddr without port
134+ {
135+ r : & http.Request {
136+ RemoteAddr : "127.0.0.1" ,
137+ },
138+ ip : "127.0.0.1" ,
139+ port : "" ,
140+ },
141+ // IPv6 "X-Forwarded-For" with port
142+ {
143+ r : & http.Request {
144+ Header : map [string ][]string {
145+ XForwardedForHeader : {"[::1]:5299" },
146+ },
147+ },
148+ ip : "::1" ,
149+ port : "5299" ,
150+ },
151+ // IPv6 "X-Forwarded-For" without port
152+ {
153+ r : & http.Request {
154+ Header : map [string ][]string {
155+ XForwardedForHeader : {"::1" },
156+ },
157+ },
158+ ip : "::1" ,
159+ port : "" ,
160+ },
161+ // IPv6 "X-Real-IP" with port
162+ {
163+ r : & http.Request {
164+ Header : map [string ][]string {
165+ XRealIPHeader : {"[::1]:5299" },
166+ },
167+ },
168+ ip : "::1" ,
169+ port : "5299" ,
170+ },
171+ // IPv6 "X-Real-IP" without port
172+ {
173+ r : & http.Request {
174+ Header : map [string ][]string {
175+ XForwardedForHeader : {"::1" },
176+ },
177+ },
178+ ip : "::1" ,
179+ port : "" ,
180+ },
181+ // IPv6 RemoteAddr with port
182+ {
183+ r : & http.Request {
184+ RemoteAddr : "[::1]:5299" ,
185+ },
186+ ip : "::1" ,
187+ port : "5299" ,
188+ },
189+ // IPv6 RemoteAddr without port
190+ {
191+ r : & http.Request {
192+ RemoteAddr : "::1" ,
193+ },
194+ ip : "::1" ,
195+ port : "" ,
196+ },
197+ // Abnormal case
198+ {
199+ r : & http.Request {},
200+ ip : "" ,
201+ port : "" ,
202+ },
203+ }
204+ for idx , testCase := range testCases {
205+ ip , port := GetIPPortFromHTTPRequest (testCase .r )
206+ re .Equal (testCase .ip , ip , "case %d" , idx )
207+ re .Equal (testCase .port , port , "case %d" , idx )
208+ }
209+ }
0 commit comments