client_ip_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. r = &http.Request{RemoteAddr: "fe80::14c2:f039:edc7:edc7"}
  19. if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
  20. t.Fatalf(`Unexpected result, got: %q`, ip)
  21. }
  22. r = &http.Request{RemoteAddr: "fe80::14c2:f039:edc7:edc7%eth0"}
  23. if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
  24. t.Fatalf(`Unexpected result, got: %q`, ip)
  25. }
  26. r = &http.Request{RemoteAddr: "[fe80::14c2:f039:edc7:edc7%eth0]:4242"}
  27. if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
  28. t.Fatalf(`Unexpected result, got: %q`, ip)
  29. }
  30. }
  31. func TestFindClientIPWithXFFHeader(t *testing.T) {
  32. // Test with multiple IPv4 addresses.
  33. headers := http.Header{}
  34. headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
  35. r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  36. if ip := FindClientIP(r); ip != "203.0.113.195" {
  37. t.Fatalf(`Unexpected result, got: %q`, ip)
  38. }
  39. // Test with single IPv6 address.
  40. headers = http.Header{}
  41. headers.Set("X-Forwarded-For", "2001:db8:85a3:8d3:1319:8a2e:370:7348")
  42. r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  43. if ip := FindClientIP(r); ip != "2001:db8:85a3:8d3:1319:8a2e:370:7348" {
  44. t.Fatalf(`Unexpected result, got: %q`, ip)
  45. }
  46. // Test with single IPv6 address with zone
  47. headers = http.Header{}
  48. headers.Set("X-Forwarded-For", "fe80::14c2:f039:edc7:edc7%eth0")
  49. r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  50. if ip := FindClientIP(r); ip != "fe80::14c2:f039:edc7:edc7" {
  51. t.Fatalf(`Unexpected result, got: %q`, ip)
  52. }
  53. // Test with single IPv4 address.
  54. headers = http.Header{}
  55. headers.Set("X-Forwarded-For", "70.41.3.18")
  56. r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  57. if ip := FindClientIP(r); ip != "70.41.3.18" {
  58. t.Fatalf(`Unexpected result, got: %q`, ip)
  59. }
  60. // Test with invalid IP address.
  61. headers = http.Header{}
  62. headers.Set("X-Forwarded-For", "fake IP")
  63. r = &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  64. if ip := FindClientIP(r); ip != "192.168.0.1" {
  65. t.Fatalf(`Unexpected result, got: %q`, ip)
  66. }
  67. }
  68. func TestClientIPWithXRealIPHeader(t *testing.T) {
  69. headers := http.Header{}
  70. headers.Set("X-Real-Ip", "192.168.122.1")
  71. r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  72. if ip := FindClientIP(r); ip != "192.168.122.1" {
  73. t.Fatalf(`Unexpected result, got: %q`, ip)
  74. }
  75. }
  76. func TestClientIPWithBothHeaders(t *testing.T) {
  77. headers := http.Header{}
  78. headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
  79. headers.Set("X-Real-Ip", "192.168.122.1")
  80. r := &http.Request{RemoteAddr: "192.168.0.1:4242", Header: headers}
  81. if ip := FindClientIP(r); ip != "203.0.113.195" {
  82. t.Fatalf(`Unexpected result, got: %q`, ip)
  83. }
  84. }
  85. func TestClientIPWithNoRemoteAddress(t *testing.T) {
  86. r := &http.Request{}
  87. if ip := FindClientIP(r); ip != "127.0.0.1" {
  88. t.Fatalf(`Unexpected result, got: %q`, ip)
  89. }
  90. }
  91. func TestClientIPWithoutRemoteAddrAndBothHeaders(t *testing.T) {
  92. headers := http.Header{}
  93. headers.Set("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
  94. headers.Set("X-Real-Ip", "192.168.122.1")
  95. r := &http.Request{RemoteAddr: "", Header: headers}
  96. if ip := FindClientIP(r); ip != "203.0.113.195" {
  97. t.Fatalf(`Unexpected result, got: %q`, ip)
  98. }
  99. }