Skip to content

Commit

Permalink
add test cases for http request parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronhurt authored and magiconair committed Feb 18, 2018
1 parent d8e2b1a commit 8169a40
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions route/access_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package route

import (
"net"
"net/http"
"testing"
)

Expand Down Expand Up @@ -105,3 +106,70 @@ func TestAccessRules_denyByIP(t *testing.T) {
})
}
}

func TestAccessRules_AccessDeniedHTTP(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com/", nil)
tests := []struct {
desc string
target *Target
xff string
remote string
denied bool
}{
{
desc: "AccessDeniedHTTPwithDeniedXFFandAllowedRemote",
target: &Target{
Opts: map[string]string{"allow": "ip:10.0.0.0/8,ip:192.168.0.0/24"},
},
xff: "1.1.1.2, 10.11.12.13, 10.11.12.14",
remote: "10.11.12.1:65500",
denied: true,
},
{
desc: "AccessDeniedHTTPwithAllowedXFFandDeniedRemote",
target: &Target{
Opts: map[string]string{"allow": "ip:10.0.0.0/8,ip:192.168.0.0/24"},
},
xff: "10.11.12.13, 1.2.3.4",
remote: "1.1.1.2:65500",
denied: true,
},
{
desc: "AccessDeniedHTTPwitAllowedXFFandAllowedRemote",
target: &Target{
Opts: map[string]string{"allow": "ip:10.0.0.0/8,ip:192.168.0.0/24"},
},
xff: "10.11.12.13, 1.2.3.4",
remote: "192.168.0.12:65500",
denied: false,
},
{
desc: "AccessDeniedHTTPwithDeniedXFFandDeniedRemote",
target: &Target{
Opts: map[string]string{"allow": "ip:10.0.0.0/8,ip:192.168.0.0/24"},
},
xff: "1.2.3.4, 10.11.12.13, 10.11.12.14",
remote: "200.17.18.20:65500",
denied: true,
},
}

for i, tt := range tests {
tt := tt // capture loop var

req.Header = http.Header{"X-Forwarded-For": []string{tt.xff}}
req.RemoteAddr = tt.remote

t.Run(tt.desc, func(t *testing.T) {
if err := tt.target.processAccessRules(); err != nil {
t.Errorf("%d: %s - failed to process access rules: %s",
i, tt.desc, err.Error())
}
if deny := tt.target.AccessDeniedHTTP(req); deny != tt.denied {
t.Errorf("%d: %s\ngot denied: %t\nwant denied: %t\n",
i, tt.desc, deny, tt.denied)
return
}
})
}
}

0 comments on commit 8169a40

Please sign in to comment.