genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] add tests

This commit is contained in:
Martin Geno 2017-03-26 19:33:47 +02:00
parent 4ae2c3a826
commit 971744c5d2
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 59 additions and 0 deletions

50
lib/http_test.go Normal file
View File

@ -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")
}

View File

@ -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")
}

1
models/testdata/config_panic.conf vendored Normal file
View File

@ -0,0 +1 @@
blub