-
Notifications
You must be signed in to change notification settings - Fork 6
/
response_recorder_test.go
99 lines (80 loc) · 2.42 KB
/
response_recorder_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
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
// Copyright (c) 2021, Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web_test
import (
"net/http"
"net/http/httptest"
"testing"
"resenje.org/web"
)
func TestResponseStatusRecorder_noWrite(t *testing.T) {
w := httptest.NewRecorder()
rec := web.NewResponseStatusRecorder(w)
if size := rec.ResponseBodySize(); size != 0 {
t.Errorf("got %v bytes that are written as body, want 0", size)
}
if status := rec.Status(); status != 0 {
t.Errorf("git status %v, want %v", status, 0)
}
}
func TestResponseStatusRecorder_write(t *testing.T) {
w := httptest.NewRecorder()
rec := web.NewResponseStatusRecorder(w)
n, err := rec.Write([]byte("hi"))
if err != nil {
t.Fatal(err)
}
if n != 2 {
t.Errorf("got %v bytes that are written, want 2", n)
}
if size := rec.ResponseBodySize(); size != 2 {
t.Errorf("got %v bytes that are written as body, want 2", size)
}
if status := rec.Status(); status != http.StatusOK {
t.Errorf("git status %v, want %v", status, http.StatusOK)
}
n, err = rec.Write([]byte("hello"))
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Errorf("got %v bytes that are written, want 5", n)
}
if size := rec.ResponseBodySize(); size != 7 {
t.Errorf("got %v bytes that are written as body, want 7", size)
}
}
func TestResponseStatusRecorder_writeHeader(t *testing.T) {
w := httptest.NewRecorder()
rec := web.NewResponseStatusRecorder(w)
rec.WriteHeader(http.StatusTeapot)
if size := rec.ResponseBodySize(); size != 0 {
t.Errorf("got %v bytes that are written as body, want 0", size)
}
if status := rec.Status(); status != http.StatusTeapot {
t.Errorf("git status %v, want %v", status, http.StatusTeapot)
}
}
func TestResponseStatusRecorder_writeHeaderAfterWrite(t *testing.T) {
w := httptest.NewRecorder()
rec := web.NewResponseStatusRecorder(w)
n, err := rec.Write([]byte("hi"))
if err != nil {
t.Fatal(err)
}
if n != 2 {
t.Errorf("got %v bytes that are written, want 2", n)
}
if size := rec.ResponseBodySize(); size != 2 {
t.Errorf("got %v bytes that are written as body, want 2", size)
}
if status := rec.Status(); status != http.StatusOK {
t.Errorf("git status %v, want %v", status, http.StatusOK)
}
rec.WriteHeader(http.StatusTeapot)
if status := rec.Status(); status != http.StatusOK {
t.Errorf("git status %v, want %v", status, http.StatusOK)
}
}