Addressing go vet / golint

This commit is contained in:
sm
2016-02-15 01:10:49 -08:00
parent c5c1dbc4a9
commit 9e9bb74c4a
5 changed files with 203 additions and 63 deletions
+114 -13
View File
@@ -1,7 +1,9 @@
package vain
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
@@ -20,8 +22,8 @@ func TestAdd(t *testing.T) {
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)
if len(ms.p) != 0 {
t.Errorf("started with something in it; got %d, want %d", len(ms.p), 0)
}
bad := ts.URL
@@ -30,8 +32,8 @@ func TestAdd(t *testing.T) {
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)
if len(ms.p) != 0 {
t.Errorf("started with something in it; got %d, want %d", len(ms.p), 0)
}
good := fmt.Sprintf("%s/foo", ts.URL)
@@ -40,16 +42,16 @@ func TestAdd(t *testing.T) {
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)
if len(ms.p) != 1 {
t.Errorf("storage should have something in it; got %d, want %d", len(ms.p), 1)
}
p, ok := s.storage.p[good]
p, ok := ms.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 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)
@@ -57,6 +59,22 @@ func TestAdd(t *testing.T) {
if want := Git; p.Vcs != want {
t.Errorf("Vcs did not go through as expected; got %q, want %q", p.Vcs, want)
}
resp, err = http.Get(ts.URL)
if err != nil {
t.Errorf("couldn't GET: %v", err)
}
defer resp.Body.Close()
if want := http.StatusOK; resp.StatusCode != want {
t.Errorf("Should have succeeded to fetch /; got %s, want %s", resp.Status, http.StatusText(want))
}
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, resp.Body); err != nil {
t.Errorf("couldn't read content from server: %v", err)
}
if got, want := strings.Count(buf.String(), "meta"), 1; got != want {
t.Errorf("did not find all the tags I need; got %d, want %d", got, want)
}
}
func TestInvalidPath(t *testing.T) {
@@ -71,11 +89,11 @@ func TestInvalidPath(t *testing.T) {
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 len(ms.p) != 0 {
t.Errorf("should have failed to insert; got %d, want %d", len(ms.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))
if want := http.StatusBadRequest; resp.StatusCode != want {
t.Errorf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(want))
}
}
@@ -131,3 +149,86 @@ func TestCannotAddExistingSubPath(t *testing.T) {
t.Errorf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want))
}
}
func TestMissingRepo(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(`{}`))
if err != nil {
t.Errorf("couldn't POST: %v", err)
}
if len(ms.p) != 0 {
t.Errorf("should have failed to insert; got %d, want %d", len(ms.p), 0)
}
if want := http.StatusBadRequest; resp.StatusCode != want {
t.Errorf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(want))
}
}
func TestBadJson(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(`{`))
if err != nil {
t.Errorf("couldn't POST: %v", err)
}
if len(ms.p) != 0 {
t.Errorf("should have failed to insert; got %d, want %d", len(ms.p), 0)
}
if want := http.StatusBadRequest; resp.StatusCode != want {
t.Errorf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(want))
}
}
func TestUnsupportedMethod(t *testing.T) {
ms := NewMemStore("")
s := &Server{
storage: ms,
}
ts := httptest.NewServer(s)
s.hostname = ts.URL
url := fmt.Sprintf("%s/foo", ts.URL)
client := &http.Client{}
req, err := http.NewRequest("PUT", url, nil)
resp, err := client.Do(req)
if err != nil {
t.Errorf("couldn't POST: %v", err)
}
if len(ms.p) != 0 {
t.Errorf("should have failed to insert; got %d, want %d", len(ms.p), 0)
}
if want := http.StatusMethodNotAllowed; resp.StatusCode != want {
t.Errorf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(want))
}
}
func TestNewServer(t *testing.T) {
ms := NewMemStore("")
sm := http.NewServeMux()
s := NewServer(sm, ms, "foo")
ts := httptest.NewServer(s)
s.hostname = ts.URL
url := fmt.Sprintf("%s/foo", ts.URL)
client := &http.Client{}
req, err := http.NewRequest("PUT", url, nil)
resp, err := client.Do(req)
if err != nil {
t.Errorf("couldn't POST: %v", err)
}
if len(ms.p) != 0 {
t.Errorf("should have failed to insert; got %d, want %d", len(ms.p), 0)
}
if want := http.StatusMethodNotAllowed; resp.StatusCode != want {
t.Errorf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(want))
}
}