|
| 1 | +// Copyright 2024 SeatGeek, Inc. |
| 2 | +// |
| 3 | +// Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms. |
| 4 | + |
| 5 | +package server |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestError_Error(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + err *Error |
| 21 | + want string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "with reason", |
| 25 | + err: &Error{Code: 403, Reason: errors.New("forbidden")}, |
| 26 | + want: "internal 403: forbidden", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "without reason", |
| 30 | + err: &Error{Code: 403}, |
| 31 | + want: "internal 403: something happened, perhaps", |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + for _, tt := range tests { |
| 36 | + t.Run(tt.name, func(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + |
| 39 | + assert.Equal(t, tt.want, tt.err.Error()) |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestError_Is(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + target := &Error{Code: 403, Reason: errors.New("forbidden")} |
| 48 | + |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + other error |
| 52 | + want bool |
| 53 | + }{ |
| 54 | + { |
| 55 | + name: "exact same object", |
| 56 | + other: target, |
| 57 | + want: true, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "different object, same values", |
| 61 | + other: &Error{Code: 403, Reason: errors.New("forbidden")}, |
| 62 | + want: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "different code", |
| 66 | + other: &Error{Code: 400, Reason: errors.New("forbidden")}, |
| 67 | + want: false, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "different reason", |
| 71 | + other: &Error{Code: 403, Reason: errors.New("not allowed")}, |
| 72 | + want: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "different type", |
| 76 | + other: errors.New("forbidden"), |
| 77 | + want: false, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "wrapped", |
| 81 | + other: fmt.Errorf("wrapped: %w", target), |
| 82 | + want: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "inner reason", |
| 86 | + other: target.Reason, |
| 87 | + want: false, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tt := range tests { |
| 92 | + t.Run(tt.name, func(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + |
| 95 | + assert.Equal(t, tt.want, errors.Is(target, tt.other)) |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments