Something went wrong. Try again.
An ATProto Powered Reputation Network
Something went wrong. Try again.
2.1 kB · 55 lines
Go
at master
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556package ui
import ( "context" "encoding/json" "errors" "html/template" "net/http" "net/http/httptest" "strings" "testing"
"github.com/bluesky-social/indigo/atproto/atclient" "github.com/bluesky-social/indigo/atproto/identity")
func TestErrorReporting(t *testing.T) { const secret = "private-token-that-must-not-appear" h := &HTTP{page: template.Must(template.New("page").Parse(page))} for _, tt := range []struct { path string err error status int code string }{ {"/api/vouch", &atclient.APIError{StatusCode: http.StatusInternalServerError, Message: secret}, http.StatusBadGateway, "pds_write_failed"}, {"/api/vouch", &atclient.APIError{StatusCode: http.StatusForbidden, Message: secret}, http.StatusUnauthorized, "authorization_failed"}, {"/api/accounts", identity.ErrHandleNotFound, http.StatusUnprocessableEntity, "account_not_found"}, {"/api/accounts", errors.New(secret), http.StatusBadGateway, "search_unavailable"}, {"/api/reputation", context.DeadlineExceeded, http.StatusGatewayTimeout, "timeout"}, {"/api/vouch", invalid("Choose a trust level.", errors.New(secret)), http.StatusBadRequest, "invalid_input"}, } { w := httptest.NewRecorder() r := httptest.NewRequest(http.MethodPost, tt.path, nil) h.fail(w, r, tt.err) var body struct { Error, Code string RequestID string `json:"request_id"` } if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { t.Fatal(err) } if w.Code != tt.status || body.Code != tt.code || body.Error == "" || body.RequestID == "" || body.RequestID != w.Header().Get("X-Request-ID") { t.Fatalf("error response: %d %s", w.Code, w.Body) } if strings.Contains(w.Body.String(), secret) { t.Fatal("response exposed a remote error body") } } w := httptest.NewRecorder() h.fail(w, httptest.NewRequest(http.MethodPost, "/oauth/login", nil), errors.New(secret)) if w.Code != http.StatusBadGateway || !strings.Contains(w.Header().Get("Content-Type"), "text/html") || !strings.Contains(w.Body.String(), "Reference:") || strings.Contains(w.Body.String(), secret) { t.Fatalf("login error page: %d %s", w.Code, w.Body) }}