func TestRoot(t *testing.T) { s := New() defer s.Stop() s.Init(staticSettings()) req, err := http.NewRequest("GET", "http://localhost/", nil) if err != nil { t.Fatal(err) } // Test without cookie. w := testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusForbidden { t.Errorf("Expected forbidden code, got: %s", w.Sdump()) } if len(w.HeaderMap["Set-Cookie"]) != 0 { t.Errorf("No cookie should have been set; got: %s", w.Sdump()) } // Test with cookie. req = getRequest("/") w = testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusOK { t.Errorf("Expected ok code, got: %s", w.Sdump()) } }
func TestResumeUpdates(t *testing.T) { s := New() defer s.Stop() s.runID = testutils.StaticRunID s.Init(staticSettings()) s.tree.Slot().Set("foo") s.Publish([]*core.Node{s.tree.Slot().Node()}) s.tree.Slot().Set("bar") s.Publish([]*core.Node{s.tree.Slot().Node()}) req := getRequest("/_stdweb/updates?updateid=1&runid=" + testutils.StaticRunID) w := testutils.NewRecorder() done := make(chan struct{}) go func() { s.ServeHTTP(w, req) close(done) }() nu := unmarshal(nextSSE(w.Body)).Nodes["sw"] if nu.Payload != "bar" { t.Errorf("Expected 'bar', got %q", nu.Payload) } s.tree.Slot().Set("bar2") s.Publish([]*core.Node{s.tree.Slot().Node()}) nu = unmarshal(nextSSE(w.Body)).Nodes["sw"] if nu.Payload != "bar2" { t.Errorf("Expected 'bar2', got %q", nu.Payload) } }
func TestInvalidRunID(t *testing.T) { s := New() defer s.Stop() s.runID = testutils.StaticRunID s.Init(staticSettings()) req := getRequest("/_stdweb/updates?runid=nop") w := testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusGone { t.Errorf("Expected gone code, got: %s", w.Sdump()) } req = getRequest("/_stdweb/updates") w = testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusGone { t.Errorf("Expected gone code, got: %s", w.Sdump()) } }
func TestInvalidUpdateID(t *testing.T) { s := New() defer s.Stop() s.runID = testutils.StaticRunID s.Init(staticSettings()) req := getRequest("/_stdweb/updates?updateid=nop&runid=" + testutils.StaticRunID) w := testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Errorf("Expected bad request, got: %s", w.Sdump()) } req = getRequest("/_stdweb/updates?updateid=1&runid=" + testutils.StaticRunID) req.Header.Add("Last-Event-ID", "invalid") w = testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Errorf("Expected bad request, got: %s", w.Sdump()) } }
func TestNoInit(t *testing.T) { s := New() defer s.Stop() req, err := http.NewRequest("GET", "http://localhost/", nil) if err != nil { t.Fatal(err) } w := testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Errorf("Expected server failure, got: %s", w.Sdump()) } }
// TestUpdatesTerminates verifies that when the client disconnects, the http // handler properly terminates. func TestUpdatesTerminates(t *testing.T) { s := New() defer s.Stop() s.Init(staticSettings()) req := getRequest("/_stdweb/updates") w := testutils.NewRecorder() done := make(chan struct{}) go func() { s.ServeHTTP(w, req) close(done) }() close(w.Done) <-done }
func TestAuthPath(t *testing.T) { s := New() defer s.Stop() s.Init(staticSettings()) authpath := fmt.Sprintf("http://localhost%s", s.AuthPath()) req, err := http.NewRequest("GET", authpath, nil) if err != nil { t.Fatal(err) } w := testutils.NewRecorder() s.ServeHTTP(w, req) if w.Code != http.StatusTemporaryRedirect { t.Errorf("Expected redirect, got: %s", w.Sdump()) } // Check cookie is set. if len(w.HeaderMap["Set-Cookie"]) != 1 { t.Fatalf("Expected only one Set-Cookie, got: %s", w.Sdump()) } header := http.Header{} header.Add("Cookie", w.HeaderMap["Set-Cookie"][0]) req = &http.Request{Header: header} found := false for _, cookie := range req.Cookies() { if cookie.Name == CookieName && cookie.Value == staticKey { found = true break } } if !found { t.Errorf("Unable to find cookie; got: %s", w.Sdump()) } }
func TestPublish(t *testing.T) { s := New() defer s.Stop() s.runID = testutils.StaticRunID s.Init(staticSettings()) req := getRequest("/_stdweb/updates?runid=" + testutils.StaticRunID) w := testutils.NewRecorder() done := make(chan struct{}) go func() { s.ServeHTTP(w, req) close(done) }() // Get initial update (and ignore it) to make sure we are connected. nextSSE(w.Body) s.tree.Slot().Set("foo") s.Publish([]*core.Node{s.tree.Slot().Node()}) u := unmarshal(nextSSE(w.Body)) nu := u.Nodes["sw"] if nu == nil { t.Fatalf("No node found in update (update: %v)", u) } if nu.ID != "sw" { t.Errorf("Expected ID 'sw', got %q", nu.ID) } if nu.Payload != "foo" { t.Errorf("Expected payload 'foo', got %q", nu.Payload) } close(w.Done) <-done }