@@ -41,9 +41,26 @@ func (QueryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
4141 io .WriteString (w , req .Form .Get ("result" ))
4242}
4343
44+ type HeadersHandler struct {}
45+
46+ // This handlers returns a body with a string containing all the request headers it received.
47+ func (HeadersHandler ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
48+ var sb strings.Builder
49+ for name , values := range req .Header {
50+ for _ , value := range values {
51+ sb .WriteString (name )
52+ sb .WriteString (": " )
53+ sb .WriteString (value )
54+ sb .WriteString (";" )
55+ }
56+ }
57+ io .WriteString (w , sb .String ())
58+ }
59+
4460func init () {
4561 http .DefaultServeMux .Handle ("/bobo" , ConstantHanlder ("bobo" ))
4662 http .DefaultServeMux .Handle ("/query" , QueryHandler {})
63+ http .DefaultServeMux .Handle ("/headers" , HeadersHandler {})
4764}
4865
4966type ConstantHanlder string
@@ -436,6 +453,33 @@ func TestSimpleMitm(t *testing.T) {
436453 }
437454}
438455
456+ func TestMitmMutateRequest (t * testing.T ) {
457+ mitmMutateRequest := func (req * http.Request , ctx * goproxy.ProxyCtx ) {
458+ // We inject a header in the request
459+ req .Header .Set ("Mitm-Header-Inject" , "true" )
460+ }
461+ mitmConnect := & goproxy.ConnectAction {
462+ Action : goproxy .ConnectMitm ,
463+ TLSConfig : goproxy .TLSConfigFromCA (& goproxy .GoproxyCa ),
464+ MitmMutateRequest : mitmMutateRequest ,
465+ }
466+ var mitm goproxy.FuncHttpsHandler = func (host string , ctx * goproxy.ProxyCtx ) (* goproxy.ConnectAction , string ) {
467+ return mitmConnect , host
468+ }
469+
470+ proxy := goproxy .NewProxyHttpServer ()
471+ proxy .OnRequest ().HandleConnect (mitm )
472+
473+ client , l := oneShotProxy (proxy , t )
474+ defer l .Close ()
475+
476+ r := string (getOrFail (https .URL + "/headers" , client , t ))
477+ if ! strings .Contains (r , "Mitm-Header-Inject: true" ) {
478+ t .Error ("Wrong response when mitm" , r , "expected MITM injected header to be returned" )
479+ }
480+
481+ }
482+
439483func TestConnectHandler (t * testing.T ) {
440484 proxy := goproxy .NewProxyHttpServer ()
441485 althttps := httptest .NewTLSServer (ConstantHanlder ("althttps" ))
0 commit comments