From 228d80e7c5c5db9537ab9b358447e10ccf73ff39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Ak=C4=B1n?= Date: Tue, 7 Nov 2017 09:04:46 +0300 Subject: [PATCH] Increase coverage for tests --- engine_test.go | 37 ++++++++++++++++++++++++++++++++++++- gongular_test.go | 46 +++++++++++++++++++++++++++++++++++++++++----- inejctor_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 6 deletions(-) diff --git a/engine_test.go b/engine_test.go index 2fff182..d37b26d 100644 --- a/engine_test.go +++ b/engine_test.go @@ -5,6 +5,9 @@ import ( "net/http" "testing" + "bytes" + "io/ioutil" + "github.com/stretchr/testify/assert" ) @@ -66,7 +69,7 @@ func TestGroup(t *testing.T) { assert.Equal(t, http.StatusTeapot, resp4.Code) } -func TestEngine_WithDefaultRouteCallback(t *testing.T) { +func TestEngineWithDefaultRouteCallback(t *testing.T) { e := NewEngine() e.GetRouter().GET("/", &simpleHandler{}) @@ -74,3 +77,35 @@ func TestEngine_WithDefaultRouteCallback(t *testing.T) { assert.Equal(t, http.StatusOK, resp.Code) assert.Equal(t, `"selam"`, content) } + +func TestEngineFileServe(t *testing.T) { + e := NewEngine() + e.ServeFile("/", "README.md") + e.ServeFiles("/static", http.Dir(".")) + + bytesReadme, err := ioutil.ReadFile("README.md") + if err != nil { + assert.NoError(t, err, "cannot read file") + } + + // Test binary files as well + bytesLogo, err := ioutil.ReadFile("logo.png") + if err != nil { + assert.NoError(t, err, "cannot read file") + } + + resp, content := get(t, e, "/") + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, string(bytesReadme), content) + + resp2, content2 := get(t, e, "/static/logo.png") + assert.Equal(t, http.StatusOK, resp2.Code) + + // Sorry for the inefficiency + assert.Equal(t, 0, bytes.Compare([]byte(content2), bytesLogo)) + + // Not found test + resp3, _ := get(t, e, "/static/no-file-should-be-here") + assert.Equal(t, http.StatusNotFound, resp3.Code) + +} diff --git a/gongular_test.go b/gongular_test.go index ab96678..3d254b1 100644 --- a/gongular_test.go +++ b/gongular_test.go @@ -43,14 +43,50 @@ func TestSimplePutHandler(t *testing.T) { assert.Equal(t, "\"selam\"", content) } -func TestSimpleHeadHandler(t *testing.T) { +func TestSimpleHttpMethodsHandler(t *testing.T) { e := newEngineTest() - e.GetRouter().HEAD("/", &simpleHandler{}) - resp, content := respWrap(t, e, "/", http.MethodHead, nil) + fns := map[string]func(string, ...RequestHandler){ + http.MethodHead: e.GetRouter().HEAD, + http.MethodConnect: e.GetRouter().CONNECT, + http.MethodDelete: e.GetRouter().DELETE, + http.MethodOptions: e.GetRouter().OPTIONS, + http.MethodPatch: e.GetRouter().PATCH, + http.MethodTrace: e.GetRouter().TRACE, + http.MethodPut: e.GetRouter().PUT, + } + + for method, fn := range fns { + path := "/" + method + fn(path, &simpleHandler{}) + resp, content := respWrap(t, e, path, method, nil) + + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, "\"selam\"", content) + } +} - assert.Equal(t, http.StatusOK, resp.Code) - assert.Equal(t, "\"selam\"", content) +func TestSimpleHttpMethodsAlternativeHandler(t *testing.T) { + fns := []string{ + http.MethodHead, + http.MethodConnect, + http.MethodDelete, + http.MethodOptions, + http.MethodPatch, + http.MethodTrace, + http.MethodPut, + } + + for _, method := range fns { + e := newEngineTest() + + path := "/" + method + e.GetRouter().Method(method, path, &simpleHandler{}) + resp, content := respWrap(t, e, path, method, nil) + + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, "\"selam\"", content) + } } type statusSetHandler struct{} diff --git a/inejctor_test.go b/inejctor_test.go index 75ef0be..441dbbd 100644 --- a/inejctor_test.go +++ b/inejctor_test.go @@ -129,3 +129,46 @@ func TestInjectCustomCache(t *testing.T) { assert.Equal(t, 2, callCount) } + +type dummyInterface interface { + getId() int +} + +type dummyInterfaceImpl struct { + id int +} + +func (d dummyInterfaceImpl) getId() int { + return d.id * 5 +} + +type injectionInterfaceHandler struct { + Param struct { + UserID uint + } + Dummy dummyInterface `inject:"key1"` +} + +func getDummyInterface() dummyInterface { + return dummyInterfaceImpl{ + id: 5, + } +} + +func (i *injectionInterfaceHandler) Handle(c *Context) error { + c.SetBody(fmt.Sprintf("%d:%d", i.Dummy.getId(), i.Param.UserID)) + return nil +} + +func TestInjectInterface(t *testing.T) { + e := newEngineTest() + e.errorHandler = defaultErrorHandler + e.ProvideUnsafe("key1", getDummyInterface()) + + e.GetRouter().GET("/my/interface/interaction/:UserID", &injectionInterfaceHandler{}) + + resp, content := get(t, e, "/my/interface/interaction/5") + + assert.Equal(t, http.StatusOK, resp.Code) + assert.Equal(t, `"25:5"`, content) +}