MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / TestHTTPClient_HTTPErrors

Function TestHTTPClient_HTTPErrors

pkg/api/http_test.go:276–304  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

274}
275
276func TestHTTPClient_HTTPErrors(t *testing.T) {
277 tests := []struct {
278 name string
279 statusCode int
280 expectedError string
281 }{
282 {"bad request", http.StatusBadRequest, "API request failed with status 400"},
283 {"not found", http.StatusNotFound, "API request failed with status 404"},
284 {"internal server error", http.StatusInternalServerError, "API request failed with status 500"},
285 }
286
287 for _, tt := range tests {
288 t.Run(tt.name, func(t *testing.T) {
289 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
290 w.WriteHeader(tt.statusCode)
291 _, _ = w.Write([]byte("Error response"))
292 }))
293 defer server.Close()
294
295 client := NewHTTPClient(server.Client(), server.URL, testutils.NewTestLogger())
296
297 var result map[string]interface{}
298 err := client.Get(context.Background(), "/test", &result)
299
300 require.Error(t, err)
301 assert.Contains(t, err.Error(), tt.expectedError)
302 })
303 }
304}
305
306func TestHTTPClient_UnauthorizedWithAPIToken(t *testing.T) {
307 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 7

GetMethod · 0.95
NewTestLoggerFunction · 0.92
NewHTTPClientFunction · 0.85
RunMethod · 0.80
ClientMethod · 0.80
CloseMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected