-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_test.go
75 lines (60 loc) · 1.83 KB
/
error_test.go
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
package openapirouter
import (
"github.com/stretchr/testify/assert"
"net/http"
"reflect"
"testing"
)
func TestErrorMapper_ShouldMapHttpErrorToResponseByDefault(t *testing.T) {
//given
mapper := errorMapper{errorMapping: make(map[reflect.Type]*HTTPError)}
err := NewHTTPError(http.StatusNotFound)
//when
result := mapper.mapError(err)
//then
assert.Equal(t, http.StatusNotFound, result.StatusCode)
assert.Equal(t, *NewHTTPError(http.StatusNotFound), result.Body)
}
type ExampleError struct {
}
func (e *ExampleError) Error() string {
return "test error"
}
func TestErrorMapper_ShouldMapKnownErrorToResponse(t *testing.T) {
//given
mapper := errorMapper{
errorMapping: map[reflect.Type]*HTTPError{
reflect.TypeOf(&ExampleError{}): NewHTTPError(http.StatusBadGateway),
},
}
err := &ExampleError{}
//when
result := mapper.mapError(err)
//then
assert.Equal(t, http.StatusBadGateway, result.StatusCode)
assert.Equal(t, *NewHTTPError(http.StatusBadGateway), result.Body)
}
func TestErrorMapper_ShouldMapKnownErrorToResponseWithDetails(t *testing.T) {
//given
mapper := errorMapper{
errorMapping: map[reflect.Type]*HTTPError{
reflect.TypeOf(&ExampleError{}): NewHTTPError(http.StatusBadGateway, "detail1", "detail2"),
},
}
err := &ExampleError{}
//when
result := mapper.mapError(err)
//then
assert.Equal(t, http.StatusBadGateway, result.StatusCode)
assert.Equal(t, *NewHTTPError(http.StatusBadGateway, "detail1", "detail2"), result.Body)
}
func TestErrorMapper_ShouldMapKnownErrorToInternalServerError(t *testing.T) {
//given
mapper := errorMapper{errorMapping: make(map[reflect.Type]*HTTPError)}
err := &ExampleError{}
//when
result := mapper.mapError(err)
//then
assert.Equal(t, http.StatusInternalServerError, result.StatusCode)
assert.Equal(t, *NewHTTPError(http.StatusInternalServerError), result.Body)
}