-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrouter_test.go
More file actions
55 lines (46 loc) · 1.47 KB
/
Copy pathrouter_test.go
File metadata and controls
55 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package prometheusproxy
import (
helper "github.com/warebot/prometheusproxy/testhelpers"
"net/http"
"testing"
)
func TestAddEndpoint_ValidEndpoint(t *testing.T) {
router := NewRouter()
err := router.AddEndpoint("the-service", "http://localhost/metrics", nil)
if err != nil {
t.Error(helper.AssertError(nil, err))
}
}
func TestAddEndpoint_InvalidEndpoint(t *testing.T) {
// All endpoint URLs must be absolute
router := NewRouter()
err := router.AddEndpoint("the-service", "localhost/metrics", nil)
expectedErr := InvalidURLErr{"url must be absolute"}
if err != expectedErr {
t.Error(helper.AssertError(expectedErr, err))
}
}
func TestRoute_ValidService(t *testing.T) {
router := NewRouter()
router.AddEndpoint("the-service", "http://localhost:8080/metrics", nil)
req, _ := http.NewRequest("GET", "http://localhost/metrics?service=the-service", nil)
endpoint, err := router.Route(req)
if err != nil {
t.Error(helper.AssertError(nil, err))
}
computed := endpoint.URL.String()
expected := "http://localhost:8080/metrics"
if computed != expected {
t.Error(helper.AssertError(expected, computed))
}
}
func TestRoute_UnknownService(t *testing.T) {
router := NewRouter()
router.AddEndpoint("the-service", "http://localhost:8080/metrics", nil)
req, _ := http.NewRequest("GET", "http://localhost/metrics?service=the-other-service", nil)
_, err := router.Route(req)
expectedErr := UnknownService{}
if err != expectedErr {
t.Error(helper.AssertError(expectedErr, err))
}
}