func NewWebSocketApp(urls []route.Uri, rPort uint16, mbusClient yagnats.ApceraWrapperNATSClient, delay time.Duration) *TestApp {
	app := NewTestApp(urls, rPort, mbusClient, nil)
	app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
		defer ginkgo.GinkgoRecover()

		Ω(r.Header.Get("Upgrade")).Should(Equal("websocket"))
		Ω(r.Header.Get("Connection")).Should(Equal("upgrade"))

		conn, _, err := w.(http.Hijacker).Hijack()
		x := test_util.NewHttpConn(conn)

		resp := test_util.NewResponse(http.StatusSwitchingProtocols)
		resp.Header.Set("Upgrade", "websocket")
		resp.Header.Set("Connection", "upgrade")

		time.Sleep(delay)

		x.WriteResponse(resp)
		Ω(err).ShouldNot(HaveOccurred())

		x.CheckLine("hello from client")
		x.WriteLine("hello from server")
	})

	return app
}
func shouldEcho(input string, expected string) {
	ln := registerHandler(r, "encoding", func(x *test_util.HttpConn) {
		x.CheckLine("GET " + expected + " HTTP/1.1")
		resp := test_util.NewResponse(http.StatusOK)
		x.WriteResponse(resp)
		x.Close()
	})
	defer ln.Close()

	x := dialProxy(proxyServer)

	req := test_util.NewRequest("GET", "encoding", input, nil)
	x.WriteRequest(req)
	resp, _ := x.ReadResponse()

	Expect(resp.StatusCode).To(Equal(http.StatusOK))
}
	"github.com/cloudfoundry/gorouter/proxy"
	"github.com/cloudfoundry/gorouter/test_util"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Session Affinity", func() {
	var done chan bool
	var jSessionIdCookie *http.Cookie

	responseNoCookies := func(x *test_util.HttpConn) {
		_, err := http.ReadRequest(x.Reader)
		Expect(err).ToNot(HaveOccurred())

		resp := test_util.NewResponse(http.StatusOK)
		x.WriteResponse(resp)
		x.Close()
		done <- true
	}

	responseWithJSessionID := func(x *test_util.HttpConn) {
		_, err := http.ReadRequest(x.Reader)
		Expect(err).ToNot(HaveOccurred())

		resp := test_util.NewResponse(http.StatusOK)
		resp.Header.Add("Set-Cookie", jSessionIdCookie.String())
		x.WriteResponse(resp)
		x.Close()
		done <- true
	}
func (_ nullVarz) MarshalJSON() ([]byte, error)                                                     { return json.Marshal(nil) }
func (_ nullVarz) ActiveApps() *stats.ActiveApps                                                    { return stats.NewActiveApps() }
func (_ nullVarz) CaptureBadRequest(*http.Request)                                                  {}
func (_ nullVarz) CaptureBadGateway(*http.Request)                                                  {}
func (_ nullVarz) CaptureRoutingRequest(b *route.Endpoint, req *http.Request)                       {}
func (_ nullVarz) CaptureRoutingResponse(*route.Endpoint, *http.Response, time.Time, time.Duration) {}
func (_ nullVarz) CaptureRegistryMessage(msg metrics.ComponentTagged)                               {}

var _ = Describe("Proxy", func() {

	It("responds to http/1.0 with path", func() {
		ln := registerHandler(r, "test/my_path", func(conn *test_util.HttpConn) {
			conn.CheckLine("GET /my_path HTTP/1.1")

			conn.WriteResponse(test_util.NewResponse(http.StatusOK))
		})
		defer ln.Close()

		conn := dialProxy(proxyServer)

		conn.WriteLines([]string{
			"GET /my_path HTTP/1.0",
			"Host: test",
		})

		conn.CheckLine("HTTP/1.0 200 OK")
	})

	It("responds transparently to a trailing slash versus no trailing slash", func() {
		lnWithoutSlash := registerHandler(r, "test/my%20path/your_path", func(conn *test_util.HttpConn) {
		req := x.NewRequest("GET", "/", nil)
		req.Host = "enfant-terrible"
		x.WriteRequest(req)

		resp, body := x.ReadResponse()
		Ω(resp.StatusCode).To(Equal(http.StatusBadGateway))
		Ω(resp.Header.Get("X-Cf-RouterError")).To(Equal("endpoint_failure"))
		Ω(body).To(Equal("502 Bad Gateway: Registered endpoint failed to handle the request.\n"))
	})

	It("trace headers added on correct TraceKey", func() {
		ln := registerHandler(r, "trace-test", func(x *test_util.HttpConn) {
			_, err := http.ReadRequest(x.Reader)
			Ω(err).NotTo(HaveOccurred())

			resp := test_util.NewResponse(http.StatusOK)
			x.WriteResponse(resp)
			x.Close()
		})
		defer ln.Close()

		x := dialProxy(proxyServer)

		req := x.NewRequest("GET", "/", nil)
		req.Host = "trace-test"
		req.Header.Set(router_http.VcapTraceHeader, "my_trace_key")
		x.WriteRequest(req)

		resp, _ := x.ReadResponse()
		Ω(resp.StatusCode).To(Equal(http.StatusOK))
		Ω(resp.Header.Get(router_http.VcapBackendHeader)).To(Equal(ln.Addr().String()))