2018-06-01 10:22:18 -04:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package request // import "miniflux.app/http/request"
|
2018-06-01 10:22:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
func TestFindClientIPWithoutHeaders(t *testing.T) {
|
2018-06-01 10:22:18 -04:00
|
|
|
r := &http.Request{RemoteAddr: "192.168.0.1:4242"}
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = &http.Request{RemoteAddr: "192.168.0.1"}
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
func TestFindClientIPWithXFFHeader(t *testing.T) {
|
2018-06-01 10:22:18 -04:00
|
|
|
// Test with multiple IPv4 addresses.
|
|
|
|
headers := http.Header{}
|
|
|
|
headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
|
|
|
|
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
|
|
|
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "203.0.113.195" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with single IPv6 address.
|
|
|
|
headers = http.Header{}
|
|
|
|
headers.Set("X-Forwarded-For", "2001:db8:85a3:8d3:1319:8a2e:370:7348")
|
|
|
|
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
|
|
|
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "2001:db8:85a3:8d3:1319:8a2e:370:7348" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with single IPv4 address.
|
|
|
|
headers = http.Header{}
|
|
|
|
headers.Set("X-Forwarded-For", "70.41.3.18")
|
|
|
|
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
|
|
|
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "70.41.3.18" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with invalid IP address.
|
|
|
|
headers = http.Header{}
|
|
|
|
headers.Set("X-Forwarded-For", "fake IP")
|
|
|
|
r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
|
|
|
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "192.168.0.1" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
func TestClientIPWithXRealIPHeader(t *testing.T) {
|
2018-06-01 10:22:18 -04:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Set("X-Real-Ip", "192.168.122.1")
|
|
|
|
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
|
|
|
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "192.168.122.1" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
func TestClientIPWithBothHeaders(t *testing.T) {
|
2018-06-01 10:22:18 -04:00
|
|
|
headers := http.Header{}
|
|
|
|
headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
|
|
|
|
headers.Set("X-Real-Ip", "192.168.122.1")
|
|
|
|
|
|
|
|
r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
|
|
|
|
|
2018-09-09 18:15:14 -04:00
|
|
|
if ip := FindClientIP(r); ip != "203.0.113.195" {
|
2018-06-01 10:22:18 -04:00
|
|
|
t.Fatalf(`Unexpected result, got: %q`, ip)
|
|
|
|
}
|
|
|
|
}
|