| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestOpenCodeGoHeadersScopedStableAndPrivate(t *testing.T) { |
| 11 | for _, endpoint := range []string{"https://opencode.ai/zen/go/v1/messages", "https://opencode.ai/zen/go/v1/chat/completions", "https://opencode.ai/zen/go/v1/responses"} { |
| 12 | ctx := WithCacheSession(context.Background(), "/private/path/to/session.jsonl") |
| 13 | var previous string |
| 14 | for i := range 3 { |
| 15 | req, _ := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, nil) |
| 16 | ApplyOpenCodeGoHeaders(req, "https://custom-base.example", NewClientIdentityHeaders()) |
| 17 | id := req.Header.Get("x-opencode-session") |
| 18 | if id == "" || strings.Contains(id, "private") || i > 0 && id != previous { |
| 19 | t.Fatalf("unstable/private session ID: %q", id) |
| 20 | } |
| 21 | if req.Header.Get("User-Agent") != "Reasonix" { |
| 22 | t.Fatal("missing client identity") |
| 23 | } |
| 24 | previous = id |
| 25 | } |
| 26 | child, _ := http.NewRequestWithContext(WithCacheSession(ctx, "child"), http.MethodPost, endpoint, nil) |
| 27 | ApplyOpenCodeGoHeaders(child, endpoint, NewClientIdentityHeaders()) |
| 28 | if child.Header.Get("x-opencode-session") == "" || child.Header.Get("x-opencode-session") == previous { |
| 29 | t.Fatal("child retained parent identity") |
| 30 | } |
| 31 | } |
| 32 | for _, base := range []string{"https://opencode.ai/zen/v1", "https://opencode.ai.evil/zen/go/v1", "http://opencode.ai/zen/go/v1", "https://opencode.ai/zen/go/v1?x=1", "https://api.deepseek.com", "https://opencode.ai:8443/zen/go"} { |
| 33 | req, _ := http.NewRequest(http.MethodPost, base, nil) |
| 34 | ApplyOpenCodeGoHeaders(req, base, NewClientIdentityHeaders()) |
| 35 | if len(req.Header) != 0 { |
| 36 | t.Fatalf("headers leaked to %s", base) |
| 37 | } |
| 38 | } |
| 39 | req, _ := http.NewRequest(http.MethodPost, "https://opencode.ai/zen/go/v1/responses", nil) |
| 40 | req.Header.Set("User-Agent", "Reasonix/custom") |
| 41 | req.Header.Set("x-opencode-session", "explicit-session") |
| 42 | ApplyOpenCodeGoHeaders(req, "https://opencode.ai/zen/go/v1", NewClientIdentityHeaders()) |
| 43 | if req.Header.Get("User-Agent") != "Reasonix/custom" || req.Header.Get("x-opencode-session") != "explicit-session" { |
| 44 | t.Fatal("overwrote explicit client header") |
| 45 | } |
| 46 | } |
| 47 |