From 971744c5d2bdbbdfce53aee0c3408d336e33a833 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Sun, 26 Mar 2017 19:33:47 +0200 Subject: [PATCH] [TASK] add tests --- lib/http_test.go | 50 +++++++++++++++++++++++++++++++ models/config_test.go | 8 +++++ models/testdata/config_panic.conf | 1 + 3 files changed, 59 insertions(+) create mode 100644 lib/http_test.go create mode 100644 models/testdata/config_panic.conf diff --git a/lib/http_test.go b/lib/http_test.go new file mode 100644 index 0000000..c294a93 --- /dev/null +++ b/lib/http_test.go @@ -0,0 +1,50 @@ +package lib + +import ( + "bytes" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestWrite(t *testing.T) { + assert := assert.New(t) + + w := httptest.NewRecorder() + from := map[string]string{"a": "b"} + Write(w, from) + result := w.Result() + + assert.Equal([]string{"application/json"}, result.Header["Content-Type"], "no header information") + buf := new(bytes.Buffer) + buf.ReadFrom(result.Body) + to := buf.String() + assert.Equal("{\"a\":\"b\"}", to, "wrong content") + + w = httptest.NewRecorder() + value := make(chan int) + Write(w, value) + result = w.Result() + + assert.Equal(http.StatusInternalServerError, result.StatusCode, "wrong statuscode") + +} + +func TestRead(t *testing.T) { + assert := assert.New(t) + + to := make(map[string]string) + r, _ := http.NewRequest("GET", "/a", strings.NewReader("{\"a\":\"b\"}")) + + r.Header["Content-Type"] = []string{"application/json"} + err := Read(r, &to) + assert.NoError(err, "no error") + assert.Equal(map[string]string{"a": "b"}, to, "wrong content") + + r.Header["Content-Type"] = []string{""} + err = Read(r, &to) + assert.Error(err, "no error") +} diff --git a/models/config_test.go b/models/config_test.go index 4dd8b06..19fefb0 100644 --- a/models/config_test.go +++ b/models/config_test.go @@ -13,4 +13,12 @@ func TestReadConfig(t *testing.T) { assert.NotNil(config) assert.Equal("[::1]:8080", config.WebserverBind) + + assert.Panics(func() { + ReadConfigFile("../config_example.co") + }, "wrong file") + + assert.Panics(func() { + ReadConfigFile("testdata/config_panic.conf") + }, "wrong toml") } diff --git a/models/testdata/config_panic.conf b/models/testdata/config_panic.conf new file mode 100644 index 0000000..40142d0 --- /dev/null +++ b/models/testdata/config_panic.conf @@ -0,0 +1 @@ +blub