krz/devianter

clone: git clone https://gitbay.org/krz/devianter.git

main: util_test.go · raw

 1package devianter
 2
 3import (
 4	"net/http"
 5	"strings"
 6	"testing"
 7)
 8
 9// Regression: request() used to call try(e) and then dereference resp (nil on a
10// transport error), panicking. From UpdateCSRF's goroutine that panic was
11// unrecovered and killed the whole process, so the container crash-looped.
12func TestRequestTransportFailureDoesNotPanic(t *testing.T) {
13	// Port 1 on loopback: nothing listening, so the dial fails fast.
14	r := request("http://127.0.0.1:1/nope")
15
16	if r.Err == nil {
17		t.Fatal("expected Err to be set on a transport failure")
18	}
19	if r.Status != 0 {
20		t.Fatalf("expected Status 0 on a failed request, got %d", r.Status)
21	}
22	if r.Body != "" {
23		t.Fatalf("expected empty Body on a failed request, got %q", r.Body)
24	}
25}
26
27func TestLooksLikeJSON(t *testing.T) {
28	jsonResp := reqrt{Body: `{"ok":true}`, Headers: http.Header{}}
29	jsonResp.Headers.Set("Content-Type", "application/json; charset=utf-8")
30	if !looksLikeJSON(jsonResp) {
31		t.Error("a JSON body with a JSON content-type should look like JSON")
32	}
33
34	htmlResp := reqrt{Body: "<!DOCTYPE HTML><html>nope</html>", Headers: http.Header{}}
35	htmlResp.Headers.Set("Content-Type", "text/html")
36	if looksLikeJSON(htmlResp) {
37		t.Error("an HTML error page must never be treated as JSON")
38	}
39}
40
41// A CloudFront block is the exact failure that produced `invalid character '<'`;
42// it should now be reported in plain language.
43func TestDescribeDetectsCloudFrontBlock(t *testing.T) {
44	r := reqrt{
45		Status:  403,
46		Body:    "<!DOCTYPE HTML><HTML><H1>403 ERROR</H1>Request blocked.\nGenerated by cloudfront (CloudFront)",
47		Headers: http.Header{},
48	}
49	r.Headers.Set("Content-Type", "text/html")
50
51	msg := describe(r)
52	if !strings.Contains(msg, "CloudFront/WAF") {
53		t.Errorf("want a CloudFront/WAF hint, got %q", msg)
54	}
55	if !strings.Contains(msg, "403") {
56		t.Errorf("want the HTTP status in the message, got %q", msg)
57	}
58}
59
60// DA's own errors are JSON and must pass through intact for callers to unmarshal.
61func TestDescribePassesThroughAPIJSON(t *testing.T) {
62	body := `{"error":"invalid_request","errorDescription":"Invalid or expired form submission"}`
63	r := reqrt{Status: 400, Body: body, Headers: http.Header{}}
64	r.Headers.Set("Content-Type", "application/json")
65
66	if got := describe(r); got != body {
67		t.Errorf("JSON API errors should pass through unchanged:\n got %q\nwant %q", got, body)
68	}
69}
70
71// APIError must not emit a JSON parse error for a non-JSON (e.g. CDN block) body.
72func TestAPIErrorHandlesNonJSON(t *testing.T) {
73	e := APIError(&stringErr{"devianter: HTTP 403 non-JSON response — blocked"})
74	if e.Reason != "request_failed" {
75		t.Errorf("want Reason=request_failed for non-JSON errors, got %q", e.Reason)
76	}
77	if !strings.Contains(e.Error, "blocked") {
78		t.Errorf("want the underlying message preserved, got %q", e.Error)
79	}
80}
81
82type stringErr struct{ s string }
83
84func (e *stringErr) Error() string { return e.s }