-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
128 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
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 |
---|---|---|
@@ -1,63 +1,31 @@ | ||
package user_ip | ||
|
||
import ( | ||
"testing" | ||
"net/http" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/kataras/iris" | ||
"github.com/valyala/fasthttp" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestGetNoIp(t *testing.T) { | ||
req := &http.Request{Method: "GET"} | ||
ip := Get(req) | ||
req := &http.Request{Method: "GET"} | ||
ip := Get(req) | ||
|
||
assert.Equal(t, ip, "", "Should not find an ip address.") | ||
} | ||
|
||
func TestGetFromContextNoIp(t *testing.T) { | ||
var ctx iris.Context | ||
ctx.RequestCtx = &fasthttp.RequestCtx{} | ||
ip := GetFromContext(&ctx) | ||
|
||
assert.Equal(t, ip, "", "Should not find an ip address.") | ||
} | ||
|
||
func TestGetFromContextWithPrivateForwadedIp(t *testing.T) { | ||
var ctx iris.Context | ||
ctx.RequestCtx = &fasthttp.RequestCtx{} | ||
header := fasthttp.RequestHeader{} | ||
header.Add("X-Forwarded-For", "100.64.0.0, 192.168.0.0") | ||
ctx.RequestCtx.Request.Header = header | ||
ip := GetFromContext(&ctx) | ||
|
||
assert.Equal(t, ip, "", "Should not find an ip address.") | ||
assert.Equal(t, ip, "", "Should not find an ip address.") | ||
} | ||
|
||
func TestGetWithPrivateForwadedIp(t *testing.T) { | ||
req := &http.Request{Method: "GET", Header: make(http.Header)} | ||
ip := Get(req) | ||
|
||
req.Header.Set("X-Forwarded-For", "100.64.0.0, 192.168.0.0") | ||
|
||
assert.Equal(t, ip, "", "Should not find an ip address.") | ||
} | ||
req := &http.Request{Method: "GET", Header: make(http.Header)} | ||
ip := Get(req) | ||
|
||
func TestGetFromContextWithGlobalAndPrivateForwardedIps(t *testing.T) { | ||
var ctx iris.Context | ||
ctx.RequestCtx = &fasthttp.RequestCtx{} | ||
header := fasthttp.RequestHeader{} | ||
header.Add("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0") | ||
ctx.RequestCtx.Request.Header = header | ||
ip := GetFromContext(&ctx) | ||
req.Header.Set("X-Forwarded-For", "100.64.0.0, 192.168.0.0") | ||
|
||
assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.") | ||
assert.Equal(t, ip, "", "Should not find an ip address.") | ||
} | ||
|
||
func TestGetWithGlobalAndPrivateForwardedIps(t *testing.T) { | ||
req := &http.Request{Method: "GET", Header: make(http.Header)} | ||
req.Header.Set("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0") | ||
req := &http.Request{Method: "GET", Header: make(http.Header)} | ||
req.Header.Set("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0") | ||
|
||
ip := Get(req) | ||
assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.") | ||
ip := Get(req) | ||
assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.") | ||
} |