func NewWebSocketApp(urls []route.Uri, rPort uint16, mbusClient yagnats.NATSConn, delay time.Duration) *TestApp { app := NewTestApp(urls, rPort, mbusClient, nil, "") app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) { defer ginkgo.GinkgoRecover() Expect(r.Header.Get("Upgrade")).To(Equal("websocket")) Expect(r.Header.Get("Connection")).To(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) Expect(err).ToNot(HaveOccurred()) x.CheckLine("hello from client") x.WriteLine("hello from server") }) return app }
func runBackendInstance(ln net.Listener, handler connHandler) { var tempDelay time.Duration // how long to sleep on accept failure for { conn, err := ln.Accept() if err != nil { if ne, ok := err.(net.Error); ok && ne.Temporary() { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } fmt.Printf("http: Accept error: %v; retrying in %v\n", err, tempDelay) time.Sleep(tempDelay) continue } break } go func() { defer GinkgoRecover() handler(test_util.NewHttpConn(conn)) }() } }
func dialProxy(proxyServer net.Listener) *test_util.HttpConn { conn, err := net.Dial("tcp", proxyServer.Addr().String()) Ω(err).NotTo(HaveOccurred()) return test_util.NewHttpConn(conn) }
It("websockets do not terminate", func() { app := test.NewWebSocketApp( []route.Uri{"ws-app.vcap.me"}, config.Port, mbusClient, 1*time.Second, ) app.Listen() Eventually(func() bool { return appRegistered(registry, app) }).Should(BeTrue()) conn, err := net.Dial("tcp", fmt.Sprintf("ws-app.vcap.me:%d", config.Port)) Ω(err).NotTo(HaveOccurred()) x := test_util.NewHttpConn(conn) req := test_util.NewRequest("GET", "ws-app.vcap.me", "/chat", nil) req.Header.Set("Upgrade", "websocket") req.Header.Set("Connection", "upgrade") x.WriteRequest(req) resp, _ := x.ReadResponse() Expect(resp.StatusCode).To(Equal(http.StatusSwitchingProtocols)) x.WriteLine("hello from client") x.CheckLine("hello from server") x.Close() })