From c3cb810bbf95d765425aaf0f9531b5f48ffb8d24 Mon Sep 17 00:00:00 2001 From: Joseph Spurrier Date: Wed, 13 Jun 2018 00:44:09 -0400 Subject: [PATCH] Add travis --- .gitignore | 6 ++++++ .travis.yml | 23 +++++++++++++++++++++++ h.go | 7 +++++++ h_test.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 .travis.yml create mode 100644 h_test.go diff --git a/.gitignore b/.gitignore index f1c181e..a6da2ae 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,9 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out + +# OS files +.DS_Store + +# Vendor files +/vendor/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..21317ee --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +language: go + +go: + #- 1.0 + #- 1.1 + #- 1.2 + #- 1.3 + - 1.4 + - 1.5 + - 1.6 + - 1.7 + - 1.8 + - 1.9 + - 1.10 + - tip + +before_install: + - go get github.com/axw/gocov/gocov + - go get github.com/mattn/goveralls + - go get golang.org/x/tools/cmd/cover + +script: + - $HOME/gopath/bin/goveralls \ No newline at end of file diff --git a/h.go b/h.go index 925b0ae..0e2469e 100644 --- a/h.go +++ b/h.go @@ -14,6 +14,13 @@ type F func(http.ResponseWriter, *http.Request) (int, error) // F.ServeHTTP. var ServeHTTP = func(w http.ResponseWriter, r *http.Request, status int, err error) { + if status >= 400 { + if err != nil { + http.Error(w, err.Error(), status) + } else { + http.Error(w, "", status) + } + } } // ServeHTTP calls f(w, r) and passes the result to h.ServeHTTP. diff --git a/h_test.go b/h_test.go new file mode 100644 index 0000000..b06e843 --- /dev/null +++ b/h_test.go @@ -0,0 +1,33 @@ +package h_test + +import ( + "errors" + "net/http" + "net/http/httptest" + "testing" + + "github.com/josephspurrier/h" + "github.com/stretchr/testify/assert" +) + +func TestServeHTTPResponseOK(t *testing.T) { + r := httptest.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + h.ServeHTTP(w, r, http.StatusOK, nil) + assert.Equal(t, http.StatusOK, w.Code) +} + +func TestServeHTTPResponseIgnoreLessThan200(t *testing.T) { + r := httptest.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + h.ServeHTTP(w, r, http.StatusCreated, nil) + assert.Equal(t, http.StatusOK, w.Code) +} + +func TestServeHTTPResponseError(t *testing.T) { + r := httptest.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + h.ServeHTTP(w, r, http.StatusInternalServerError, errors.New("error happened")) + assert.Equal(t, http.StatusInternalServerError, w.Code) + assert.Contains(t, w.Body.String(), "error happened") +}