-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
response_test.go
67 lines (55 loc) · 1.33 KB
/
response_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
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
package rpc_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/tj/assert"
"github.com/apex/rpc"
)
// DiscardResponseWriter .
type DiscardResponseWriter struct {
}
// Header implementation.
func (d DiscardResponseWriter) Header() http.Header {
return http.Header{}
}
// Write implementation.
func (d DiscardResponseWriter) Write([]byte) (int, error) {
return 0, nil
}
// WriteHeader implementation.
func (d DiscardResponseWriter) WriteHeader(int) {}
// Test responses.
func TestWriteResponse(t *testing.T) {
t.Run("with no content", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteResponse(w, nil)
assert.Equal(t, http.StatusNoContent, w.Code)
assert.Equal(t, ``, strings.TrimSpace(w.Body.String()))
})
t.Run("with no content", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteResponse(w, struct {
Name string `json:"name"`
}{
Name: "Tobi",
})
assert.Equal(t, 200, w.Code)
assert.Equal(t, "{\n \"name\": \"Tobi\"\n}", strings.TrimSpace(w.Body.String()))
})
}
// Benchmark responses.
func BenchmarkWriteResponse(b *testing.B) {
b.ReportAllocs()
b.SetBytes(1)
out := struct{ Name, Species, Email string }{
Name: "Tobi",
Species: "Ferret",
Email: "tobi@ferret.com",
}
var w DiscardResponseWriter
for i := 0; i < b.N; i++ {
rpc.WriteResponse(w, out)
}
}