mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-04 17:58:14 +08:00
Merge 47a03c7cf23e41bb45193ab400ad01c89b1c89a2 into d3ffc9985281dcf4d3bef604cce4e662b1a327a6
This commit is contained in:
commit
16c0888678
8
gin.go
8
gin.go
@ -169,6 +169,11 @@ type Engine struct {
|
|||||||
// UseH2C enable h2c support.
|
// UseH2C enable h2c support.
|
||||||
UseH2C bool
|
UseH2C bool
|
||||||
|
|
||||||
|
// H2CConfig optionally specifies the http2.Server configuration used for h2c connections.
|
||||||
|
// If nil, a default &http2.Server{} is used. Set this to configure options such as
|
||||||
|
// MaxConcurrentStreams, IdleTimeout, etc.
|
||||||
|
H2CConfig *http2.Server
|
||||||
|
|
||||||
// ContextWithFallback enable fallback Context.Deadline(), Context.Done(), Context.Err() and Context.Value() when Context.Request.Context() is not nil.
|
// ContextWithFallback enable fallback Context.Deadline(), Context.Done(), Context.Err() and Context.Value() when Context.Request.Context() is not nil.
|
||||||
ContextWithFallback bool
|
ContextWithFallback bool
|
||||||
|
|
||||||
@ -246,6 +251,9 @@ func (engine *Engine) Handler() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
h2s := &http2.Server{}
|
h2s := &http2.Server{}
|
||||||
|
if engine.H2CConfig != nil {
|
||||||
|
h2s = engine.H2CConfig
|
||||||
|
}
|
||||||
return h2c.NewHandler(engine, h2s)
|
return h2c.NewHandler(engine, h2s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
49
gin_test.go
49
gin_test.go
@ -120,6 +120,55 @@ func TestH2c(t *testing.T) {
|
|||||||
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestH2CHandlerDefaultConfig(t *testing.T) {
|
||||||
|
r := Default()
|
||||||
|
r.UseH2C = true
|
||||||
|
handler := r.Handler()
|
||||||
|
// When UseH2C is true and H2CConfig is nil, Handler() should return an h2c handler (not the engine itself).
|
||||||
|
assert.NotEqual(t, r, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestH2CHandlerWithCustomConfig(t *testing.T) {
|
||||||
|
ln, err := net.Listen("tcp", localhostIP+":0")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
r := Default()
|
||||||
|
r.UseH2C = true
|
||||||
|
r.H2CConfig = &http2.Server{
|
||||||
|
IdleTimeout: 60 * time.Second,
|
||||||
|
}
|
||||||
|
r.GET("/", func(c *Context) {
|
||||||
|
c.String(200, "<h1>Hello world</h1>")
|
||||||
|
})
|
||||||
|
go func() {
|
||||||
|
err := http.Serve(ln, r.Handler())
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
defer ln.Close()
|
||||||
|
|
||||||
|
url := "http://" + ln.Addr().String() + "/"
|
||||||
|
|
||||||
|
httpClient := http.Client{
|
||||||
|
Transport: &http2.Transport{
|
||||||
|
AllowHTTP: true,
|
||||||
|
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
|
||||||
|
return net.Dial(netw, addr)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := httpClient.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := io.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadHTMLGlobTestMode(t *testing.T) {
|
func TestLoadHTMLGlobTestMode(t *testing.T) {
|
||||||
ts := setupHTMLFiles(
|
ts := setupHTMLFiles(
|
||||||
t,
|
t,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user