-
Notifications
You must be signed in to change notification settings - Fork 14
/
user_agent_handler_test.go
74 lines (69 loc) · 2.08 KB
/
user_agent_handler_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
package nethttplibrary
import (
nethttp "net/http"
httptest "net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestItAddsTheUserAgentHeader(t *testing.T) {
handler := NewUserAgentHandler()
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(200)
res.Write([]byte("body"))
}))
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
assert.Equal(t, "kiota-go", strings.Split(req.Header.Get("User-Agent"), "/")[0])
}
func TestItAddsTheUserAgentHeaderOnce(t *testing.T) {
handler := NewUserAgentHandler()
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(200)
res.Write([]byte("body"))
}))
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
resp, err = handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
assert.Equal(t, 1, len(strings.Split(req.Header.Get("User-Agent"), "kiota-go"))-1)
}
func TestItDoesNotAddTheUserAgentHeaderWhenDisabled(t *testing.T) {
options := NewUserAgentHandlerOptions()
options.Enabled = false
handler := NewUserAgentHandlerWithOptions(options)
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(200)
res.Write([]byte("body"))
}))
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
resp, err = handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
assert.Equal(t, false, strings.Contains(req.Header.Get("User-Agent"), "kiota-go"))
}