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)) }
AccessLogger: fakeAccessLogger, SecureCookies: conf.SecureCookies, TLSConfig: tlsConfig, RouteServiceEnabled: conf.RouteServiceEnabled, RouteServiceTimeout: conf.RouteServiceTimeout, Crypto: crypto, CryptoPrev: cryptoPrev, }) r.Register(route.Uri("some-app"), &route.Endpoint{}) }) Context("Log response time", func() { It("logs response time for HTTP connections", func() { body := []byte("some body") req := test_util.NewRequest("GET", "some-app", "/", bytes.NewReader(body)) resp := httptest.NewRecorder() proxyObj.ServeHTTP(resp, req) Expect(fakeAccessLogger.LogCallCount()).To(Equal(1)) Expect(fakeAccessLogger.LogArgsForCall(0).FinishedAt).NotTo(Equal(time.Time{})) }) It("logs response time for TCP connections", func() { req := test_util.NewRequest("UPGRADE", "some-app", "/", nil) req.Header.Set("Upgrade", "tcp") req.Header.Set("Connection", "upgrade") resp := httptest.NewRecorder() proxyObj.ServeHTTP(resp, req) Expect(fakeAccessLogger.LogCallCount()).To(Equal(1))
Value: "xxx", MaxAge: 1, Expires: time.Now(), } }) Context("context paths", func() { Context("when two requests have the same context paths", func() { It("responds with the same instance id", func() { ln := registerHandlerWithInstanceId(r, "app.com/path1", "", responseWithJSessionID, "instance-id-1") defer ln.Close() ln2 := registerHandlerWithInstanceId(r, "app.com/path2/context/path", "", responseWithJSessionID, "instance-id-2") defer ln2.Close() conn := dialProxy(proxyServer) req := test_util.NewRequest("GET", "app.com", "/path1/some/sub/path/index.html", nil) conn.WriteRequest(req) Eventually(done).Should(Receive()) resp, _ := conn.ReadResponse() cookie := getCookie(proxy.VcapCookieId, resp.Cookies()) Expect(cookie).ToNot(BeNil()) Expect(cookie.Path).To(Equal("/path1")) Expect(cookie.Value).To(Equal("instance-id-1")) req2 := test_util.NewRequest("GET", "app.com", "/path1/other/sub/path/index.html", nil) conn.WriteRequest(req2) Eventually(done).Should(Receive())
conn.WriteResponse(test_util.NewResponse(http.StatusOK)) }) defer lnWithoutSlash.Close() lnWithSlash := registerHandler(r, "test/another-path/your_path/", func(conn *test_util.HttpConn) { conn.CheckLine("GET /another-path/your_path HTTP/1.1") conn.WriteResponse(test_util.NewResponse(http.StatusOK)) }) defer lnWithSlash.Close() conn := dialProxy(proxyServer) y := dialProxy(proxyServer) req := test_util.NewRequest("GET", "test", "/my%20path/your_path/", nil) conn.WriteRequest(req) resp, _ := conn.ReadResponse() Expect(resp.StatusCode).To(Equal(http.StatusOK)) req = test_util.NewRequest("GET", "test", "/another-path/your_path", nil) y.WriteRequest(req) resp, _ = y.ReadResponse() Expect(resp.StatusCode).To(Equal(http.StatusOK)) }) It("Content-type is not set by proxy", func() { ln := registerHandler(r, "content-test", func(x *test_util.HttpConn) { _, err := http.ReadRequest(x.Reader)
}) app.Listen() go app.RegisterRepeatedly(1 * time.Second) Eventually(func() bool { return appRegistered(registry, app) }).Should(BeTrue()) conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", config.Ip, config.Port)) Expect(err).NotTo(HaveOccurred()) defer conn.Close() httpConn := test_util.NewHttpConn(conn) req := test_util.NewRequest("GET", "foo.vcap.me", "/", nil) req.Header.Add(router_http.VcapRequestIdHeader, "A-BOGUS-REQUEST-ID") httpConn.WriteRequest(req) var answer string Eventually(done).Should(Receive(&answer)) Expect(answer).ToNot(Equal("A-BOGUS-REQUEST-ID")) Expect(answer).To(MatchRegexp(uuid_regex)) Expect(logger).To(gbytes.Say("vcap-request-id-header-set")) resp, _ := httpConn.ReadResponse() Expect(resp.StatusCode).To(Equal(http.StatusOK)) }) It("handles a /routes request", func() {
endpointIterator *routefakes.FakeEndpointIterator transport *proxyfakes.FakeRoundTripper handler proxy.RequestHandler logger lager.Logger req *http.Request resp *proxyfakes.FakeProxyResponseWriter dialError = &net.OpError{ Err: errors.New("error"), Op: "dial", } after proxy.AfterRoundTrip ) BeforeEach(func() { endpointIterator = &routefakes.FakeEndpointIterator{} req = test_util.NewRequest("GET", "myapp.com", "/", nil) req.URL.Scheme = "http" resp = &proxyfakes.FakeProxyResponseWriter{} nullVarz := nullVarz{} nullAccessRecord := &access_log.AccessLogRecord{} logger = lagertest.NewTestLogger("test") handler = proxy.NewRequestHandler(req, resp, nullVarz, nullAccessRecord, logger) transport = &proxyfakes.FakeRoundTripper{} after = func(rsp *http.Response, endpoint *route.Endpoint, err error) { Expect(endpoint.Tags).ShouldNot(BeNil()) } })
conf.RouteServiceEnabled = false conf.SSLSkipValidation = true routeServiceHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { Fail("Should not get here into Route Service") }) }) It("return 502 Bad Gateway", func() { ln := registerHandlerWithRouteService(r, "my_host.com", "https://"+routeServiceListener.Addr().String(), func(conn *test_util.HttpConn) { Fail("Should not get here into the app") }) defer ln.Close() conn := dialProxy(proxyServer) req := test_util.NewRequest("GET", "my_host.com", "/", nil) conn.WriteRequest(req) res, body := conn.ReadResponse() Expect(res.StatusCode).To(Equal(http.StatusBadGateway)) Expect(body).To(ContainSubstring("Support for route services is disabled.")) }) }) Context("with SSLSkipValidation enabled", func() { BeforeEach(func() { conf.SSLSkipValidation = true }) Context("when a request does not have a valid Route service signature header", func() {
}) AfterEach(func() { crypto = nil cryptoPrev = nil config = nil }) Describe("SetupRouteServiceRequest", func() { var ( request *http.Request rsArgs route_service.RouteServiceArgs ) BeforeEach(func() { request = test_util.NewRequest("GET", "test.com", "/path/", nil) str := "https://example-route-service.com" parsed, err := url.Parse(str) Expect(err).NotTo(HaveOccurred()) rsArgs = route_service.RouteServiceArgs{ UrlString: str, ParsedUrl: parsed, Signature: "signature", Metadata: "metadata", ForwardedUrlRaw: "http://test.com/path/", } }) It("sets the signature and metadata headers", func() { Expect(request.Header.Get(route_service.RouteServiceSignature)).To(Equal("")) Expect(request.Header.Get(route_service.RouteServiceMetadata)).To(Equal(""))
conn.WriteResponse(test_util.NewResponse(http.StatusOK)) }) defer lnWithoutSlash.Close() lnWithSlash := registerHandler(r, "test/another-path/your_path/", func(conn *test_util.HttpConn) { conn.CheckLine("GET /another-path/your_path HTTP/1.1") conn.WriteResponse(test_util.NewResponse(http.StatusOK)) }) defer lnWithSlash.Close() conn := dialProxy(proxyServer) y := dialProxy(proxyServer) req := test_util.NewRequest("GET", "test", "/my%20path/your_path/", nil) conn.WriteRequest(req) resp, _ := conn.ReadResponse() Expect(resp.StatusCode).To(Equal(http.StatusOK)) req = test_util.NewRequest("GET", "test", "/another-path/your_path", nil) y.WriteRequest(req) resp, _ = y.ReadResponse() Expect(resp.StatusCode).To(Equal(http.StatusOK)) }) It("responds to http/1.0 with path/path", func() { ln := registerHandler(r, "test/my%20path/your_path", func(conn *test_util.HttpConn) { conn.CheckLine("GET /my%20path/your_path HTTP/1.1")
It("websockets do not terminate", func() { app := test.NewWebSocketApp( []route.Uri{"ws-app.vcap.me"}, config.Port, mbusClient, 1*time.Second, ) app.Listen() 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", "/chat", nil) req.Host = "ws-app.vcap.me" req.Header.Set("Upgrade", "websocket") req.Header.Set("Connection", "upgrade") x.WriteRequest(req) resp, _ := x.ReadResponse() Ω(resp.StatusCode).To(Equal(http.StatusSwitchingProtocols)) x.WriteLine("hello from client") x.CheckLine("hello from server") x.Close() }) })
jSessionIdCookie = &http.Cookie{ Name: proxy.StickyCookieKey, Value: "xxx", MaxAge: 1, Expires: time.Now(), } }) Context("first request", func() { Context("when the response does not contain a JESSIONID cookie", func() { It("does not respond with a VCAP_ID cookie", func() { ln := registerHandlerWithInstanceId(r, "app", responseNoCookies, "my-id") defer ln.Close() x := dialProxy(proxyServer) req := test_util.NewRequest("GET", "/", nil) req.Host = "app" x.WriteRequest(req) Eventually(done).Should(Receive()) resp, _ := x.ReadResponse() Ω(getCookie(proxy.VcapCookieId, resp.Cookies())).Should(BeNil()) }) }) Context("when the response contains a JESSIONID cookie", func() { It("responds with a VCAP_ID cookie scoped to the session", func() { ln := registerHandlerWithInstanceId(r, "app", responseWithJSessionID, "my-id") defer ln.Close()
Ω(body).Should(Equal("ABCD")) rsp := test_util.NewResponse(200) out := &bytes.Buffer{} out.WriteString("DEFG") rsp.Body = ioutil.NopCloser(out) x.WriteResponse(rsp) }) defer ln.Close() x := dialProxy(proxyServer) body := &bytes.Buffer{} body.WriteString("ABCD") req := test_util.NewRequest("POST", "/", ioutil.NopCloser(body)) req.Host = "test" x.WriteRequest(req) x.CheckLine("HTTP/1.1 200 OK") var payload []byte Eventually(func() int { accessLogFile.Read(&payload) return len(payload) }).ShouldNot(BeZero()) //make sure the record includes all the data //since the building of the log record happens throughout the life of the request Ω(strings.HasPrefix(string(payload), "test - [")).Should(BeTrue()) Ω(string(payload)).To(ContainSubstring(`"POST / HTTP/1.1" 200 4 4 "-"`))