From 648f74ef5e30b65a22b165440f48407db9bd0b9e Mon Sep 17 00:00:00 2001 From: "Steven E. Harris" Date: Wed, 28 Sep 2016 10:43:53 -0400 Subject: [PATCH] Use GoConvey for HTTP error type tests --- errors_test.go | 62 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/errors_test.go b/errors_test.go index b00af05..3154003 100644 --- a/errors_test.go +++ b/errors_test.go @@ -4,31 +4,49 @@ package fargo import ( "errors" + "strconv" "testing" + + . "github.com/smartystreets/goconvey/convey" ) func TestHTTPResponseStatusCode(t *testing.T) { - tests := []struct { - input error - present bool - code int - }{ - {nil, false, 0}, - {errors.New("other"), false, 0}, - {&unsuccessfulHTTPResponse{404, "missing"}, true, 404}, - } - for _, test := range tests { - code, present := HTTPResponseStatusCode(test.input) - if present { - if !test.present { - t.Errorf("input %v: want absent, got code %d", test.input, code) - continue - } - if code != test.code { - t.Errorf("input %v: want %d, got %d", test.input, test.code, code) - } - } else if test.present { - t.Errorf("input %v: want code %d, got absent", test.input, test.code) + Convey("An nil error should have no HTTP status code", t, func() { + _, present := HTTPResponseStatusCode(nil) + So(present, ShouldBeFalse) + }) + Convey("A foreign error should have no detectable HTTP status code", t, func() { + _, present := HTTPResponseStatusCode(errors.New("other")) + So(present, ShouldBeFalse) + }) + Convey("A fargo error generated from a response from Eureka", t, func() { + verify := func(err *unsuccessfulHTTPResponse) { + Convey("should have the given HTTP status code", func() { + code, present := HTTPResponseStatusCode(err) + So(present, ShouldBeTrue) + So(code, ShouldEqual, err.statusCode) + Convey("should produce a message", func() { + msg := err.Error() + if len(err.messagePrefix) == 0 { + Convey("that lacks a prefx", func() { + So(msg, ShouldNotStartWith, ",") + }) + } else { + Convey("that starts with the given prefix", func() { + So(msg, ShouldStartWith, err.messagePrefix) + }) + } + Convey("that contains the status code in decimal notation", func() { + So(msg, ShouldContainSubstring, strconv.Itoa(err.statusCode)) + }) + }) + }) } - } + Convey("with a message prefix", func() { + verify(&unsuccessfulHTTPResponse{500, "operation failed"}) + }) + Convey("without a message prefix", func() { + verify(&unsuccessfulHTTPResponse{statusCode: 500}) + }) + }) }