-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic unit tests to utilities (#227)
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package util | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestGetRequestRealIp(t *testing.T) { | ||
// Simple match | ||
req, err := http.NewRequest("GET", "/", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req.RemoteAddr = "10.0.0.1:12345" | ||
|
||
remoteIP := GetRequestRealIp(req) | ||
if remoteIP != "10.0.0.1" { | ||
t.Fatal("simple match failed") | ||
} | ||
|
||
// With X-Forwarded-For (success path) | ||
req.RemoteAddr = "127.0.0.1:1234" | ||
req.Header.Set("X-Forwarded-For", "203.0.113.195") | ||
remoteIP = GetRequestRealIp(req) | ||
if remoteIP != "203.0.113.195" { | ||
t.Fatalf("simple match failed got %s", remoteIP) | ||
} | ||
// now ipv6 | ||
ipv6addr := "2001:db8:85a3:8d3:1319:8a2e:370:7348" | ||
req.RemoteAddr = ipv6addr | ||
req.Header.Set("X-Forwarded-For", ipv6addr) | ||
remoteIP = GetRequestRealIp(req) | ||
if remoteIP != ipv6addr { | ||
t.Fatalf("simple match failed got %s", remoteIP) | ||
} | ||
// todo... with chain | ||
// 203.0.113.195,2001:db8:85a3:8d3:1319:8a2e:370:7348,198.51.100.178 | ||
} | ||
|
||
func TestCreateSimpleDataBodyRequestSuccess(t *testing.T) { | ||
req, err := CreateSimpleDataBodyRequest("GET", "/", []byte("some-content"), "mimte-content-type") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if req == nil { | ||
t.Fatal("req must NOT be null") | ||
} | ||
} | ||
|
||
func TestCreateFormDataBodyRequestSimple(t *testing.T) { | ||
req, err := CreateFormDataBodyRequest("GET", "/", "filedata", "file-field-name", "filename") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if req == nil { | ||
t.Fatal("req must NOT be null") | ||
} | ||
} |