setup_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }