forked from ggicci/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgorilla_test.go
More file actions
41 lines (34 loc) · 1.05 KB
/
gorilla_test.go
File metadata and controls
41 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package httpin
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/justinas/alice"
. "github.com/smartystreets/goconvey/convey"
)
type GetPostOfUserInput struct {
Username string `in:"path=username"`
PostID int64 `in:"path=pid"`
}
func GetPostOfUserHandler(rw http.ResponseWriter, r *http.Request) {
var input = r.Context().Value(Input).(*GetPostOfUserInput)
json.NewEncoder(rw).Encode(input)
}
func TestGorillaMuxVars(t *testing.T) {
UseGorillaMux("path", mux.Vars) // register the "path" executor
Convey("Gorilla: can extract mux vars", t, func() {
rw := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/ggicci/posts/1024", nil)
So(err, ShouldBeNil)
router := mux.NewRouter()
router.Handle("/{username}/posts/{pid}", alice.New(
NewInput(GetPostOfUserInput{}),
).ThenFunc(GetPostOfUserHandler)).Methods("GET")
router.ServeHTTP(rw, r)
So(rw.Code, ShouldEqual, 200)
expected := `{"Username":"ggicci","PostID":1024}` + "\n"
So(rw.Body.String(), ShouldEqual, expected)
})
}