Skip to content

Commit 648f74e

Browse files
committed
Use GoConvey for HTTP error type tests
1 parent 4f48c58 commit 648f74e

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

errors_test.go

+40-22
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,49 @@ package fargo
44

55
import (
66
"errors"
7+
"strconv"
78
"testing"
9+
10+
. "github.com/smartystreets/goconvey/convey"
811
)
912

1013
func TestHTTPResponseStatusCode(t *testing.T) {
11-
tests := []struct {
12-
input error
13-
present bool
14-
code int
15-
}{
16-
{nil, false, 0},
17-
{errors.New("other"), false, 0},
18-
{&unsuccessfulHTTPResponse{404, "missing"}, true, 404},
19-
}
20-
for _, test := range tests {
21-
code, present := HTTPResponseStatusCode(test.input)
22-
if present {
23-
if !test.present {
24-
t.Errorf("input %v: want absent, got code %d", test.input, code)
25-
continue
26-
}
27-
if code != test.code {
28-
t.Errorf("input %v: want %d, got %d", test.input, test.code, code)
29-
}
30-
} else if test.present {
31-
t.Errorf("input %v: want code %d, got absent", test.input, test.code)
14+
Convey("An nil error should have no HTTP status code", t, func() {
15+
_, present := HTTPResponseStatusCode(nil)
16+
So(present, ShouldBeFalse)
17+
})
18+
Convey("A foreign error should have no detectable HTTP status code", t, func() {
19+
_, present := HTTPResponseStatusCode(errors.New("other"))
20+
So(present, ShouldBeFalse)
21+
})
22+
Convey("A fargo error generated from a response from Eureka", t, func() {
23+
verify := func(err *unsuccessfulHTTPResponse) {
24+
Convey("should have the given HTTP status code", func() {
25+
code, present := HTTPResponseStatusCode(err)
26+
So(present, ShouldBeTrue)
27+
So(code, ShouldEqual, err.statusCode)
28+
Convey("should produce a message", func() {
29+
msg := err.Error()
30+
if len(err.messagePrefix) == 0 {
31+
Convey("that lacks a prefx", func() {
32+
So(msg, ShouldNotStartWith, ",")
33+
})
34+
} else {
35+
Convey("that starts with the given prefix", func() {
36+
So(msg, ShouldStartWith, err.messagePrefix)
37+
})
38+
}
39+
Convey("that contains the status code in decimal notation", func() {
40+
So(msg, ShouldContainSubstring, strconv.Itoa(err.statusCode))
41+
})
42+
})
43+
})
3244
}
33-
}
45+
Convey("with a message prefix", func() {
46+
verify(&unsuccessfulHTTPResponse{500, "operation failed"})
47+
})
48+
Convey("without a message prefix", func() {
49+
verify(&unsuccessfulHTTPResponse{statusCode: 500})
50+
})
51+
})
3452
}

0 commit comments

Comments
 (0)