-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrap_test.go
59 lines (48 loc) · 1.28 KB
/
wrap_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
package gaelog
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/kylelemons/godebug/pretty"
"google.golang.org/genproto/googleapis/api/monitoredres"
)
func TestWrap(t *testing.T) {
envVars := map[string]string{
"GOOGLE_CLOUD_PROJECT": testProjectID,
"GAE_SERVICE": testServiceID,
"GAE_VERSION": testVersionID,
}
unset := setEnvVars(envVars)
defer unset()
expectedResource := &monitoredres.MonitoredResource{
Labels: map[string]string{
"module_id": testServiceID,
"project_id": testProjectID,
"version_id": testVersionID,
},
Type: "gae_app",
}
handler := Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cv := ctx.Value(ctxKey)
if cv == nil {
t.Errorf("expected value for key %q, got nil", ctxKey)
return
}
logger, ok := cv.(*Logger)
if !ok {
t.Errorf("expected var of type *Logger, got %T", cv)
return
}
if diff := pretty.Compare(logger.monRes, expectedResource); diff != "" {
t.Errorf("Unexpected result (-got +want):\n%s", diff)
return
}
fmt.Fprintf(w, "ok")
}))
req := httptest.NewRequest("GET", "http://example.com", nil)
req.Header.Set(traceContextHeaderName, "abcdef0123456789/abcdef")
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
}