forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpapi_test.go
More file actions
143 lines (122 loc) · 3.54 KB
/
httpapi_test.go
File metadata and controls
143 lines (122 loc) · 3.54 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package httpapi_test
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/codersdk"
)
func TestInternalServerError(t *testing.T) {
t.Parallel()
t.Run("NoError", func(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
httpapi.InternalServerError(w, nil)
var resp codersdk.Response
err := json.NewDecoder(w.Body).Decode(&resp)
require.NoError(t, err)
require.Equal(t, http.StatusInternalServerError, w.Code)
require.NotEmpty(t, resp.Message)
require.Empty(t, resp.Detail)
})
t.Run("WithError", func(t *testing.T) {
t.Parallel()
var (
w = httptest.NewRecorder()
httpErr = xerrors.New("error!")
)
httpapi.InternalServerError(w, httpErr)
var resp codersdk.Response
err := json.NewDecoder(w.Body).Decode(&resp)
require.NoError(t, err)
require.Equal(t, http.StatusInternalServerError, w.Code)
require.NotEmpty(t, resp.Message)
require.Equal(t, httpErr.Error(), resp.Detail)
})
}
func TestWrite(t *testing.T) {
t.Parallel()
t.Run("NoErrors", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
rw := httptest.NewRecorder()
httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
Message: "Wow.",
})
var m map[string]interface{}
err := json.NewDecoder(rw.Body).Decode(&m)
require.NoError(t, err)
_, ok := m["errors"]
require.False(t, ok)
})
}
func TestRead(t *testing.T) {
t.Parallel()
t.Run("EmptyStruct", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
rw := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", bytes.NewBufferString("{}"))
v := struct{}{}
require.True(t, httpapi.Read(ctx, rw, r, &v))
})
t.Run("NoBody", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
rw := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", nil)
var v json.RawMessage
require.False(t, httpapi.Read(ctx, rw, r, v))
})
t.Run("Validate", func(t *testing.T) {
t.Parallel()
type toValidate struct {
Value string `json:"value" validate:"required"`
}
ctx := context.Background()
rw := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", bytes.NewBufferString(`{"value":"hi"}`))
var validate toValidate
require.True(t, httpapi.Read(ctx, rw, r, &validate))
require.Equal(t, "hi", validate.Value)
})
t.Run("ValidateFailure", func(t *testing.T) {
t.Parallel()
type toValidate struct {
Value string `json:"value" validate:"required"`
}
ctx := context.Background()
rw := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", bytes.NewBufferString("{}"))
var validate toValidate
require.False(t, httpapi.Read(ctx, rw, r, &validate))
var v codersdk.Response
err := json.NewDecoder(rw.Body).Decode(&v)
require.NoError(t, err)
require.Len(t, v.Validations, 1)
require.Equal(t, "value", v.Validations[0].Field)
require.Equal(t, "Validation failed for tag \"required\" with value: \"\"", v.Validations[0].Detail)
})
}
func WebsocketCloseMsg(t *testing.T) {
t.Parallel()
t.Run("TruncateSingleByteCharacters", func(t *testing.T) {
t.Parallel()
msg := strings.Repeat("d", 255)
trunc := httpapi.WebsocketCloseSprintf(msg)
assert.LessOrEqual(t, len(trunc), 123)
})
t.Run("TruncateMultiByteCharacters", func(t *testing.T) {
t.Parallel()
msg := strings.Repeat("こんにちは", 10)
trunc := httpapi.WebsocketCloseSprintf(msg)
assert.LessOrEqual(t, len(trunc), 123)
})
}