|
1 | 1 | package v1 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
4 | 8 | "testing" |
5 | 9 | ) |
6 | 10 |
|
@@ -74,6 +78,107 @@ func TestCheckDeploymentID(t *testing.T) { |
74 | 78 | } |
75 | 79 | } |
76 | 80 |
|
| 81 | +func TestContextWithDynamicAPIKey(t *testing.T) { |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + apiKey string |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "non-empty key", |
| 88 | + apiKey: "my-secret-key", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "empty key", |
| 92 | + apiKey: "", |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + ctx := ContextWithDynamicAPIKey(context.Background(), tt.apiKey) |
| 99 | + got, ok := ctx.Value(apiKeyContextKey).(string) |
| 100 | + if !ok { |
| 101 | + t.Fatal("ContextWithDynamicAPIKey() did not store a string value in context") |
| 102 | + } |
| 103 | + if got != tt.apiKey { |
| 104 | + t.Errorf("ContextWithDynamicAPIKey() stored %q, want %q", got, tt.apiKey) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestRequestAPIDynamicAPIKey(t *testing.T) { |
| 111 | + tests := []struct { |
| 112 | + name string |
| 113 | + clientKey string |
| 114 | + ctxKey *string // nil means don't set context key |
| 115 | + expectedHeader string |
| 116 | + }{ |
| 117 | + { |
| 118 | + name: "static key, no ctx", |
| 119 | + clientKey: "static-key", |
| 120 | + ctxKey: nil, |
| 121 | + expectedHeader: "static-key", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "static key + ctx key, client takes precedence", |
| 125 | + clientKey: "static-key", |
| 126 | + ctxKey: strPtr("dynamic-key"), |
| 127 | + expectedHeader: "static-key", |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "dynamic + ctx key", |
| 131 | + clientKey: DynamicAPIKey, |
| 132 | + ctxKey: strPtr("per-request-key"), |
| 133 | + expectedHeader: "per-request-key", |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "dynamic, no ctx key", |
| 137 | + clientKey: DynamicAPIKey, |
| 138 | + ctxKey: nil, |
| 139 | + expectedHeader: "", |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "dynamic + empty ctx key", |
| 143 | + clientKey: DynamicAPIKey, |
| 144 | + ctxKey: strPtr(""), |
| 145 | + expectedHeader: "", |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + for _, tt := range tests { |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + var capturedHeader string |
| 152 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 153 | + capturedHeader = r.Header.Get(AccessTokenHeader) |
| 154 | + w.WriteHeader(http.StatusOK) |
| 155 | + _ = json.NewEncoder(w).Encode([]CloudProviderInfo{}) |
| 156 | + })) |
| 157 | + defer server.Close() |
| 158 | + |
| 159 | + client, err := New(tt.clientKey, WithBaseURL(server.URL)) |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("New() error = %v", err) |
| 162 | + } |
| 163 | + |
| 164 | + ctx := context.Background() |
| 165 | + if tt.ctxKey != nil { |
| 166 | + ctx = ContextWithDynamicAPIKey(ctx, *tt.ctxKey) |
| 167 | + } |
| 168 | + |
| 169 | + _, _ = client.ListCloudProviders(ctx) |
| 170 | + |
| 171 | + if capturedHeader != tt.expectedHeader { |
| 172 | + t.Errorf("request header %q = %q, want %q", AccessTokenHeader, capturedHeader, tt.expectedHeader) |
| 173 | + } |
| 174 | + }) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func strPtr(s string) *string { |
| 179 | + return &s |
| 180 | +} |
| 181 | + |
77 | 182 | func TestIsValidTenantID(t *testing.T) { |
78 | 183 | tests := []struct { |
79 | 184 | name string |
|
0 commit comments