setup_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package geoip
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "github.com/mholt/caddy/caddyhttp/httpserver"
  8. maxminddb "github.com/oschwald/maxminddb-golang"
  9. )
  10. type testResponseRecorder struct {
  11. *httpserver.ResponseWriterWrapper
  12. }
  13. func (testResponseRecorder) CloseNotify() <-chan bool { return nil }
  14. func TestReplacers(t *testing.T) {
  15. dbhandler, err := maxminddb.Open("./test-data/GeoLite2-City.mmdb")
  16. if err != nil {
  17. t.Errorf("geoip: Can't open database: GeoLite2-City.mmdb")
  18. }
  19. config := Config{}
  20. l := GeoIP{
  21. Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
  22. return 0, nil
  23. }),
  24. DBHandler: dbhandler,
  25. Config: config,
  26. }
  27. r := httptest.NewRequest("GET", "/", strings.NewReader(""))
  28. r.RemoteAddr = "212.50.99.193"
  29. rr := httpserver.NewResponseRecorder(testResponseRecorder{
  30. ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: httptest.NewRecorder()},
  31. })
  32. rr.Replacer = httpserver.NewReplacer(r, rr, "-")
  33. l.ServeHTTP(rr, r)
  34. if got, want := rr.Replacer.Replace("{geoip_country_code}"), "CY"; got != want {
  35. t.Errorf("Expected custom placeholder {geoip_country_code} to be set (%s), but it wasn't; got: %s", want, got)
  36. }
  37. if got, want := rr.Replacer.Replace("{geoip_country_name}"), "Cyprus"; got != want {
  38. t.Errorf("Expected custom placeholder {geoip_country_name} to be set (%s), but it wasn't; got: %s", want, got)
  39. }
  40. if got, want := rr.Replacer.Replace("{geoip_country_eu}"), "false"; got != want {
  41. t.Errorf("Expected custom placeholder {geoip_country_eu} to be set (%s), but it wasn't; got: %s", want, got)
  42. }
  43. if got, want := rr.Replacer.Replace("{geoip_city_name}"), "Limassol"; got != want {
  44. t.Errorf("Expected custom placeholder {geoip_city_name} to be set (%s), but it wasn't; got: %s", want, got)
  45. }
  46. if got, want := rr.Replacer.Replace("{geoip_latitude}"), "34.684100"; got != want {
  47. t.Errorf("Expected custom placeholder {geoip_latitude} to be set (%s), but it wasn't; got: %s", want, got)
  48. }
  49. if got, want := rr.Replacer.Replace("{geoip_longitude}"), "33.037900"; got != want {
  50. t.Errorf("Expected custom placeholder {geoip_longitude} to be set (%s), but it wasn't; got: %s", want, got)
  51. }
  52. if got, want := rr.Replacer.Replace("{geoip_time_zone}"), "Asia/Nicosia"; got != want {
  53. t.Errorf("Expected custom placeholder {geoip_time_zone} to be set (%s), but it wasn't; got: %s", want, got)
  54. }
  55. if got, want := rr.Replacer.Replace("{geoip_geohash}"), "swpmrf13wbgg"; got != want {
  56. t.Errorf("Expected custom placeholder {geoip_geohash} to be set (%s), but it wasn't; got: %s", want, got)
  57. }
  58. if got, want := rr.Replacer.Replace("{geoip_city_geoname_id}"), "146384"; got != want {
  59. t.Errorf("Expected custom placeholder {geoip_city_geoname_id} to be set (%s), but it wasn't; got: %s", want, got)
  60. }
  61. if got, want := rr.Replacer.Replace("{geoip_country_geoname_id}"), "146669"; got != want {
  62. t.Errorf("Expected custom placeholder {geoip_country_geoname_id} to be set (%s), but it wasn't; got: %s", want, got)
  63. }
  64. //
  65. // Verify that a request via the loopback interface address results in
  66. // the expected placeholder values.
  67. //
  68. var loopback_placeholders = [][2]string{
  69. {"{geoip_country_code}", "**"},
  70. {"{geoip_country_name}", "Loopback"},
  71. {"{geoip_city_name}", "Loopback"},
  72. {"{geoip_country_geoname_id}", "0"},
  73. {"{geoip_city_geoname_id}", "0"},
  74. {"{geoip_latitude}", "0.000000"},
  75. {"{geoip_longitude}", "0.000000"},
  76. {"{geoip_geohash}", "s00000000000"},
  77. {"{geoip_time_zone}", ""},
  78. }
  79. r = httptest.NewRequest("GET", "/", strings.NewReader(""))
  80. r.RemoteAddr = "127.0.0.1"
  81. rr = httpserver.NewResponseRecorder(testResponseRecorder{
  82. ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: httptest.NewRecorder()},
  83. })
  84. rr.Replacer = httpserver.NewReplacer(r, rr, "-")
  85. l.ServeHTTP(rr, r)
  86. for _, v := range loopback_placeholders {
  87. if got, want := rr.Replacer.Replace(v[0]), v[1]; got != want {
  88. t.Errorf("Expected custom placeholder %s to be set (%s), but it wasn't; got: %s", v[0], want, got)
  89. }
  90. }
  91. //
  92. // Verify that a request via a private address results in the expected
  93. // placeholder values. Note that the MaxMind DB doesn't include
  94. // location data for private addresses.
  95. //
  96. var private_addr_placeholders = [][2]string{
  97. {"{geoip_country_code}", "!!"},
  98. {"{geoip_country_name}", "No Country"},
  99. {"{geoip_city_name}", "No City"},
  100. {"{geoip_country_geoname_id}", "0"},
  101. {"{geoip_city_geoname_id}", "0"},
  102. {"{geoip_latitude}", "0.000000"},
  103. {"{geoip_longitude}", "0.000000"},
  104. {"{geoip_geohash}", "s00000000000"},
  105. {"{geoip_time_zone}", ""},
  106. }
  107. r = httptest.NewRequest("GET", "/", strings.NewReader(""))
  108. r.RemoteAddr = "192.168.0.1"
  109. rr = httpserver.NewResponseRecorder(testResponseRecorder{
  110. ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: httptest.NewRecorder()},
  111. })
  112. rr.Replacer = httpserver.NewReplacer(r, rr, "-")
  113. l.ServeHTTP(rr, r)
  114. if got, want := rr.Replacer.Replace("{geoip_country_code}"), "!!"; got != want {
  115. t.Errorf("Expected custom placeholder {geoip_country_code} to be set (%s), but it wasn't; got: %s", want, got)
  116. }
  117. for _, v := range private_addr_placeholders {
  118. if got, want := rr.Replacer.Replace(v[0]), v[1]; got != want {
  119. t.Errorf("Expected custom placeholder %s to be set (%s), but it wasn't; got: %s", v[0], want, got)
  120. }
  121. }
  122. }