@@ -11,14 +11,20 @@ import (
1111)
1212
1313func TestHttpAuthRandom_OnHttpRequestHeaders (t * testing.T ) {
14- host , done := proxytest .NewHttpFilterHost (newContext )
15- defer done ()
14+ opt := proxytest .NewEmulatorOption ().
15+ WithNewHttpContext (newContext )
16+ host := proxytest .NewHostEmulator (opt )
17+ defer host .Done ()
1618
17- id := host .InitContext ()
18- host .PutRequestHeaders ( id , [][2 ]string {{"key" , "value" }}) // OnHttpRequestHeaders called
19+ contextID := host .HttpFilterInitContext ()
20+ host .HttpFilterPutRequestHeaders ( contextID , [][2 ]string {{"key" , "value" }}) // OnHttpRequestHeaders called
1921
20- require .True (t , host .IsDispatchCalled (id )) // check if http call is dispatched
21- require .Equal (t , types .ActionPause , host .GetCurrentAction (id )) // check if the current action is pause
22+ attrs := host .GetCalloutAttributesFromContext (contextID )
23+ require .Equal (t , len (attrs ), 1 ) // verify DispatchHttpCall is called
24+
25+ require .Equal (t , "httpbin" , attrs [0 ].Upstream )
26+ require .Equal (t , types .ActionPause ,
27+ host .HttpFilterGetCurrentStreamAction (contextID )) // check if the current action is pause
2228
2329 logs := host .GetLogs (types .LogLevelInfo )
2430 require .GreaterOrEqual (t , len (logs ), 2 )
@@ -28,8 +34,10 @@ func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
2834}
2935
3036func TestHttpAuthRandom_OnHttpCallResponse (t * testing.T ) {
31- host , done := proxytest .NewHttpFilterHost (newContext )
32- defer done ()
37+ opt := proxytest .NewEmulatorOption ().
38+ WithNewHttpContext (newContext )
39+ host := proxytest .NewHostEmulator (opt )
40+ defer host .Done ()
3341
3442 // http://httpbin.org/uuid
3543 headers := [][2 ]string {
@@ -40,20 +48,30 @@ func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
4048 }
4149
4250 // access granted body
43- id := host .InitContext ()
51+ contextID := host .HttpFilterInitContext ()
52+ host .HttpFilterPutRequestHeaders (contextID , nil ) // OnHttpRequestHeaders called
53+
4454 body := []byte (`{"uuid": "7b10a67a-1c67-4199-835b-cbefcd4a63d4"}` )
45- host .PutCalloutResponse (id , headers , nil , body )
46- assert .Nil (t , host .GetSentLocalResponse (id ))
55+ attrs := host .GetCalloutAttributesFromContext (contextID )
56+ require .Equal (t , len (attrs ), 1 ) // verify DispatchHttpCall is called
57+
58+ host .PutCalloutResponse (attrs [0 ].CalloutID , headers , nil , body )
59+ assert .Nil (t , host .HttpFilterGetSentLocalResponse (contextID ))
4760
4861 logs := host .GetLogs (types .LogLevelInfo )
4962 require .Greater (t , len (logs ), 1 )
5063 assert .Equal (t , "access granted" , logs [len (logs )- 1 ])
5164
5265 // access denied body
53- id = host .InitContext ()
66+ contextID = host .HttpFilterInitContext ()
67+ host .HttpFilterPutRequestHeaders (contextID , nil ) // OnHttpRequestHeaders called
68+
5469 body = []byte (`{"uuid": "aaaaaaaa-1c67-4199-835b-cbefcd4a63d4"}` )
55- host .PutCalloutResponse (id , headers , nil , body )
56- localResponse := host .GetSentLocalResponse (id ) // check local responses
70+ attrs = host .GetCalloutAttributesFromContext (contextID )
71+ require .Equal (t , len (attrs ), 1 ) // verify DispatchHttpCall is called
72+
73+ host .PutCalloutResponse (attrs [0 ].CalloutID , headers , nil , body )
74+ localResponse := host .HttpFilterGetSentLocalResponse (contextID ) // check local responses
5775 assert .NotNil (t , localResponse )
5876 logs = host .GetLogs (types .LogLevelInfo )
5977 assert .Equal (t , "access forbidden" , logs [len (logs )- 1 ])
0 commit comments