func NewWebSocketApp(urls []route.Uri, rPort uint16, mbusClient *nats.Conn, delay time.Duration) *common.TestApp { app := common.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)) }() } }
proxyPort = test_util.NextAvailPort() cfgFile = filepath.Join(tmpdir, "config.yml") config = createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, 1, false, natsPort) }) JustBeforeEach(func() { gorouterSession = startGorouterSession(cfgFile) }) It("responds to healthcheck", func() { req := test_util.NewRequest("GET", "", "/", nil) req.Header.Set("User-Agent", "HTTP-Monitor/1.1") conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", proxyPort)) Expect(err).ToNot(HaveOccurred()) http_conn := test_util.NewHttpConn(conn) http_conn.WriteRequest(req) resp, body := http_conn.ReadResponse() Expect(resp.Status).To(Equal("200 OK")) Expect(body).To(Equal("ok\n")) }) It("waits for all requests to finish", func() { mbusClient, err := newMessageBus(config) Expect(err).ToNot(HaveOccurred()) requestMade := make(chan bool) requestProcessing := make(chan bool) responseRead := make(chan bool) longApp := common.NewTestApp([]route.Uri{"longapp.vcap.me"}, proxyPort, mbusClient, nil, "")
func dialProxy(proxyServer net.Listener) *test_util.HttpConn { conn, err := net.Dial("tcp", proxyServer.Addr().String()) Expect(err).NotTo(HaveOccurred()) return test_util.NewHttpConn(conn) }