Skip to content

Commit

Permalink
Increase coverage for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaakin committed Nov 7, 2017
1 parent 092f9d9 commit 228d80e
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
37 changes: 36 additions & 1 deletion engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"net/http"
"testing"

"bytes"
"io/ioutil"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -66,11 +69,43 @@ 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{})

resp, content := get(t, e, "/")
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)

}
46 changes: 41 additions & 5 deletions gongular_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
43 changes: 43 additions & 0 deletions inejctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 228d80e

Please sign in to comment.