-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
74 lines (61 loc) · 1.99 KB
/
context_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
// 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"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestServerName(t *testing.T) {
t.Run("empty value", func(t *testing.T) {
assert.Equal(t, "", ServerName(context.Background()))
})
}
func TestAddServerName(t *testing.T) {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
require.NoError(t, err)
t.Run("normal", func(t *testing.T) {
var have string
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
have = ServerName(req.Context())
})
AddServerName("foobar", handler).ServeHTTP(nil, req)
assert.Equal(t, "foobar", have)
})
t.Run("wrapped", func(t *testing.T) {
var have string
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
have = ServerName(req.Context())
})
AddHandlerName("myhandler", AddServerName("foobar", handler)).ServeHTTP(nil, req)
assert.Equal(t, "foobar", have)
})
}
func TestHandlerName(t *testing.T) {
t.Run("empty value", func(t *testing.T) {
assert.Equal(t, "", HandlerName(context.Background()))
})
}
func TestAddHandlerName(t *testing.T) {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
require.NoError(t, err)
t.Run("normal", func(t *testing.T) {
var have string
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
have = HandlerName(req.Context())
})
AddHandlerName("foobar", handler).ServeHTTP(nil, req)
assert.Equal(t, "foobar", have)
})
t.Run("wrapped", func(t *testing.T) {
var have string
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
have = HandlerName(req.Context())
})
AddServerName("myserver", AddHandlerName("foobar", handler)).ServeHTTP(nil, req)
assert.Equal(t, "foobar", have)
})
}