client_ip_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package request // import "miniflux.app/http/request"
  5. import (
  6. "net/http"
  7. "testing"
  8. )
  9. func TestFindClientIPWithoutHeaders(t *testing.T) {
  10. r := &http.Request{RemoteAddr: "192.168.0.1:4242"}
  11. if ip := FindClientIP(r); ip != "192.168.0.1" {
  12. t.Fatalf(`Unexpected result, got: %q`, ip)
  13. }
  14. r = &http.Request{RemoteAddr: "192.168.0.1"}
  15. if ip := FindClientIP(r); ip != "192.168.0.1" {
  16. t.Fatalf(`Unexpected result, got: %q`, ip)
  17. }
  18. }
  19. func TestFindClientIPWithXFFHeader(t *testing.T) {
  20. // Test with multiple IPv4 addresses.
  21. headers := http.Header{}
  22. headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
  23. r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  24. if ip := FindClientIP(r); ip != "203.0.113.195" {
  25. t.Fatalf(`Unexpected result, got: %q`, ip)
  26. }
  27. // Test with single IPv6 address.
  28. headers = http.Header{}
  29. headers.Set("X-Forwarded-For", "2001:db8:85a3:8d3:1319:8a2e:370:7348")
  30. r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  31. if ip := FindClientIP(r); ip != "2001:db8:85a3:8d3:1319:8a2e:370:7348" {
  32. t.Fatalf(`Unexpected result, got: %q`, ip)
  33. }
  34. // Test with single IPv4 address.
  35. headers = http.Header{}
  36. headers.Set("X-Forwarded-For", "70.41.3.18")
  37. r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  38. if ip := FindClientIP(r); ip != "70.41.3.18" {
  39. t.Fatalf(`Unexpected result, got: %q`, ip)
  40. }
  41. // Test with invalid IP address.
  42. headers = http.Header{}
  43. headers.Set("X-Forwarded-For", "fake IP")
  44. r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  45. if ip := FindClientIP(r); ip != "192.168.0.1" {
  46. t.Fatalf(`Unexpected result, got: %q`, ip)
  47. }
  48. }
  49. func TestClientIPWithXRealIPHeader(t *testing.T) {
  50. headers := http.Header{}
  51. headers.Set("X-Real-Ip", "192.168.122.1")
  52. r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  53. if ip := FindClientIP(r); ip != "192.168.122.1" {
  54. t.Fatalf(`Unexpected result, got: %q`, ip)
  55. }
  56. }
  57. func TestClientIPWithBothHeaders(t *testing.T) {
  58. headers := http.Header{}
  59. headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
  60. headers.Set("X-Real-Ip", "192.168.122.1")
  61. r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  62. if ip := FindClientIP(r); ip != "203.0.113.195" {
  63. t.Fatalf(`Unexpected result, got: %q`, ip)
  64. }
  65. }
  66. func TestClientIPWithNoRemoteAddress(t *testing.T) {
  67. r := &http.Request{}
  68. if ip := FindClientIP(r); ip != "127.0.0.1" {
  69. t.Fatalf(`Unexpected result, got: %q`, ip)
  70. }
  71. }
  72. func TestClientIPWithoutRemoteAddrAndBothHeaders(t *testing.T) {
  73. headers := http.Header{}
  74. headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
  75. headers.Set("X-Real-Ip", "192.168.122.1")
  76. r := &http.Request{RemoteAddr: "", Header: headers}
  77. if ip := FindClientIP(r); ip != "203.0.113.195" {
  78. t.Fatalf(`Unexpected result, got: %q`, ip)
  79. }
  80. }