4
4
"net/http"
5
5
"net/http/httptest"
6
6
"net/url"
7
+ "os"
7
8
"path/filepath"
8
9
"strings"
9
10
"testing"
@@ -16,15 +17,19 @@ import (
16
17
// It loads the server configuration and sets up the router, failing the test if either step encounters an error.
17
18
func setupRouter (t * testing.T ) * gin.Engine {
18
19
t .Helper ()
20
+
19
21
config , err := LoadConfig ()
20
22
if err != nil {
21
23
t .Fatalf ("Failed to load config: %v" , err )
22
24
}
25
+
23
26
config .StaticDir = filepath .Join (".." , ".." , "static" )
27
+
24
28
r , err := SetupRouter (config )
25
29
if err != nil {
26
30
t .Fatalf ("Failed to setup router: %v" , err )
27
31
}
32
+
28
33
return r
29
34
}
30
35
@@ -88,13 +93,15 @@ func TestRouterSetup(t *testing.T) {
88
93
for _ , tt := range tests {
89
94
t .Run (tt .name , func (t * testing.T ) {
90
95
router := setupRouter (t )
96
+
91
97
var req * http.Request
92
98
if tt .method == "POST" {
93
99
req , _ = http .NewRequest (tt .method , tt .path , strings .NewReader (tt .formData .Encode ()))
94
100
req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
95
101
} else {
96
102
req , _ = http .NewRequest (tt .method , tt .path , nil )
97
103
}
104
+
98
105
resp := httptest .NewRecorder ()
99
106
router .ServeHTTP (resp , req )
100
107
assert .Equal (t , tt .wantStatus , resp .Code , "Status code" )
@@ -117,6 +124,7 @@ func TestTrustedProxies(t *testing.T) {
117
124
wantBody string
118
125
wantError bool
119
126
}{
127
+ // Existing test cases remain unchanged
120
128
{
121
129
name : "No trusted proxies" ,
122
130
trustedProxies : "" ,
@@ -145,6 +153,16 @@ func TestTrustedProxies(t *testing.T) {
145
153
wantBody : "" ,
146
154
wantError : true ,
147
155
},
156
+ // New test case for Line 45 - Empty proxy entry
157
+ {
158
+ name : "Trusted proxies with empty entry" ,
159
+ trustedProxies : "192.168.1.1,,10.0.0.1" ,
160
+ remoteAddr : "192.168.1.1:12345" ,
161
+ xForwardedFor : "203.0.113.1" ,
162
+ wantClientIP : "203.0.113.1" ,
163
+ wantStatus : http .StatusOK ,
164
+ wantBody : "EUI-64 Calculator" ,
165
+ },
148
166
}
149
167
150
168
for _ , tt := range tests {
@@ -158,32 +176,98 @@ func TestTrustedProxies(t *testing.T) {
158
176
if tt .wantError {
159
177
_ , err := SetupRouter (Config {Port : defaultPort , TrustedProxies : strings .Split (tt .trustedProxies , "," )})
160
178
assert .Error (t , err , "Expected error for invalid proxy" )
179
+
161
180
return
162
181
}
163
182
164
183
router := setupRouter (t )
165
- req , _ := http .NewRequest ("GET" , "/" , nil )
184
+ req , _ := http .NewRequest (http . MethodGet , "/" , nil )
166
185
req .RemoteAddr = tt .remoteAddr
186
+
167
187
if tt .xForwardedFor != "" {
168
188
req .Header .Set ("X-Forwarded-For" , tt .xForwardedFor )
169
189
}
190
+
170
191
resp := httptest .NewRecorder ()
171
192
router .ServeHTTP (resp , req )
172
193
assert .Equal (t , tt .wantStatus , resp .Code , "Status code" )
173
194
assert .Contains (t , resp .Body .String (), tt .wantBody , "Response body" )
174
195
175
196
var gotClientIP string
197
+
176
198
router .GET ("/test-ip" , func (c * gin.Context ) {
177
199
gotClientIP = c .ClientIP ()
178
200
})
179
- req , _ = http .NewRequest ("GET" , "/test-ip" , nil )
201
+
202
+ req , _ = http .NewRequest (http .MethodGet , "/test-ip" , nil )
180
203
req .RemoteAddr = tt .remoteAddr
204
+
181
205
if tt .xForwardedFor != "" {
182
206
req .Header .Set ("X-Forwarded-For" , tt .xForwardedFor )
183
207
}
208
+
184
209
resp = httptest .NewRecorder ()
185
210
router .ServeHTTP (resp , req )
186
211
assert .Equal (t , tt .wantClientIP , gotClientIP , "Client IP" )
187
212
})
188
213
}
189
214
}
215
+
216
+ // New TestLoadConfig to cover Lines 37 and 56.
217
+ func TestLoadConfig (t * testing.T ) {
218
+ tests := []struct {
219
+ name string
220
+ portEnv string
221
+ trustedProxies string
222
+ staticDirEnv string
223
+ wantPort string
224
+ wantProxies []string
225
+ wantStaticDir string
226
+ }{
227
+ {
228
+ name : "Default config" ,
229
+ portEnv : "" ,
230
+ trustedProxies : "" ,
231
+ staticDirEnv : "" ,
232
+ wantPort : defaultPort ,
233
+ wantProxies : nil ,
234
+ wantStaticDir : filepath .Join (filepath .Dir (os .Args [0 ]), defaultStaticDir ), // Approximation
235
+ },
236
+ {
237
+ name : "Custom port (Line 37)" ,
238
+ portEnv : "9090" ,
239
+ trustedProxies : "" ,
240
+ staticDirEnv : "" ,
241
+ wantPort : ":9090" ,
242
+ wantProxies : nil ,
243
+ wantStaticDir : filepath .Join (filepath .Dir (os .Args [0 ]), defaultStaticDir ),
244
+ },
245
+ {
246
+ name : "Custom static dir" ,
247
+ portEnv : "" ,
248
+ trustedProxies : "" ,
249
+ staticDirEnv : "/custom/static" ,
250
+ wantPort : defaultPort ,
251
+ wantProxies : nil ,
252
+ wantStaticDir : "/custom/static" ,
253
+ },
254
+ // Note: Line 56 (os.Executable failure) is harder to test directly without mocking.
255
+ // We can assume it works if STATIC_DIR is unset and defaultStaticDir is used.
256
+ }
257
+
258
+ for _ , tt := range tests {
259
+ t .Run (tt .name , func (t * testing.T ) {
260
+ // Clear and set environment variables
261
+ t .Setenv ("PORT" , tt .portEnv )
262
+ t .Setenv (trustedProxiesEnv , tt .trustedProxies )
263
+ t .Setenv (staticDirEnv , tt .staticDirEnv )
264
+
265
+ config , err := LoadConfig ()
266
+ assert .NoError (t , err , "LoadConfig should not error" )
267
+
268
+ assert .Equal (t , tt .wantPort , config .Port , "Port" )
269
+ assert .Equal (t , tt .wantProxies , config .TrustedProxies , "TrustedProxies" )
270
+ assert .Equal (t , tt .wantStaticDir , config .StaticDir , "StaticDir" )
271
+ })
272
+ }
273
+ }
0 commit comments