@@ -4,31 +4,49 @@ package fargo
4
4
5
5
import (
6
6
"errors"
7
+ "strconv"
7
8
"testing"
9
+
10
+ . "github.com/smartystreets/goconvey/convey"
8
11
)
9
12
10
13
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
+ })
32
44
}
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
+ })
34
52
}
0 commit comments