-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
37 lines (34 loc) · 747 Bytes
/
server_test.go
File metadata and controls
37 lines (34 loc) · 747 Bytes
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
package web
import (
"net/http"
"testing"
)
type User struct {
Name string
Id int
}
func TestServer(t *testing.T) {
request := &User{}
server := NewHttpServer()
server.Use(Recovery(), AccessLog())
server.Post("/hello/name", func(ctx *Context) {
ctx.BindJSON(request)
ctx.RespJSON(http.StatusNotFound, request)
})
server.Get("/hello", func(ctx *Context) {
s := ctx.QueryValue("name")
v, err := s.String()
if err != nil {
ctx.RespJSON(http.StatusNotFound, err.Error())
} else {
ctx.RespJSONOK(v)
}
})
server.Get("/hello/name/*", func(ctx *Context) {
ctx.Resp.Write([]byte("hello, star"))
})
server.Get("/hello/:id", func(ctx *Context) {
ctx.RespJSONOK(ctx.PathValue("id").val)
})
server.Start(":8080")
}