From af4faf02065f998d7bf68c456dcc774106e7f86e Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sun, 14 Feb 2016 21:52:26 -0800 Subject: [PATCH] Added http tests for conflicts fixes #4. --- api_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/api_test.go b/api_test.go index b535635..7810d94 100644 --- a/api_test.go +++ b/api_test.go @@ -78,3 +78,56 @@ func TestInvalidPath(t *testing.T) { t.Errorf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(http.StatusBadRequest)) } } + +func TestCannotDuplicateExistingPath(t *testing.T) { + ms := NewMemStore() + s := &Server{ + storage: ms, + } + ts := httptest.NewServer(s) + s.hostname = ts.URL + + url := fmt.Sprintf("%s/foo", ts.URL) + resp, err := http.Post(url, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + if err != nil { + t.Errorf("couldn't POST: %v", err) + } + if want := http.StatusOK; resp.StatusCode != want { + t.Errorf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want)) + } + resp, err = http.Post(url, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + if err != nil { + t.Errorf("couldn't POST: %v", err) + } + if want := http.StatusConflict; resp.StatusCode != want { + t.Errorf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want)) + } +} + +func TestCannotAddExistingSubPath(t *testing.T) { + ms := NewMemStore() + s := &Server{ + storage: ms, + } + ts := httptest.NewServer(s) + s.hostname = ts.URL + + url := fmt.Sprintf("%s/foo/bar", ts.URL) + resp, err := http.Post(url, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + if err != nil { + t.Errorf("couldn't POST: %v", err) + } + if want := http.StatusOK; resp.StatusCode != want { + t.Errorf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want)) + } + + url = fmt.Sprintf("%s/foo", ts.URL) + resp, err = http.Post(url, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + resp, err = http.Post(url, "application/json", strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)) + if err != nil { + t.Errorf("couldn't POST: %v", err) + } + if want := http.StatusConflict; resp.StatusCode != want { + t.Errorf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want)) + } +}