client_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2015 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // +build go1.7
  14. package api
  15. import (
  16. "net/http"
  17. "net/url"
  18. "testing"
  19. )
  20. func TestConfig(t *testing.T) {
  21. c := Config{}
  22. if c.roundTripper() != DefaultRoundTripper {
  23. t.Fatalf("expected default roundtripper for nil RoundTripper field")
  24. }
  25. }
  26. func TestClientURL(t *testing.T) {
  27. tests := []struct {
  28. address string
  29. endpoint string
  30. args map[string]string
  31. expected string
  32. }{
  33. {
  34. address: "http://localhost:9090",
  35. endpoint: "/test",
  36. expected: "http://localhost:9090/test",
  37. },
  38. {
  39. address: "http://localhost",
  40. endpoint: "/test",
  41. expected: "http://localhost/test",
  42. },
  43. {
  44. address: "http://localhost:9090",
  45. endpoint: "test",
  46. expected: "http://localhost:9090/test",
  47. },
  48. {
  49. address: "http://localhost:9090/prefix",
  50. endpoint: "/test",
  51. expected: "http://localhost:9090/prefix/test",
  52. },
  53. {
  54. address: "https://localhost:9090/",
  55. endpoint: "/test/",
  56. expected: "https://localhost:9090/test",
  57. },
  58. {
  59. address: "http://localhost:9090",
  60. endpoint: "/test/:param",
  61. args: map[string]string{
  62. "param": "content",
  63. },
  64. expected: "http://localhost:9090/test/content",
  65. },
  66. {
  67. address: "http://localhost:9090",
  68. endpoint: "/test/:param/more/:param",
  69. args: map[string]string{
  70. "param": "content",
  71. },
  72. expected: "http://localhost:9090/test/content/more/content",
  73. },
  74. {
  75. address: "http://localhost:9090",
  76. endpoint: "/test/:param/more/:foo",
  77. args: map[string]string{
  78. "param": "content",
  79. "foo": "bar",
  80. },
  81. expected: "http://localhost:9090/test/content/more/bar",
  82. },
  83. {
  84. address: "http://localhost:9090",
  85. endpoint: "/test/:param",
  86. args: map[string]string{
  87. "nonexistent": "content",
  88. },
  89. expected: "http://localhost:9090/test/:param",
  90. },
  91. }
  92. for _, test := range tests {
  93. ep, err := url.Parse(test.address)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. hclient := &httpClient{
  98. endpoint: ep,
  99. client: http.Client{Transport: DefaultRoundTripper},
  100. }
  101. u := hclient.URL(test.endpoint, test.args)
  102. if u.String() != test.expected {
  103. t.Errorf("unexpected result: got %s, want %s", u, test.expected)
  104. continue
  105. }
  106. }
  107. }