@@ -17,30 +17,46 @@ import (
17
17
func TestAddToContext (t * testing.T ) {
18
18
ctx := testcontext .New (t )
19
19
20
- request , err := http .NewRequestWithContext (ctx , "GET" , "" , http .NoBody )
21
- require .NoError (t , err )
22
-
23
- rw := httptest .NewRecorder ()
24
-
25
20
var requestID string
26
- handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
21
+ handler := AddToContext ( http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
27
22
w .WriteHeader (http .StatusOK )
28
23
require .NotNil (t , r .Context ().Value (contextKey {}), "RequestId should not be nil" )
29
24
require .NotEqual (t , "" , r .Context ().Value (contextKey {}).(string ), "RequestId not set in Context" )
30
25
31
26
requestID = r .Context ().Value (contextKey {}).(string )
27
+ }))
28
+
29
+ t .Run ("success" , func (t * testing.T ) {
30
+ rw := httptest .NewRecorder ()
31
+
32
+ request , err := http .NewRequestWithContext (ctx , "GET" , "" , http .NoBody )
33
+ require .NoError (t , err )
34
+ handler .ServeHTTP (rw , request )
35
+
36
+ require .NotEqual (t , "" , rw .Header ().Get (HeaderKey ), "RequestId is not set in response header" )
37
+ require .Equal (t , requestID , rw .Header ().Get (HeaderKey ), "Correct RequestId is not set in response header" )
32
38
})
33
39
34
- newHandler := AddToContext (handler )
35
- newHandler .ServeHTTP (rw , request )
40
+ t .Run ("too-long" , func (t * testing.T ) {
41
+ rw := httptest .NewRecorder ()
42
+
43
+ request , err := http .NewRequestWithContext (ctx , "GET" , "" , http .NoBody )
44
+ require .NoError (t , err )
45
+ const tooLongKey = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
46
+ request .Header .Set (HeaderKey , tooLongKey )
47
+
48
+ handler .ServeHTTP (rw , request )
36
49
37
- require .NotEqual (t , "" , rw .Header ().Get (HeaderKey ), "RequestId is not set in response header" )
38
- require .Equal (t , requestID , rw .Header ().Get (HeaderKey ), "Correct RequestId is not set in response header" )
50
+ require .NotEqual (t , "" , rw .Header ().Get (HeaderKey ), "RequestId is not set in response header" )
51
+ require .NotEqual (t , tooLongKey , requestID )
52
+ })
39
53
}
40
54
41
55
func TestPropagate (t * testing.T ) {
42
56
ctx := testcontext .New (t )
43
57
58
+ require .Equal (t , "" , FromContext (ctx ))
59
+
44
60
requestID := "test-request-id"
45
61
reqctx := context .WithValue (ctx , contextKey {}, requestID )
46
62
0 commit comments