package devianter import ( "net/http" "strings" "testing" ) // Regression: request() used to call try(e) and then dereference resp (nil on a // transport error), panicking. From UpdateCSRF's goroutine that panic was // unrecovered and killed the whole process, so the container crash-looped. func TestRequestTransportFailureDoesNotPanic(t *testing.T) { // Port 1 on loopback: nothing listening, so the dial fails fast. r := request("http://127.0.0.1:1/nope") if r.Err == nil { t.Fatal("expected Err to be set on a transport failure") } if r.Status != 0 { t.Fatalf("expected Status 0 on a failed request, got %d", r.Status) } if r.Body != "" { t.Fatalf("expected empty Body on a failed request, got %q", r.Body) } } func TestLooksLikeJSON(t *testing.T) { jsonResp := reqrt{Body: `{"ok":true}`, Headers: http.Header{}} jsonResp.Headers.Set("Content-Type", "application/json; charset=utf-8") if !looksLikeJSON(jsonResp) { t.Error("a JSON body with a JSON content-type should look like JSON") } htmlResp := reqrt{Body: "nope", Headers: http.Header{}} htmlResp.Headers.Set("Content-Type", "text/html") if looksLikeJSON(htmlResp) { t.Error("an HTML error page must never be treated as JSON") } } // A CloudFront block is the exact failure that produced `invalid character '<'`; // it should now be reported in plain language. func TestDescribeDetectsCloudFrontBlock(t *testing.T) { r := reqrt{ Status: 403, Body: "