mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-17 15:20:55 +08:00
- Compress finalized Codex OAuth request bodies with zstd on the verified ChatGPT backend path, with size/benefit gating, header-body alignment, and one uncompressed retry on 400/415 rejection. - Negotiate downstream HTTP response compression under server.response-compression (zstd/gzip with q-values, SSE-safe flushing, commit-time Vary merging) and enable negotiated permessage-deflate on Codex and downstream WebSockets. - Advertise Accept-Encoding to Codex upstreams and decode gzip/deflate/br/zstd upstream responses through a shared decoder before parsing or relaying. Closes: #6905
69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func TestResponseCompressionConfig(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
input string
|
|
enabled bool
|
|
minBytes int
|
|
}{
|
|
{name: "defaults", input: "{}", enabled: true, minBytes: 1024},
|
|
{name: "disabled", input: "server:\n response-compression:\n enabled: false\n min-bytes: 64", minBytes: 64},
|
|
{name: "enabled", input: "server:\n response-compression:\n enabled: true\n min-bytes: -1", enabled: true, minBytes: 1024},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var cfg Config
|
|
if errUnmarshal := yaml.Unmarshal([]byte(tc.input), &cfg); errUnmarshal != nil {
|
|
t.Fatal(errUnmarshal)
|
|
}
|
|
if got := cfg.ServerResponseCompression.IsEnabled(); got != tc.enabled {
|
|
t.Fatalf("IsEnabled() = %v, want %v", got, tc.enabled)
|
|
}
|
|
if got := cfg.ServerResponseCompression.EffectiveMinBytes(); got != tc.minBytes {
|
|
t.Fatalf("EffectiveMinBytes() = %d, want %d", got, tc.minBytes)
|
|
}
|
|
encoded, errMarshal := json.Marshal(cfg)
|
|
if errMarshal != nil {
|
|
t.Fatal(errMarshal)
|
|
}
|
|
var roundTrip Config
|
|
if errUnmarshal := json.Unmarshal(encoded, &roundTrip); errUnmarshal != nil {
|
|
t.Fatal(errUnmarshal)
|
|
}
|
|
if roundTrip.ServerResponseCompression.IsEnabled() != tc.enabled || roundTrip.ServerResponseCompression.EffectiveMinBytes() != tc.minBytes {
|
|
t.Fatal("JSON round trip changed compression configuration")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCodexWebsocketCompressionConfig(t *testing.T) {
|
|
var nilConfig *CodexConfig
|
|
if !nilConfig.WebsocketCompressionEnabled() {
|
|
t.Fatal("nil configuration must enable WebSocket compression")
|
|
}
|
|
for _, tc := range []struct {
|
|
input string
|
|
want bool
|
|
}{
|
|
{input: "{}", want: true},
|
|
{input: "codex:\n websocket-compression: true", want: true},
|
|
{input: "codex:\n websocket-compression: false", want: false},
|
|
} {
|
|
var cfg Config
|
|
if errUnmarshal := yaml.Unmarshal([]byte(tc.input), &cfg); errUnmarshal != nil {
|
|
t.Fatal(errUnmarshal)
|
|
}
|
|
if got := cfg.Codex.WebsocketCompressionEnabled(); got != tc.want {
|
|
t.Fatalf("WebsocketCompressionEnabled() = %v, want %v", got, tc.want)
|
|
}
|
|
}
|
|
}
|