-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo_test.go
More file actions
executable file
·143 lines (124 loc) · 3.72 KB
/
Copy pathinfo_test.go
File metadata and controls
executable file
·143 lines (124 loc) · 3.72 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
// Copyright (c) 2023, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package serv
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-pogo/serv/response"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestContextWithInfo(t *testing.T) {
t.Run("add", func(t *testing.T) {
want := Info{
ServerName: "foo",
HandlerName: "bar",
}
ctx := context.Background()
assert.Nil(t, InfoFromContext(ctx))
have := InfoFromContext(ContextWithInfo(ctx, want))
assert.NotNil(t, have)
assert.Equal(t, want, *have)
})
t.Run("replace", func(t *testing.T) {
want := Info{
ServerName: "foo",
HandlerName: "bar",
}
ctx := ContextWithInfo(context.Background(), Info{ServerName: "initial"})
assert.Equal(t, "initial", ServerName(ctx))
_ = ContextWithInfo(ctx, want)
assert.Equal(t, want, *InfoFromContext(ctx))
})
}
func TestServerName(t *testing.T) {
t.Run("empty context", func(t *testing.T) {
assert.Equal(t, "", ServerName(context.Background()))
})
t.Run("from context", func(t *testing.T) {
const want = "foobar"
assert.Equal(t, want, ServerName(ContextWithInfo(
context.Background(),
Info{ServerName: want},
)))
})
t.Run("server handler", func(t *testing.T) {
const want = "quxoo"
srv, err := New(WithName(want))
require.NoError(t, err)
srv.Handler = http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
assert.Equal(t, want, ServerName(req.Context()))
})
require.NoError(t, srv.start())
srv.httpServer.Handler.ServeHTTP(nil, httptest.NewRequest(http.MethodGet, "/", nil))
})
}
func TestHandlerName(t *testing.T) {
t.Run("empty context", func(t *testing.T) {
assert.Equal(t, "", HandlerName(context.Background()))
})
t.Run("from context", func(t *testing.T) {
const want = "foobar"
assert.Equal(t, want, HandlerName(ContextWithInfo(
context.Background(),
Info{HandlerName: want},
)))
})
t.Run("route handler", func(t *testing.T) {
const want = "quxoo"
route := Route{
Name: want,
Method: http.MethodGet,
Pattern: "/",
Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
assert.Equal(t, want, HandlerName(req.Context()))
}),
}
route.ServeHTTP(nil, httptest.NewRequest(route.Method, route.Pattern, nil))
})
}
func TestRequestID(t *testing.T) {
t.Run("empty context", func(t *testing.T) {
assert.Equal(t, "", RequestID(context.Background()))
})
t.Run("from context", func(t *testing.T) {
const want = "foobar"
assert.Equal(t, want, RequestID(ContextWithInfo(
context.Background(),
Info{RequestID: want},
)))
})
}
func testInfoHandlers(t *testing.T,
addFn func(string, http.Handler) http.Handler,
getFn func(context.Context) string,
) {
t.Run("inside handler", func(t *testing.T) {
var have string
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
have = getFn(req.Context())
})
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
addFn("foobar", handler).ServeHTTP(nil, req)
assert.Equal(t, "foobar", have)
})
t.Run("outside handler", func(t *testing.T) {
ctx := ContextWithInfo(context.Background(), Info{})
assert.Empty(t, getFn(ctx))
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/", nil)
addFn("foobar", response.NoopHandler()).ServeHTTP(nil, req)
assert.Equal(t, "foobar", getFn(ctx))
})
}
func TestAddServerName(t *testing.T) {
testInfoHandlers(t, AddServerName, ServerName)
}
func TestAddHandlerName(t *testing.T) {
testInfoHandlers(t, AddHandlerName, HandlerName)
}
func TestAddRequestID(t *testing.T) {
testInfoHandlers(t, AddRequestID, RequestID)
}