httputil_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httputil
  15. import (
  16. "net/http"
  17. "testing"
  18. )
  19. func TestGetHostname(t *testing.T) {
  20. tt := []struct {
  21. req *http.Request
  22. host string
  23. }{
  24. {&http.Request{Host: "localhost"}, "localhost"},
  25. {&http.Request{Host: "localhost:2379"}, "localhost"},
  26. {&http.Request{Host: "localhost."}, "localhost."},
  27. {&http.Request{Host: "localhost.:2379"}, "localhost."},
  28. {&http.Request{Host: "127.0.0.1"}, "127.0.0.1"},
  29. {&http.Request{Host: "127.0.0.1:2379"}, "127.0.0.1"},
  30. {&http.Request{Host: "localhos"}, "localhos"},
  31. {&http.Request{Host: "localhos:2379"}, "localhos"},
  32. {&http.Request{Host: "localhos."}, "localhos."},
  33. {&http.Request{Host: "localhos.:2379"}, "localhos."},
  34. {&http.Request{Host: "1.2.3.4"}, "1.2.3.4"},
  35. {&http.Request{Host: "1.2.3.4:2379"}, "1.2.3.4"},
  36. // too many colons in address
  37. {&http.Request{Host: "localhost:::::"}, "localhost:::::"},
  38. }
  39. for i := range tt {
  40. hv := GetHostname(tt[i].req)
  41. if hv != tt[i].host {
  42. t.Errorf("#%d: %q expected host %q, got '%v'", i, tt[i].req.Host, tt[i].host, hv)
  43. }
  44. }
  45. }