diff options
author | Corey Hulen <corey@hulen.com> | 2016-11-22 11:05:54 -0800 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2016-11-22 14:05:54 -0500 |
commit | 7961599b2e41c71720a42b3bfde641f7529f05fe (patch) | |
tree | 3c039e1d3790a954ba65fe551c7b348331bce994 /vendor/github.com/prometheus/common/route/route_test.go | |
parent | e033dcce8e57ed6b6684227adf9b29347e4718b3 (diff) | |
download | chat-7961599b2e41c71720a42b3bfde641f7529f05fe.tar.gz chat-7961599b2e41c71720a42b3bfde641f7529f05fe.tar.bz2 chat-7961599b2e41c71720a42b3bfde641f7529f05fe.zip |
PLT-4357 adding performance monitoring (#4622)
* WIP
* WIP
* Adding metrics collection
* updating vendor packages
* Adding metrics to config
* Adding admin console page for perf monitoring
* Updating glide
* switching to tylerb/graceful
Diffstat (limited to 'vendor/github.com/prometheus/common/route/route_test.go')
-rw-r--r-- | vendor/github.com/prometheus/common/route/route_test.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/vendor/github.com/prometheus/common/route/route_test.go b/vendor/github.com/prometheus/common/route/route_test.go new file mode 100644 index 000000000..4055d69d5 --- /dev/null +++ b/vendor/github.com/prometheus/common/route/route_test.go @@ -0,0 +1,75 @@ +package route + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "golang.org/x/net/context" +) + +func TestRedirect(t *testing.T) { + router := New(nil).WithPrefix("/test/prefix") + w := httptest.NewRecorder() + r, err := http.NewRequest("GET", "http://localhost:9090/foo", nil) + if err != nil { + t.Fatalf("Error building test request: %s", err) + } + + router.Redirect(w, r, "/some/endpoint", http.StatusFound) + if w.Code != http.StatusFound { + t.Fatalf("Unexpected redirect status code: got %d, want %d", w.Code, http.StatusFound) + } + + want := "/test/prefix/some/endpoint" + got := w.Header()["Location"][0] + if want != got { + t.Fatalf("Unexpected redirect location: got %s, want %s", got, want) + } +} + +func TestContextFn(t *testing.T) { + router := New(func(r *http.Request) (context.Context, error) { + return context.WithValue(context.Background(), "testkey", "testvalue"), nil + }) + + router.Get("/test", func(w http.ResponseWriter, r *http.Request) { + want := "testvalue" + got := Context(r).Value("testkey") + if want != got { + t.Fatalf("Unexpected context value: want %q, got %q", want, got) + } + }) + + r, err := http.NewRequest("GET", "http://localhost:9090/test", nil) + if err != nil { + t.Fatalf("Error building test request: %s", err) + } + router.ServeHTTP(nil, r) +} + +func TestContextFnError(t *testing.T) { + router := New(func(r *http.Request) (context.Context, error) { + return context.Background(), fmt.Errorf("test error") + }) + + router.Get("/test", func(w http.ResponseWriter, r *http.Request) {}) + + r, err := http.NewRequest("GET", "http://localhost:9090/test", nil) + if err != nil { + t.Fatalf("Error building test request: %s", err) + } + w := httptest.NewRecorder() + router.ServeHTTP(w, r) + + if w.Code != http.StatusBadRequest { + t.Fatalf("Unexpected response status: got %q, want %q", w.Code, http.StatusBadRequest) + } + + want := "Error creating request context: test error\n" + got := w.Body.String() + if want != got { + t.Fatalf("Unexpected response body: got %q, want %q", got, want) + } +} |