From c63a62ab2af09ce7084e40c604a6a55a3476f1db Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sun, 14 Feb 2016 21:10:18 -0800 Subject: [PATCH] added initial http test. --- api_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 api_test.go diff --git a/api_test.go b/api_test.go new file mode 100644 index 0000000..b535635 --- /dev/null +++ b/api_test.go @@ -0,0 +1,80 @@ +package vain + +import ( + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestAdd(t *testing.T) { + ms := NewMemStore() + s := &Server{ + storage: ms, + } + ts := httptest.NewServer(s) + s.hostname = ts.URL + resp, err := http.Get(ts.URL) + if err != nil { + t.Errorf("couldn't GET: %v", err) + } + resp.Body.Close() + if len(s.storage.p) != 0 { + t.Errorf("started with something in it; got %d, want %d", len(s.storage.p), 0) + } + + bad := ts.URL + resp, err = http.Post(bad, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + if err != nil { + t.Errorf("couldn't POST: %v", err) + } + resp.Body.Close() + if len(s.storage.p) != 0 { + t.Errorf("started with something in it; got %d, want %d", len(s.storage.p), 0) + } + + good := fmt.Sprintf("%s/foo", ts.URL) + resp, err = http.Post(good, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + if err != nil { + t.Errorf("couldn't POST: %v", err) + } + + if len(s.storage.p) != 1 { + t.Errorf("storage should have something in it; got %d, want %d", len(s.storage.p), 1) + } + + p, ok := s.storage.p[good] + if !ok { + t.Fatalf("did not find package for %s; should have posted a valid package", good) + } + if p.Path != good { + t.Errorf("package name did not go through as expected; got %q, want %q", p.Path, good) + } + if want := "https://s.mcquay.me/sm/vain"; p.Repo != want { + t.Errorf("repo did not go through as expected; got %q, want %q", p.Repo, want) + } + if want := Git; p.Vcs != want { + t.Errorf("Vcs did not go through as expected; got %q, want %q", p.Vcs, want) + } +} + +func TestInvalidPath(t *testing.T) { + ms := NewMemStore() + s := &Server{ + storage: ms, + } + ts := httptest.NewServer(s) + s.hostname = ts.URL + + resp, err := http.Post(ts.URL, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + if err != nil { + t.Errorf("couldn't POST: %v", err) + } + if len(s.storage.p) != 0 { + t.Errorf("should have failed to insert; got %d, want %d", len(s.storage.p), 0) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(http.StatusBadRequest)) + } +}