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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
package roll
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)
// -- Test helpers
type CustomError struct {
s string
}
func (e *CustomError) Error() string {
return e.s
}
func setup() func() {
Token = os.Getenv("TOKEN")
Environment = "test"
if Token == "" {
Token = "test"
originalEndpoint := Endpoint
server := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"result": {"uuid": "01234567890123456789012345678901"}}`))
},
))
Endpoint = server.URL
return func() {
Endpoint = originalEndpoint
Token = ""
server.Close()
}
}
// Assume Token was provided and we want integration tests.
return func() {}
}
// -- Tests
func TestErrorClass(t *testing.T) {
errors := map[string]error{
"{508e076d}": fmt.Errorf("Something is broken!"),
"roll.CustomError": &CustomError{"Terrible mistakes were made."},
}
for expected, err := range errors {
if errorClass(err) != expected {
t.Error("Got:", errorClass(err), "Expected:", expected)
}
}
}
func TestCritical(t *testing.T) {
teardown := setup()
defer teardown()
uuid, err := Critical(errors.New("global critical"), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestError(t *testing.T) {
teardown := setup()
defer teardown()
uuid, err := Error(errors.New("global error"), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestWarning(t *testing.T) {
teardown := setup()
defer teardown()
uuid, err := Warning(errors.New("global warning"), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestInfo(t *testing.T) {
teardown := setup()
defer teardown()
uuid, err := Info("global info", map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestDebug(t *testing.T) {
teardown := setup()
defer teardown()
uuid, err := Debug("global debug", map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientCritical(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.Critical(errors.New("new client critical"), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientError(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.Error(errors.New("new client error"), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientWarning(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.Warning(errors.New("new client warning"), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientInfo(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.Info("new client info", map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientDebug(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.Debug("new client debug", map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientCriticalStack(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.CriticalStack(errors.New("new client critical"), getCallers(0), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientErrorStack(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.ErrorStack(errors.New("new client error"), getCallers(0), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
func TestRollbarClientWarningStack(t *testing.T) {
teardown := setup()
defer teardown()
client := New(Token, "test")
uuid, err := client.WarningStack(errors.New("new client warning"), getCallers(0), map[string]string{"extras": "true"})
if err != nil {
t.Error(err)
}
if len(uuid) != 32 {
t.Errorf("expected UUID, got: %#v", uuid)
}
}
|