mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
- Implemented \`applyDefaultServerConfig\` in \`Engine\` to apply \`serverProtocols\` to \`http.Server\`. - Uncommented all calls to \`applyDefaultServerConfig\` in \`serve.go\`. - Refactored \`SetProtocols\` and added internal \`setProtocols\` to ensure user-defined protocols are not overwritten by framework defaults in \`RunTLS\`. - Added exhaustive tests in \`protocols_test.go\` to verify protocol inheritance and persistence.
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
package touka
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestApplyDefaultServerConfig(t *testing.T) {
|
|
engine := New()
|
|
|
|
// 1. 测试默认协议
|
|
srv1 := &http.Server{}
|
|
engine.applyDefaultServerConfig(srv1)
|
|
|
|
if srv1.Protocols == nil {
|
|
t.Fatal("srv1.Protocols should not be nil after applyDefaultServerConfig")
|
|
}
|
|
|
|
// 默认配置是 Http1: true, Http2: false, Http2_Cleartext: false
|
|
if !srv1.Protocols.HTTP1() {
|
|
t.Error("Expected HTTP/1 to be enabled by default")
|
|
}
|
|
if srv1.Protocols.HTTP2() {
|
|
t.Error("Expected HTTP/2 to be disabled by default")
|
|
}
|
|
|
|
// 2. 测试自定义协议
|
|
engine.SetProtocols(&ProtocolsConfig{
|
|
Http1: true,
|
|
Http2: true,
|
|
Http2_Cleartext: true,
|
|
})
|
|
|
|
srv2 := &http.Server{}
|
|
engine.applyDefaultServerConfig(srv2)
|
|
|
|
if srv2.Protocols == nil {
|
|
t.Fatal("srv2.Protocols should not be nil after applyDefaultServerConfig")
|
|
}
|
|
|
|
if !srv2.Protocols.HTTP1() {
|
|
t.Error("Expected HTTP/1 to be enabled after SetProtocols")
|
|
}
|
|
if !srv2.Protocols.HTTP2() {
|
|
t.Error("Expected HTTP/2 to be enabled after SetProtocols")
|
|
}
|
|
if !srv2.Protocols.UnencryptedHTTP2() {
|
|
t.Error("Expected Unencrypted HTTP/2 to be enabled after SetProtocols")
|
|
}
|
|
|
|
// 3. 再次更改协议并验证
|
|
engine.SetProtocols(&ProtocolsConfig{
|
|
Http1: false,
|
|
Http2: true,
|
|
Http2_Cleartext: false,
|
|
})
|
|
|
|
srv3 := &http.Server{}
|
|
engine.applyDefaultServerConfig(srv3)
|
|
|
|
if srv3.Protocols == nil {
|
|
t.Fatal("srv3.Protocols should not be nil")
|
|
}
|
|
if srv3.Protocols.HTTP1() {
|
|
t.Error("Expected HTTP/1 to be disabled")
|
|
}
|
|
if !srv3.Protocols.HTTP2() {
|
|
t.Error("Expected HTTP/2 to be enabled")
|
|
}
|
|
}
|
|
|
|
func TestRunTLSProtocolInheritance(t *testing.T) {
|
|
engine := New()
|
|
|
|
// 模拟 RunTLS 中的逻辑: 如果使用默认协议, 则启用 HTTP/2
|
|
if engine.useDefaultProtocols {
|
|
engine.setProtocols(&ProtocolsConfig{
|
|
Http1: true,
|
|
Http2: true,
|
|
})
|
|
}
|
|
|
|
srv := &http.Server{TLSConfig: &tls.Config{}}
|
|
engine.applyDefaultServerConfig(srv)
|
|
|
|
if !srv.Protocols.HTTP2() {
|
|
t.Error("RunTLS simulation: Expected HTTP/2 to be enabled for default config")
|
|
}
|
|
|
|
// 模拟用户设置了自定义协议后调用 RunTLS
|
|
engine = New()
|
|
engine.SetProtocols(&ProtocolsConfig{
|
|
Http1: true,
|
|
Http2: false, // 用户明确不想要 HTTP/2
|
|
})
|
|
|
|
if engine.useDefaultProtocols {
|
|
engine.setProtocols(&ProtocolsConfig{
|
|
Http1: true,
|
|
Http2: true,
|
|
})
|
|
}
|
|
|
|
srv2 := &http.Server{TLSConfig: &tls.Config{}}
|
|
engine.applyDefaultServerConfig(srv2)
|
|
|
|
if srv2.Protocols.HTTP2() {
|
|
t.Error("RunTLS simulation: Expected HTTP/2 to be DISABLED if user set custom protocols previously")
|
|
}
|
|
}
|