-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
41 lines (33 loc) · 1.17 KB
/
api_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
package main
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWarpResponse_WithNoError(t *testing.T) {
recorder := httptest.NewRecorder()
data := "test data"
warpResponse(recorder, http.StatusOK, data, nil)
assert.Equal(t, http.StatusOK, recorder.Code)
assert.Contains(t, recorder.Body.String(), `"data":"test data"`)
assert.NotContains(t, recorder.Body.String(), "error")
}
func TestWarpResponse_WithError(t *testing.T) {
recorder := httptest.NewRecorder()
err := errors.New("test error")
warpResponse(recorder, http.StatusInternalServerError, nil, err)
assert.Equal(t, http.StatusInternalServerError, recorder.Code)
assert.Contains(t, recorder.Body.String(), `"error":"test error"`)
assert.NotContains(t, recorder.Body.String(), "data")
}
func TestWarpResponse_WithDataAndError(t *testing.T) {
recorder := httptest.NewRecorder()
data := "test data"
err := errors.New("test error")
warpResponse(recorder, http.StatusOK, data, err)
assert.Equal(t, http.StatusOK, recorder.Code)
assert.Contains(t, recorder.Body.String(), `"data":"test data"`)
assert.Contains(t, recorder.Body.String(), `"error":"test error"`)
}