Something went wrong. Try again.
/x/perimental project monorepo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218package web
import ( "log/slog" "net/http" "net/http/httptest" "net/url" "testing")
func TestNewError(t *testing.T) { tests := []struct { name string wantStatus int response *http.Response wantErr bool checkError func(*testing.T, error) }{ { name: "successful error creation", wantStatus: 200, response: makeResponse(404, "not found"), wantErr: false, checkError: func(t *testing.T, err error) { if err == nil { t.Fatalf("expected *Error, got nil") } if _, ok := err.(*Error); !ok { t.Fatalf("expected *Error, got %T", err) } }, }, { name: "status codes match", wantStatus: 200, response: makeResponse(500, "internal server error"), wantErr: false, checkError: func(t *testing.T, err error) { webErr, ok := err.(*Error) if !ok { t.Fatalf("expected *Error, got %T", err) } if webErr.WantStatus != 200 { t.Errorf("expected WantStatus 200, got %d", webErr.WantStatus) } if webErr.GotStatus != 500 { t.Errorf("expected GotStatus 500, got %d", webErr.GotStatus) } }, }, { name: "captures response body", wantStatus: 200, response: makeResponse(500, "internal server error"), wantErr: false, checkError: func(t *testing.T, err error) { webErr, ok := err.(*Error) if !ok { t.Fatalf("expected *Error, got %T", err) } if webErr.ResponseBody != "internal server error" { t.Errorf("expected ResponseBody 'internal server error', got %q", webErr.ResponseBody) } }, }, { name: "captures URL", wantStatus: 200, response: makeResponse(404, "not found"), wantErr: false, checkError: func(t *testing.T, err error) { webErr, ok := err.(*Error) if !ok { t.Fatalf("expected *Error, got %T", err) } if webErr.URL == nil { t.Fatal("expected URL to be set, got nil") } }, }, { name: "captures method", wantStatus: 200, response: makeResponse(404, "not found"), wantErr: false, checkError: func(t *testing.T, err error) { webErr, ok := err.(*Error) if !ok { t.Fatalf("expected *Error, got %T", err) } if webErr.Method != "GET" { t.Errorf("expected Method 'GET', got %q", webErr.Method) } }, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := NewError(tt.wantStatus, tt.response) if tt.wantErr && err == nil { t.Fatal("expected error, got nil") } if tt.checkError != nil { tt.checkError(t, err) } }) }}
func TestError_Error(t *testing.T) { tests := []struct { name string err *Error want string }{ { name: "basic error message", err: &Error{ WantStatus: 200, GotStatus: 404, URL: mustParseURL("http://example.com/test"), Method: "GET", ResponseBody: "not found", }, want: "GET http://example.com/test: wanted status code 200, got: 404: not found", }, { name: "post request error", err: &Error{ WantStatus: 201, GotStatus: 400, URL: mustParseURL("http://example.com/api"), Method: "POST", ResponseBody: "bad request", }, want: "POST http://example.com/api: wanted status code 201, got: 400: bad request", }, { name: "empty response body", err: &Error{ WantStatus: 200, GotStatus: 500, URL: mustParseURL("http://example.com/"), Method: "DELETE", ResponseBody: "", }, want: "DELETE http://example.com/: wanted status code 200, got: 500: ", }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.err.Error() if got != tt.want { t.Errorf("Error.Error() = %q, want %q", got, tt.want) } }) }}
func TestError_LogValue(t *testing.T) { tests := []struct { name string err *Error check func(*testing.T, slog.Value) }{ { name: "contains all fields", err: &Error{ WantStatus: 200, GotStatus: 404, URL: mustParseURL("http://example.com/test"), Method: "GET", ResponseBody: "not found", }, check: func(t *testing.T, v slog.Value) { // LogValue returns a slog.GroupValue, which we can't directly inspect // but we can verify it doesn't panic and returns a group kind if v.Kind() != slog.KindGroup { t.Errorf("expected KindGroup, got %v", v.Kind()) } }, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.err.LogValue() if tt.check != nil { tt.check(t, got) } }) }}
func makeResponse(status int, body string) *http.Response { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(status) w.Write([]byte(body)) })) defer ts.Close()
resp, err := http.Get(ts.URL) if err != nil { panic(err) } return resp}
func mustParseURL(s string) *url.URL { u, err := url.Parse(s) if err != nil { panic(err) } return u}