Skip to content

Commit 2474057

Browse files
committed
Utils for errors and testing
1 parent 1dc7664 commit 2474057

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

src/api/utils/errors/errors.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package errors
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"net/http"
7+
)
8+
9+
type ApiError interface {
10+
Status() int
11+
Message() string
12+
Error() string
13+
}
14+
15+
type apiError struct {
16+
AStatus int `json:"status"`
17+
AMessage string `json:"message"`
18+
AnError string `json:"error,omitempty"`
19+
}
20+
21+
func (e *apiError) Status() int {
22+
return e.AStatus
23+
}
24+
25+
func (e *apiError) Message() string {
26+
return e.AMessage
27+
}
28+
29+
func (e *apiError) Error() string {
30+
return e.AnError
31+
}
32+
33+
func NewApiError(statusCode int, message string) ApiError {
34+
return &apiError{AStatus: statusCode, AMessage: message}
35+
}
36+
37+
func NewApiErrFromBytes(body []byte) (ApiError, error) {
38+
var result apiError
39+
if err := json.Unmarshal(body, &result); err != nil {
40+
return nil, errors.New("invalid json body")
41+
}
42+
return &result, nil
43+
}
44+
45+
func NewInternalServerError(message string) ApiError {
46+
return &apiError{
47+
AStatus: http.StatusInternalServerError,
48+
AMessage: message,
49+
}
50+
}
51+
52+
func NewNotFoundError(message string) ApiError {
53+
return &apiError{
54+
AStatus: http.StatusNotFound,
55+
AMessage: message,
56+
}
57+
}
58+
func NewBadRequestError(message string) ApiError {
59+
return &apiError{
60+
AStatus: http.StatusBadRequest,
61+
AMessage: message,
62+
}
63+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test_utils
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
func GetMockedContext(request *http.Request, response *httptest.ResponseRecorder) *gin.Context {
11+
c, _ := gin.CreateTestContext(response)
12+
c.Request = request
13+
return c
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test_utils
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestGetMockedContext(t *testing.T) {
12+
request, err := http.NewRequest(http.MethodGet, "http://localhost:123/something", nil)
13+
assert.Nil(t, err)
14+
response := httptest.NewRecorder()
15+
request.Header = http.Header{"X-Mock": {"true"}}
16+
c := GetMockedContext(request, response)
17+
18+
assert.EqualValues(t, http.MethodGet, c.Request.Method)
19+
assert.EqualValues(t, "123", c.Request.URL.Port())
20+
assert.EqualValues(t, "/something", c.Request.URL.Path)
21+
assert.EqualValues(t, "http", c.Request.URL.Scheme)
22+
assert.EqualValues(t, 1, len(c.Request.Header))
23+
assert.EqualValues(t, "true", c.GetHeader("x-mock"))
24+
assert.EqualValues(t, "true", c.GetHeader("X-Mock"))
25+
}

0 commit comments

Comments
 (0)