forked from Azure/azure-sdk-for-go
/
policy_logging_test.go
118 lines (111 loc) · 3.32 KB
/
policy_logging_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// +build go1.13
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azcore
import (
"context"
"errors"
"net/http"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
)
func TestPolicyLoggingSuccess(t *testing.T) {
log := map[LogClassification]string{}
Log().SetListener(func(cls LogClassification, s string) {
log[cls] = s
})
srv, close := mock.NewServer()
defer close()
srv.SetResponse()
pl := NewPipeline(srv, NewRequestLogPolicy(RequestLogOptions{}))
req := NewRequest(http.MethodGet, srv.URL())
qp := req.URL.Query()
qp.Set("one", "fish")
qp.Set("sig", "redact")
req.URL.RawQuery = qp.Encode()
resp, err := pl.Do(context.Background(), req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
if logReq, ok := log[LogRequest]; ok {
// Request ==> OUTGOING REQUEST (Try=1)
// GET http://127.0.0.1:49475?one=fish&sig=REDACTED
// (no headers)
if !strings.Contains(logReq, "sig=REDACTED") {
t.Fatal("missing redacted sig query param")
}
if !strings.Contains(logReq, "(no headers)") {
t.Fatal("missing (no headers)")
}
} else {
t.Fatal("missing LogRequest")
}
if logResp, ok := log[LogResponse]; ok {
// Response ==> REQUEST/RESPONSE (Try=1/1.0034ms, OpTime=1.0034ms) -- RESPONSE SUCCESSFULLY RECEIVED
// GET http://127.0.0.1:49475?one=fish&sig=REDACTED
// (no headers)
// --------------------------------------------------------------------------------
// RESPONSE Status: 200 OK
// Content-Length: [0]
// Date: [Fri, 22 Nov 2019 23:48:02 GMT]
if !strings.Contains(logResp, "RESPONSE Status: 200 OK") {
t.Fatal("missing response status")
}
} else {
t.Fatal("missing LogResponse")
}
}
func TestPolicyLoggingError(t *testing.T) {
log := map[LogClassification]string{}
Log().SetListener(func(cls LogClassification, s string) {
log[cls] = s
})
srv, close := mock.NewServer()
defer close()
srv.SetError(errors.New("bogus error"))
pl := NewPipeline(srv, NewRequestLogPolicy(RequestLogOptions{}))
req := NewRequest(http.MethodGet, srv.URL())
req.Header.Add("header", "one")
req.Header.Add("Authorization", "redact")
resp, err := pl.Do(context.Background(), req)
if err == nil {
t.Fatal("unexpected nil error")
}
if resp != nil {
t.Fatal("unexpected respose")
}
if logReq, ok := log[LogRequest]; ok {
// Request ==> OUTGOING REQUEST (Try=1)
// GET http://127.0.0.1:50057
// Authorization: REDACTED
// Header: [one]
if !strings.Contains(logReq, "Authorization: REDACTED") {
t.Fatal("missing redacted authorization header")
}
} else {
t.Fatal("missing LogRequest")
}
if logError, ok := log[LogError]; ok {
// Error ==> REQUEST/RESPONSE (Try=1/0s, OpTime=0s) -- REQUEST ERROR
// GET http://127.0.0.1:50057
// Authorization: REDACTED
// Header: [one]
// --------------------------------------------------------------------------------
// ERROR:
// bogus error
// ...stack track...
if !strings.Contains(logError, "Authorization: REDACTED") {
t.Fatal("missing redacted authorization header")
}
if !strings.Contains(logError, "bogus error") {
t.Fatal("missing error message")
}
} else {
t.Fatal("missing LogError")
}
}
// TODO: add test for slow response