router_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package app_test
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "net/http/httptest"
  7. "reflect"
  8. "testing"
  9. "time"
  10. "context"
  11. "github.com/gorilla/mux"
  12. "github.com/ugorji/go/codec"
  13. "github.com/weaveworks/common/test"
  14. "github.com/weaveworks/scope/app"
  15. "github.com/weaveworks/scope/test/fixture"
  16. )
  17. type v map[string]string
  18. func TestURLMatcher(t *testing.T) {
  19. test := func(pattern, path string, match bool, vars v) {
  20. routeMatch := &mux.RouteMatch{}
  21. if app.URLMatcher(pattern)(&http.Request{RequestURI: path}, routeMatch) != match {
  22. t.Fatalf("'%s' '%s'", pattern, path)
  23. }
  24. if match && !reflect.DeepEqual(v(routeMatch.Vars), vars) {
  25. t.Fatalf("%v != %v", v(routeMatch.Vars), vars)
  26. }
  27. }
  28. test("/a/b/c", "/a/b/c", true, v{})
  29. test("/a/b/c", "/c/b/a", false, v{})
  30. test("/{a}/b/c", "/b/b/c", true, v{"a": "b"})
  31. test("/{a}/b/c", "/b/b/b", false, v{})
  32. test("/a/b/{c}", "/a/b/b", true, v{"c": "b"})
  33. test("/a/b/{c}", "/a/b/b%2Fb", true, v{"c": "b/b"})
  34. }
  35. func TestReportPostHandler(t *testing.T) {
  36. test := func(contentType string, encoder func(interface{}) ([]byte, error)) {
  37. router := mux.NewRouter()
  38. c := app.NewCollector(1 * time.Minute)
  39. app.RegisterReportPostHandler(c, router)
  40. ts := httptest.NewServer(router)
  41. defer ts.Close()
  42. b, err := encoder(fixture.Report)
  43. if err != nil {
  44. t.Fatalf("Content-Type %s: %s", contentType, err)
  45. }
  46. req, err := http.NewRequest("POST", ts.URL+"/api/report", bytes.NewReader(b))
  47. if err != nil {
  48. t.Fatalf("Error posting report: %v", err)
  49. }
  50. req.Header.Set("Content-Type", contentType)
  51. resp, err := http.DefaultClient.Do(req)
  52. if err != nil {
  53. t.Fatalf("Error posting report %v", err)
  54. }
  55. _, err = ioutil.ReadAll(resp.Body)
  56. resp.Body.Close()
  57. if err != nil {
  58. t.Fatalf("Error posting report: %v", err)
  59. }
  60. if resp.StatusCode != http.StatusOK {
  61. t.Fatalf("Error posting report: %d", resp.StatusCode)
  62. }
  63. ctx := context.Background()
  64. report, err := c.Report(ctx, time.Now())
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. if want, have := fixture.Report.Endpoint.Nodes, report.Endpoint.Nodes; len(have) == 0 || len(want) != len(have) {
  69. t.Fatalf("Content-Type %s: %v", contentType, test.Diff(have, want))
  70. }
  71. }
  72. test("application/json", func(v interface{}) ([]byte, error) {
  73. buf := &bytes.Buffer{}
  74. err := codec.NewEncoder(buf, &codec.JsonHandle{}).Encode(v)
  75. return buf.Bytes(), err
  76. })
  77. test("application/msgpack", func(v interface{}) ([]byte, error) {
  78. buf := &bytes.Buffer{}
  79. err := codec.NewEncoder(buf, &codec.MsgpackHandle{}).Encode(v)
  80. return buf.Bytes(), err
  81. })
  82. }