More unit tests

This commit is contained in:
Manu Mtz-Almeida 2015-07-03 04:20:18 +02:00
parent 4194adce4c
commit 0316b735c4
3 changed files with 24 additions and 5 deletions

View File

@ -77,6 +77,25 @@ func TestContextReset(t *testing.T) {
assert.Equal(t, c.Writer.(*responseWriter), &c.writermem) assert.Equal(t, c.Writer.(*responseWriter), &c.writermem)
} }
func TestContextHandlers(t *testing.T) {
c, _, _ := createTestContext()
assert.Nil(t, c.handlers)
assert.Nil(t, c.handlers.Last())
c.handlers = HandlersChain{}
assert.NotNil(t, c.handlers)
assert.Nil(t, c.handlers.Last())
f := func(c *Context) {}
g := func(c *Context) {}
c.handlers = HandlersChain{f}
compareFunc(t, f, c.handlers.Last())
c.handlers = HandlersChain{f, g}
compareFunc(t, g, c.handlers.Last())
}
// TestContextSetGet tests that a parameter is set correctly on the // TestContextSetGet tests that a parameter is set correctly on the
// current context and can be retrieved using Get. // current context and can be retrieved using Get.
func TestContextSetGet(t *testing.T) { func TestContextSetGet(t *testing.T) {
@ -190,13 +209,13 @@ func TestContextQueryAndPostForm(t *testing.T) {
var obj struct { var obj struct {
Foo string `form:"foo"` Foo string `form:"foo"`
Id string `form:"id"` ID string `form:"id"`
Page string `form:"page"` Page string `form:"page"`
Both string `form:"both"` Both string `form:"both"`
} }
assert.NoError(t, c.Bind(&obj)) assert.NoError(t, c.Bind(&obj))
assert.Equal(t, obj.Foo, "bar") assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.Id, "main") assert.Equal(t, obj.ID, "main")
assert.Equal(t, obj.Page, "11") assert.Equal(t, obj.Page, "11")
assert.Equal(t, obj.Both, "POST") assert.Equal(t, obj.Both, "POST")
} }

View File

@ -63,6 +63,7 @@ func TestErrorSlice(t *testing.T) {
{Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}}, {Err: errors.New("third"), Type: ErrorTypePublic, Meta: H{"status": "400"}},
} }
assert.Equal(t, errs, errs.ByType(ErrorTypeAny))
assert.Equal(t, errs.Last().Error(), "third") assert.Equal(t, errs.Last().Error(), "third")
assert.Equal(t, errs.Errors(), []string{"first", "second", "third"}) assert.Equal(t, errs.Errors(), []string{"first", "second", "third"})
assert.Equal(t, errs.ByType(ErrorTypePublic).Errors(), []string{"third"}) assert.Equal(t, errs.ByType(ErrorTypePublic).Errors(), []string{"third"})

5
fs.go
View File

@ -14,7 +14,7 @@ type (
} }
) )
// It returns a http.Filesystem that can be used by http.FileServer(). It is used interally // Dir returns a http.Filesystem that can be used by http.FileServer(). It is used interally
// in router.Static(). // in router.Static().
// if listDirectory == true, then it works the same as http.Dir() otherwise it returns // if listDirectory == true, then it works the same as http.Dir() otherwise it returns
// a filesystem that prevents http.FileServer() to list the directory files. // a filesystem that prevents http.FileServer() to list the directory files.
@ -22,9 +22,8 @@ func Dir(root string, listDirectory bool) http.FileSystem {
fs := http.Dir(root) fs := http.Dir(root)
if listDirectory { if listDirectory {
return fs return fs
} else {
return &onlyfilesFS{fs}
} }
return &onlyfilesFS{fs}
} }
// Conforms to http.Filesystem // Conforms to http.Filesystem