|
| 1 | +// Package assert implements common assertions used in go-httbin's unit tests. |
| 2 | +package assert |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/mccutchen/websocket/internal/testing/must" |
| 13 | +) |
| 14 | + |
| 15 | +// Equal asserts that two values are equal. |
| 16 | +func Equal[T comparable](t *testing.T, got, want T, msg string, arg ...any) { |
| 17 | + t.Helper() |
| 18 | + if got != want { |
| 19 | + if msg == "" { |
| 20 | + msg = "expected values to match" |
| 21 | + } |
| 22 | + msg = fmt.Sprintf(msg, arg...) |
| 23 | + t.Fatalf("%s:\nwant: %#v\n got: %#v", msg, want, got) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// DeepEqual asserts that two values are deeply equal. |
| 28 | +func DeepEqual[T any](t *testing.T, got, want T, msg string, arg ...any) { |
| 29 | + t.Helper() |
| 30 | + if !reflect.DeepEqual(got, want) { |
| 31 | + if msg == "" { |
| 32 | + msg = "expected values to match" |
| 33 | + } |
| 34 | + msg = fmt.Sprintf(msg, arg...) |
| 35 | + t.Fatalf("%s:\nwant: %#v\n got: %#v", msg, want, got) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// NilError asserts that an error is nil. |
| 40 | +func NilError(t *testing.T, err error) { |
| 41 | + t.Helper() |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("expected nil error, got %s (%T)", err, err) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Error asserts that an error is not nil. |
| 48 | +func Error(t *testing.T, got, expected error) { |
| 49 | + t.Helper() |
| 50 | + if got != expected { |
| 51 | + if got != nil && expected != nil { |
| 52 | + if got.Error() == expected.Error() { |
| 53 | + return |
| 54 | + } |
| 55 | + } |
| 56 | + t.Fatalf("expected error %v, got %v", expected, got) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// StatusCode asserts that a response has a specific status code. |
| 61 | +func StatusCode(t *testing.T, resp *http.Response, code int) { |
| 62 | + t.Helper() |
| 63 | + if resp.StatusCode != code { |
| 64 | + t.Fatalf("expected status code %d, got %d", code, resp.StatusCode) |
| 65 | + } |
| 66 | + if resp.StatusCode >= 400 { |
| 67 | + // Ensure our error responses are never served as HTML, so that we do |
| 68 | + // not need to worry about XSS or other attacks in error responses. |
| 69 | + if ct := resp.Header.Get("Content-Type"); !isSafeContentType(ct) { |
| 70 | + t.Errorf("HTTP %s error served with dangerous content type: %s", resp.Status, ct) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func isSafeContentType(ct string) bool { |
| 76 | + return strings.HasPrefix(ct, "application/json") || strings.HasPrefix(ct, "text/plain") || strings.HasPrefix(ct, "application/octet-stream") |
| 77 | +} |
| 78 | + |
| 79 | +// Header asserts that a header key has a specific value in a response. |
| 80 | +func Header(t *testing.T, resp *http.Response, key, want string) { |
| 81 | + t.Helper() |
| 82 | + got := resp.Header.Get(key) |
| 83 | + if want != got { |
| 84 | + t.Fatalf("expected header %s=%#v, got %#v", key, want, got) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// ContentType asserts that a response has a specific Content-Type header |
| 89 | +// value. |
| 90 | +func ContentType(t *testing.T, resp *http.Response, contentType string) { |
| 91 | + t.Helper() |
| 92 | + Header(t, resp, "Content-Type", contentType) |
| 93 | +} |
| 94 | + |
| 95 | +// Contains asserts that needle is found in the given string. |
| 96 | +func Contains(t *testing.T, s string, needle string, description string) { |
| 97 | + t.Helper() |
| 98 | + if !strings.Contains(s, needle) { |
| 99 | + t.Fatalf("expected string %q in %s %q", needle, description, s) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// BodyContains asserts that a response body contains a specific substring. |
| 104 | +func BodyContains(t *testing.T, resp *http.Response, needle string) { |
| 105 | + t.Helper() |
| 106 | + body := must.ReadAll(t, resp.Body) |
| 107 | + Contains(t, body, needle, "body") |
| 108 | +} |
| 109 | + |
| 110 | +// BodyEquals asserts that a response body is equal to a specific string. |
| 111 | +func BodyEquals(t *testing.T, resp *http.Response, want string) { |
| 112 | + t.Helper() |
| 113 | + got := must.ReadAll(t, resp.Body) |
| 114 | + Equal(t, got, want, "incorrect response body") |
| 115 | +} |
| 116 | + |
| 117 | +// BodySize asserts that a response body is a specific size. |
| 118 | +func BodySize(t *testing.T, resp *http.Response, want int) { |
| 119 | + t.Helper() |
| 120 | + got := must.ReadAll(t, resp.Body) |
| 121 | + Equal(t, len(got), want, "incorrect response body size") |
| 122 | +} |
| 123 | + |
| 124 | +// DurationRange asserts that a duration is within a specific range. |
| 125 | +func DurationRange(t *testing.T, got, minVal, maxVal time.Duration) { |
| 126 | + t.Helper() |
| 127 | + if got < minVal || got > maxVal { |
| 128 | + t.Fatalf("expected duration between %s and %s, got %s", minVal, maxVal, got) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +type number interface { |
| 133 | + ~int64 | ~float64 |
| 134 | +} |
| 135 | + |
| 136 | +// RoughlyEqual asserts that a numeric value is within a certain tolerance. |
| 137 | +func RoughlyEqual[T number](t *testing.T, got, want T, epsilon T) { |
| 138 | + t.Helper() |
| 139 | + if got < want-epsilon || got > want+epsilon { |
| 140 | + t.Fatalf("expected value between %v and %v, got %v", want-epsilon, want+epsilon, got) |
| 141 | + } |
| 142 | +} |
0 commit comments