Adds helper function. Wrap(http.HandlerFund) gin.HandlerFun

This commit is contained in:
Manu Mtz-Almeida 2015-05-19 22:18:58 +02:00
parent f2ab821223
commit 4f855faef2
2 changed files with 24 additions and 0 deletions

View File

@ -6,12 +6,19 @@ package gin
import ( import (
"encoding/xml" "encoding/xml"
"net/http"
"path" "path"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
) )
func Wrap(f http.HandlerFunc) HandlerFunc {
return func(c *Context) {
f(c.Writer, c.Request)
}
}
type H map[string]interface{} type H map[string]interface{}
// Allows type H to be used with xml.Marshal // Allows type H to be used with xml.Marshal

View File

@ -5,6 +5,8 @@
package gin package gin
import ( import (
"fmt"
"net/http"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -14,6 +16,21 @@ func init() {
SetMode(TestMode) SetMode(TestMode)
} }
func TestWrap(t *testing.T) {
router := New()
router.GET("/path", Wrap(func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, req.Method, "GET")
assert.Equal(t, req.URL.Path, "/path")
w.WriteHeader(400)
fmt.Fprint(w, "hola!")
}))
w := performRequest(router, "GET", "/path")
assert.Equal(t, w.Code, 400)
assert.Equal(t, w.Body.String(), "hola!")
}
func TestLastChar(t *testing.T) { func TestLastChar(t *testing.T) {
assert.Equal(t, lastChar("hola"), uint8('a')) assert.Equal(t, lastChar("hola"), uint8('a'))
assert.Equal(t, lastChar("adios"), uint8('s')) assert.Equal(t, lastChar("adios"), uint8('s'))