forked from apex/rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
37 lines (31 loc) · 1.13 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
package rpc_test
import (
"errors"
"net/http/httptest"
"strings"
"testing"
"github.com/tj/assert"
"github.com/apex/rpc"
)
// Test error reporting.
func TestWriteError(t *testing.T) {
t.Run("with a regular error", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteError(w, errors.New("boom"))
assert.Equal(t, 500, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, "{\n \"type\": \"internal\",\n \"message\": \"boom\"\n}", strings.TrimSpace(w.Body.String()))
})
t.Run("with a TypeProvider", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteError(w, rpc.Error(400, "invalid_slug", "Invalid team slug"))
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, "{\n \"type\": \"invalid_slug\",\n \"message\": \"Invalid team slug\"\n}", strings.TrimSpace(w.Body.String()))
})
t.Run("with a StatusProvider", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteError(w, rpc.Error(400, "invalid_slug", "Invalid team slug"))
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, 400, w.Code)
})
}