-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse_bench_test.go
More file actions
180 lines (158 loc) · 4.79 KB
/
Copy pathresponse_bench_test.go
File metadata and controls
180 lines (158 loc) · 4.79 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package bast_test
import (
"net/http/httptest"
"testing"
"github.com/bastion-framework/bast"
)
// --- Correctness tests for response envelope ---
func TestCtxOK_Nil_Envelope(t *testing.T) {
ctx := bast.NewTestCtxWithPath("/")
resp := ctx.OK(nil)
got := string(resp.Body())
want := `{"data":null,"meta":null}`
if got != want {
t.Errorf("OK(nil) body = %q, want %q", got, want)
}
if resp.Status() != 200 {
t.Errorf("OK(nil) status = %d, want 200", resp.Status())
}
}
func TestCtxOK_Data_Envelope(t *testing.T) {
ctx := bast.NewTestCtxWithPath("/")
resp := ctx.OK(map[string]string{"id": "42"})
got := string(resp.Body())
if got == "" {
t.Fatal("OK(data) produced empty body")
}
if resp.ContentType() != "application/json" {
t.Errorf("content-type = %q, want application/json", resp.ContentType())
}
}
func TestCtxCreated_Nil_Envelope(t *testing.T) {
ctx := bast.NewTestCtxWithPath("/")
resp := ctx.Created(nil)
got := string(resp.Body())
want := `{"data":null,"meta":null}`
if got != want {
t.Errorf("Created(nil) body = %q, want %q", got, want)
}
if resp.Status() != 201 {
t.Errorf("Created(nil) status = %d, want 201", resp.Status())
}
}
func TestResponse_Headers_NilOnFreshResponse(t *testing.T) {
r := bast.NewRawResponse(200, "application/json", nil)
if h := r.Headers(); h != nil {
t.Errorf("fresh response Headers() = %v, want nil", h)
}
}
func TestResponse_WithHeader_BuildsCorrectMap(t *testing.T) {
r := bast.NewRawResponse(200, "", nil)
r = r.WithHeader("X-Trace", "abc").WithHeader("X-Request-Id", "xyz")
h := r.Headers()
if h["X-Trace"] != "abc" {
t.Errorf("X-Trace = %q, want abc", h["X-Trace"])
}
if h["X-Request-Id"] != "xyz" {
t.Errorf("X-Request-Id = %q, want xyz", h["X-Request-Id"])
}
}
// --- Security tests ---
func TestWithHeader_StripsCRLF_InValue(t *testing.T) {
r := bast.NewRawResponse(200, "", nil)
r = r.WithHeader("X-Custom", "val\r\nX-Injected: evil")
h := r.Headers()
want := "valX-Injected: evil"
if h["X-Custom"] != want {
t.Errorf("WithHeader value after CRLF strip = %q, want %q", h["X-Custom"], want)
}
}
func TestWithHeader_StripsCRLF_InKey(t *testing.T) {
r := bast.NewRawResponse(200, "", nil)
r = r.WithHeader("X-Key\r\n", "value")
h := r.Headers()
if h["X-Key"] != "value" {
t.Errorf("key after CRLF strip not found: headers = %v", h)
}
}
func TestWithHeader_CleanHeader_Unchanged(t *testing.T) {
r := bast.NewRawResponse(200, "", nil)
r = r.WithHeader("X-Request-Id", "req-123")
h := r.Headers()
if h["X-Request-Id"] != "req-123" {
t.Errorf("clean header changed unexpectedly: %v", h)
}
}
// --- Micro-benchmarks ---
// BenchmarkResponse_NewRaw measures raw response construction.
// Target after Fix 1+4: 0 allocs/op.
func BenchmarkResponse_NewRaw(b *testing.B) {
body := []byte(`{"data":null,"meta":null}`)
b.ReportAllocs()
for b.Loop() {
_ = bast.NewRawResponse(200, "application/json", body)
}
}
// BenchmarkResponse_OK_Nil measures ctx.OK(nil).
// Target after Fix 2+1+4: 0 allocs/op (pre-baked envelope, no map init).
func BenchmarkResponse_OK_Nil(b *testing.B) {
ctx := bast.NewTestCtxWithPath("/")
b.ReportAllocs()
for b.Loop() {
_ = ctx.OK(nil)
}
}
// BenchmarkResponse_OK_Data measures ctx.OK(data) — JSON marshal path.
// Target after Fix 3+1+4: reduced allocs vs baseline.
func BenchmarkResponse_OK_Data(b *testing.B) {
ctx := bast.NewTestCtxWithPath("/")
data := map[string]string{"id": "42", "name": "kasim"}
b.ReportAllocs()
for b.Loop() {
_ = ctx.OK(data)
}
}
// BenchmarkResponse_WithHeader measures the WithHeader copy path.
// Target after Fix 4: 1 alloc/op (one slice allocation, no map).
func BenchmarkResponse_WithHeader(b *testing.B) {
base := bast.NewRawResponse(200, "application/json", nil)
b.ReportAllocs()
for b.Loop() {
_ = base.WithHeader("X-Request-Id", "bench-id-12345")
}
}
// BenchmarkResponse_Raw measures ctx.Raw (no marshal).
// Target after Fix 1+4: 0 allocs/op (no map, no body alloc).
func BenchmarkResponse_Raw(b *testing.B) {
ctx := bast.NewTestCtxWithPath("/")
body := []byte(`{"data":null,"meta":null}`)
b.ReportAllocs()
for b.Loop() {
_ = ctx.Raw(200, "application/json", body)
}
}
// BenchmarkApp_WriteResponse exercises the full ServeHTTP path with ctx.OK(nil).
// This is the production hot path benchmark.
func BenchmarkApp_WriteResponse_OKNil(b *testing.B) {
app := bast.New(bast.Config{Logger: silentLogger{}})
app.Register(bast.Module{
Prefix: "/bench",
Controller: &benchOKNilController{},
})
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/bench/ping", nil)
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
w.Body.Reset()
app.ServeHTTP(w, r)
}
}
type benchOKNilController struct{}
func (c *benchOKNilController) Routes() []bast.Route {
return []bast.Route{
bast.GET("/ping", func(ctx *bast.Ctx) bast.Response {
return ctx.OK(nil)
}),
}
}