Skip to content

Commit

Permalink
Use GoConvey for HTTP error type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seh committed Sep 28, 2016
1 parent 4f48c58 commit 648f74e
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
})
})
}

0 comments on commit 648f74e

Please sign in to comment.