Skip to content

Commit

Permalink
Rename RemoteIp -> RemoteIP (golint was not amused)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahlo committed Sep 2, 2015
1 parent cd95763 commit 7311681
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
)

// RemoteIp tries to get the remote ip and returns it or ""
func RemoteIp(r *http.Request) string {
// RemoteIP tries to get the remote ip and returns it or ""
func RemoteIP(r *http.Request) string {
a := r.Header.Get("X-Real-IP")

if a == "" {
Expand Down
24 changes: 12 additions & 12 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,61 @@ func mockRequestContext(t *testing.T, fn func(*http.Request)) {
fn(r)
}

func TestRemoteIp(t *testing.T) {
func TestRemoteIP(t *testing.T) {
mockRequestContext(t, func(r *http.Request) {
ip := "123.456.7.8"

r.Header = http.Header{
"X-Real-Ip": []string{ip},
}

out := RemoteIp(r)
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
}

func TestRemoteIpForwardedFor(t *testing.T) {
func TestRemoteIPForwardedFor(t *testing.T) {
mockRequestContext(t, func(r *http.Request) {
ip := "123.456.7.8"

r.Header = http.Header{
"X-Forwarded-For": []string{ip},
}

out := RemoteIp(r)
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
}

func TestRemoteIpRemoteAddr(t *testing.T) {
func TestRemoteIPRemoteAddr(t *testing.T) {
mockRequestContext(t, func(r *http.Request) {
ip := "123.456.7.8"
r.RemoteAddr = ip

out := RemoteIp(r)
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
}

func TestRemoteIpLocalhost(t *testing.T) {
func TestRemoteIPLocalhost(t *testing.T) {
mockRequestContext(t, func(r *http.Request) {
ip := "127.0.0.1"
r.RemoteAddr = "["

out := RemoteIp(r)
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
}

func remoteIpMockServe(h http.HandlerFunc) {
func remoteIPMockServe(h http.HandlerFunc) {
mockRequestContext(nil, func(r *http.Request) {
r.RemoteAddr = "123.456.7.8"

Expand All @@ -81,13 +81,13 @@ func remoteIpMockServe(h http.HandlerFunc) {
})
}

func ExampleRemoteIp() {
func ExampleRemoteIP() {
someHandler := func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("New request from %s\n", RemoteIp(r))
fmt.Printf("New request from %s\n", RemoteIP(r))
}

// Get's called with RemoteAddr = 123.456.7.8
remoteIpMockServe(someHandler)
remoteIPMockServe(someHandler)

// Output: New request from 123.456.7.8
}

0 comments on commit 7311681

Please sign in to comment.