(t *testing.T)
| 16 | const httpTestAddr = "127.0.0.1:8081" |
| 17 | |
| 18 | func TestHttp(t *testing.T) { |
| 19 | |
| 20 | p := peer.NewGenericPeer("http.Acceptor", "httpserver", httpTestAddr, nil) |
| 21 | |
| 22 | proc.BindProcessorHandler(p, "http", func(raw cellnet.Event) { |
| 23 | |
| 24 | if matcher, ok := raw.Session().(httppeer.RequestMatcher); ok { |
| 25 | switch { |
| 26 | case matcher.Match("GET", "/hello_get"): |
| 27 | |
| 28 | // 默认返回json |
| 29 | raw.Session().Send(&httppeer.MessageRespond{ |
| 30 | Msg: &HttpEchoACK{ |
| 31 | Token: "get", |
| 32 | }, |
| 33 | }) |
| 34 | case matcher.Match("POST", "/hello_post"): |
| 35 | |
| 36 | // 默认返回json |
| 37 | raw.Session().Send(&httppeer.MessageRespond{ |
| 38 | Msg: &HttpEchoACK{ |
| 39 | Token: "post", |
| 40 | }, |
| 41 | }) |
| 42 | |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | }) |
| 47 | |
| 48 | p.Start() |
| 49 | |
| 50 | requestThenValid(t, "GET", "/hello_get", &HttpEchoREQ{ |
| 51 | UserName: "kitty_get", |
| 52 | }, &HttpEchoACK{ |
| 53 | Token: "get", |
| 54 | }) |
| 55 | |
| 56 | requestThenValid(t, "POST", "/hello_post", &HttpEchoREQ{ |
| 57 | UserName: "kitty_post", |
| 58 | }, &HttpEchoACK{ |
| 59 | Token: "post", |
| 60 | }) |
| 61 | |
| 62 | p.Stop() |
| 63 | } |
| 64 | |
| 65 | func requestThenValid(t *testing.T, method, path string, req, expectACK interface{}) { |
| 66 |
nothing calls this directly
no test coverage detected