Skip to content

Commit

Permalink
lint: constants for http status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
asoorm authored and buger committed Mar 20, 2018
1 parent 4e667e2 commit e7aa7c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions mw_ip_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func (i *IPWhiteListMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Re
// Check CIDR if possible
if allowedNet != nil && allowedNet.Contains(remoteIP) {
// matched, pass through
return nil, 200
return nil, http.StatusOK
}

// We parse the IP to manage IPv4 and IPv6 easily
if allowedIP.Equal(remoteIP) {
// matched, pass through
return nil, 200
return nil, http.StatusOK
}
}

Expand All @@ -52,5 +52,5 @@ func (i *IPWhiteListMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Re
reportHealthValue(i.Spec, KeyFailure, "-1")

// Not matched, fail
return errors.New("Access from this IP has been disallowed"), 403
return errors.New("access from this IP has been disallowed"), http.StatusForbidden
}
11 changes: 6 additions & 5 deletions mw_ip_whitelist_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"
)
Expand All @@ -15,11 +16,11 @@ func TestIPMiddlewarePass(t *testing.T) {
remote, forwarded string
wantCode int
}{
{"127.0.0.1:80", "", 200}, // remote exact match
{"127.0.0.2:80", "", 200}, // remote CIDR match
{"10.0.0.1:80", "", 403}, // no match
{"10.0.0.1:80", "127.0.0.1", 200}, // forwarded exact match
{"10.0.0.1:80", "127.0.0.2", 200}, // forwarded CIDR match
{"127.0.0.1:80", "", http.StatusOK}, // remote exact match
{"127.0.0.2:80", "", http.StatusOK}, // remote CIDR match
{"10.0.0.1:80", "", http.StatusForbidden}, // no match
{"10.0.0.1:80", "127.0.0.1", http.StatusOK}, // forwarded exact match
{"10.0.0.1:80", "127.0.0.2", http.StatusOK}, // forwarded CIDR match
} {
rec := httptest.NewRecorder()
req := testReq(t, "GET", "/", nil)
Expand Down

0 comments on commit e7aa7c2

Please sign in to comment.