add test for HandleHeader (#1467)

pull/1473/head
letian 3 years ago committed by GitHub
parent b92e8a9da8
commit 754d83ea9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      transport/http/server_test.go

@ -33,6 +33,9 @@ func TestServer(t *testing.T) {
srv := NewServer() srv := NewServer()
srv.HandleFunc("/index", fn) srv.HandleFunc("/index", fn)
srv.HandleFunc("/index/{id:[0-9]+}", fn) srv.HandleFunc("/index/{id:[0-9]+}", fn)
srv.HandleHeader("content-type", "application/grpc-web+json", func(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(testData{Path: r.RequestURI})
})
if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") { if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
t.Fatal(e, err) t.Fatal(e, err)
@ -44,10 +47,25 @@ func TestServer(t *testing.T) {
} }
}() }()
time.Sleep(time.Second) time.Sleep(time.Second)
testHeader(t, srv)
testClient(t, srv) testClient(t, srv)
assert.NoError(t, srv.Stop(ctx)) assert.NoError(t, srv.Stop(ctx))
} }
func testHeader(t *testing.T, srv *Server) {
e, err := srv.Endpoint()
assert.NoError(t, err)
client, err := NewClient(context.Background(), WithEndpoint(e.Host))
assert.NoError(t, err)
reqURL := fmt.Sprintf(e.String() + "/index")
req, err := http.NewRequest("GET", reqURL, nil)
assert.NoError(t, err)
req.Header.Set("content-type", "application/grpc-web+json")
resp, err := client.Do(req)
assert.NoError(t, err)
resp.Body.Close()
}
func testClient(t *testing.T, srv *Server) { func testClient(t *testing.T, srv *Server) {
tests := []struct { tests := []struct {
method string method string

Loading…
Cancel
Save