aboutsummaryrefslogtreecommitdiff
path: root/util_test.go
blob: 5576bd4b7c74c2b564f28e5af40f063a0c089ef4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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: "<!DOCTYPE HTML><html>nope</html>", 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:    "<!DOCTYPE HTML><HTML><H1>403 ERROR</H1>Request blocked.\nGenerated by cloudfront (CloudFront)",
		Headers: http.Header{},
	}
	r.Headers.Set("Content-Type", "text/html")

	msg := describe(r)
	if !strings.Contains(msg, "CloudFront/WAF") {
		t.Errorf("want a CloudFront/WAF hint, got %q", msg)
	}
	if !strings.Contains(msg, "403") {
		t.Errorf("want the HTTP status in the message, got %q", msg)
	}
}

// DA's own errors are JSON and must pass through intact for callers to unmarshal.
func TestDescribePassesThroughAPIJSON(t *testing.T) {
	body := `{"error":"invalid_request","errorDescription":"Invalid or expired form submission"}`
	r := reqrt{Status: 400, Body: body, Headers: http.Header{}}
	r.Headers.Set("Content-Type", "application/json")

	if got := describe(r); got != body {
		t.Errorf("JSON API errors should pass through unchanged:\n got %q\nwant %q", got, body)
	}
}

// APIError must not emit a JSON parse error for a non-JSON (e.g. CDN block) body.
func TestAPIErrorHandlesNonJSON(t *testing.T) {
	e := APIError(&stringErr{"devianter: HTTP 403 non-JSON response — blocked"})
	if e.Reason != "request_failed" {
		t.Errorf("want Reason=request_failed for non-JSON errors, got %q", e.Reason)
	}
	if !strings.Contains(e.Error, "blocked") {
		t.Errorf("want the underlying message preserved, got %q", e.Error)
	}
}

type stringErr struct{ s string }

func (e *stringErr) Error() string { return e.s }