Download internal/panel/config_env_test.go from a3216/gcli2api: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/panel/config_env_test.go
- Command line
-
hf download hf://spaces/a3216/gcli2api/internal/panel/config_env_test.go
-
curl -L -o config_env_test.go https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/panel/config_env_test.go
1.18 kB
| package panel | |
| import ( | |
| "encoding/json" | |
| "net/http" | |
| "net/http/httptest" | |
| "strings" | |
| "testing" | |
| ) | |
| // TestConfigAPIReportsEnvironmentManagedKey 配置读写接口都应报告「API key 由启动 | |
| // 环境变量托管」,供前端禁用输入框 + 不把无效值写进本地会话密钥。移植上游 PR #104。 | |
| func TestConfigAPIReportsEnvironmentManagedKey(t *testing.T) { | |
| t.Setenv("WB2A_API_KEY", "environment-key") | |
| p := New(Config{APIKey: "environment-key", LoadConfig: func() (any, error) { return map[string]any{"api_key": "environment-key"}, nil }, SaveConfig: func([]byte) ([]string, error) { return nil, nil }}) | |
| for _, method := range []string{http.MethodGet, http.MethodPost} { | |
| req := httptest.NewRequest(method, "/panel/api/config", strings.NewReader(`{"api_key":"other"}`)) | |
| req.Header.Set("Authorization", "Bearer environment-key") | |
| rec := httptest.NewRecorder() | |
| p.ServeHTTP(rec, req) | |
| var response map[string]any | |
| if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil { | |
| t.Fatal(err) | |
| } | |
| if rec.Code != 200 || response["api_key_env_managed"] != true { | |
| t.Fatalf("method=%s status=%d response=%v", method, rec.Code, response) | |
| } | |
| } | |
| } | |