func TestReplaceImage(t *testing.T) { proxy := goproxy.NewProxyHttpServer() panda := getImage("test_data/panda.png", t) football := getImage("test_data/football.png", t) proxy.HandleResponse(goproxy.UrlIsIn("/test_data/panda.png")(goproxy_image.HandleImage(func(img image.Image, ctx *goproxy.ProxyCtx) image.Image { return football }))) proxy.HandleResponse(goproxy.UrlIsIn("/test_data/football.png")(goproxy_image.HandleImage(func(img image.Image, ctx *goproxy.ProxyCtx) image.Image { return panda }))) client, l := oneShotProxy(proxy, t) defer l.Close() imgByPandaReq, _, err := image.Decode(bytes.NewReader(getOrFail(localFile("test_data/panda.png"), client, t))) fatalOnErr(err, "decode panda", t) compareImage(football, imgByPandaReq, t) imgByFootballReq, _, err := image.Decode(bytes.NewReader(getOrFail(localFile("test_data/football.png"), client, t))) fatalOnErr(err, "decode football", t) compareImage(panda, imgByFootballReq, t) }
func TestMitmIsFiltered(t *testing.T) { proxy := goproxy.NewProxyHttpServer() //proxy.Verbose = true proxy.HandleConnect(goproxy.AlwaysMitm) // PREVIOUSLY: proxy.OnRequest(goproxy.ReqHostIs(https.Listener.Addr().String())).HandleConnect(goproxy.AlwaysMitm) proxy.HandleRequest(goproxy.UrlIsIn("/momo")(goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next { ctx.NewTextResponse("koko") return goproxy.FORWARD }))) client, l := oneShotProxy(proxy, t) defer l.Close() if resp := string(getOrFail(https.URL+"/momo", client, t)); resp != "koko" { t.Error("Proxy should capture /momo to be koko and not", resp) } if resp := string(getOrFail(https.URL+"/bobo", client, t)); resp != "bobo" { t.Error("But still /bobo should be 'bobo' and not", resp) } }
func TestReplaceReponseForUrl(t *testing.T) { proxy := goproxy.NewProxyHttpServer() proxy.HandleResponse(goproxy.UrlIsIn("/koko")(goproxy.HandlerFunc(func(ctx *goproxy.ProxyCtx) goproxy.Next { if ctx.Resp == nil { return goproxy.NEXT } ctx.Resp.StatusCode = http.StatusOK ctx.SetResponseBody([]byte("chico")) return goproxy.NEXT }))) client, l := oneShotProxy(proxy, t) defer l.Close() if result := string(getOrFail(srv.URL+("/koko"), client, t)); result != "chico" { t.Error("hooked 'koko', should be chico, instead:", result) } if result := string(getOrFail(srv.URL+("/bobo"), client, t)); result != "bobo" { t.Error("still, bobo should stay as usual, instead:", result) } }
func TestImageHandler(t *testing.T) { proxy := goproxy.NewProxyHttpServer() football := getImage("test_data/football.png", t) proxy.HandleResponse(goproxy.UrlIsIn("/test_data/panda.png")(goproxy_image.HandleImage(func(img image.Image, ctx *goproxy.ProxyCtx) image.Image { return football }))) client, l := oneShotProxy(proxy, t) defer l.Close() resp, err := client.Get(localFile("test_data/panda.png")) if err != nil { t.Fatal("Cannot get panda.png", err) } img, _, err := image.Decode(resp.Body) if err != nil { t.Error("decode", err) } else { compareImage(football, img, t) } // and again resp, err = client.Get(localFile("test_data/panda.png")) if err != nil { t.Fatal("Cannot get panda.png", err) } img, _, err = image.Decode(resp.Body) if err != nil { t.Error("decode", err) } else { compareImage(football, img, t) } }