-
Notifications
You must be signed in to change notification settings - Fork 1
/
ensure.go
57 lines (49 loc) · 1.64 KB
/
ensure.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
package testutil
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"app/api/model"
"github.com/stretchr/testify/assert"
)
// EnsureOK tests for HTTP status 200.
func EnsureOK(t *testing.T, w *httptest.ResponseRecorder) *model.OKResponse {
r := new(model.OKResponse)
err := json.Unmarshal(w.Body.Bytes(), &r.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, w.Code)
return r
}
// EnsureCreated tests for http status 201.
func EnsureCreated(t *testing.T, w *httptest.ResponseRecorder) *model.CreatedResponse {
r := new(model.CreatedResponse)
err := json.Unmarshal(w.Body.Bytes(), &r.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusCreated, w.Code)
return r
}
// EnsureBadRequest tests for HTTP status 400.
func EnsureBadRequest(t *testing.T, w *httptest.ResponseRecorder) *model.BadRequestResponse {
r := new(model.BadRequestResponse)
err := json.Unmarshal(w.Body.Bytes(), &r.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusBadRequest, w.Code)
return r
}
// EnsureUnauthorized tests for HTTP status 401.
func EnsureUnauthorized(t *testing.T, w *httptest.ResponseRecorder) *model.UnauthorizedResponse {
r := new(model.UnauthorizedResponse)
err := json.Unmarshal(w.Body.Bytes(), &r.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusUnauthorized, w.Code)
return r
}
// EnsureInternalServerError tests for http status 500.
func EnsureInternalServerError(t *testing.T, w *httptest.ResponseRecorder) *model.InternalServerErrorResponse {
r := new(model.InternalServerErrorResponse)
err := json.Unmarshal(w.Body.Bytes(), &r.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusInternalServerError, w.Code)
return r
}