Example #1
0
func ContainerMetricHandlerProvider(messages <-chan []byte, logger *gosteno.Logger) http.Handler {
	outputChan := DeDupe(messages)
	return handlers.NewHttpHandler(outputChan, logger)
}
Example #2
0
			receivedLogMessages, recentError = connection.Recent(appGuid, authToken)
		}

		Context("when the connection cannot be established", func() {
			It("invalid endpoints return error", func() {
				endpoint = "invalid-endpoint"
				perform()

				Expect(recentError).ToNot(BeNil())
			})
		})

		Context("when the connection can be established", func() {

			BeforeEach(func() {
				testServer = httptest.NewServer(handlers.NewHttpHandler(messagesToSend))
				endpoint = "ws://" + testServer.Listener.Addr().String()
			})

			It("returns messages from the server", func() {
				messagesToSend <- marshalMessage(createMessage("test-message-0", 0))
				messagesToSend <- marshalMessage(createMessage("test-message-1", 0))

				perform()

				Expect(recentError).NotTo(HaveOccurred())
				Expect(receivedLogMessages).To(HaveLen(2))
				Expect(receivedLogMessages[0].Message).To(Equal([]byte("test-message-0")))
				Expect(receivedLogMessages[1].Message).To(Equal([]byte("test-message-1")))
			})
		})
			Expect(err.Error()).To(ContainSubstring(http.StatusText(http.StatusBadRequest)))
		})
	})

	Describe("RecentLogs", func() {
		var httpTestServer *httptest.Server
		var incomingMessages []*events.LogMessage

		perform := func() {
			close(messagesToSend)
			connection = noaa.NewConsumer(endpoint, tlsSettings, consumerProxyFunc)
			incomingMessages, err = connection.RecentLogs(appGuid, authToken)
		}

		BeforeEach(func() {
			httpTestServer = httptest.NewServer(handlers.NewHttpHandler(messagesToSend, loggertesthelper.Logger()))
			endpoint = "ws://" + httpTestServer.Listener.Addr().String()
		})

		AfterEach(func() {
			httpTestServer.Close()
		})

		It("returns messages from the server", func() {
			messagesToSend <- marshalMessage(createMessage("test-message-0", 0))
			messagesToSend <- marshalMessage(createMessage("test-message-1", 0))

			perform()

			Expect(err).NotTo(HaveOccurred())
			Expect(incomingMessages).To(HaveLen(2))
Example #4
0
func HttpHandlerProvider(messages <-chan []byte, logger *gosteno.Logger) http.Handler {
	return handlers.NewHttpHandler(messages, logger)
}
Example #5
0
			receivedLogMessages, recentError = connection.Recent(appGuid, authToken)
		}

		Context("when the connection cannot be established", func() {
			It("invalid endpoints return error", func() {
				endpoint = "invalid-endpoint"
				perform()

				Expect(recentError).ToNot(BeNil())
			})
		})

		Context("when the connection can be established", func() {

			BeforeEach(func() {
				testServer = httptest.NewServer(handlers.NewHttpHandler(messagesToSend, loggertesthelper.Logger()))
				endpoint = "ws://" + testServer.Listener.Addr().String()
			})

			It("returns messages from the server", func() {
				messagesToSend <- marshalMessage(createMessage("test-message-0", 0))
				messagesToSend <- marshalMessage(createMessage("test-message-1", 0))

				perform()

				Expect(recentError).NotTo(HaveOccurred())
				Expect(receivedLogMessages).To(HaveLen(2))
				Expect(receivedLogMessages[0].Message).To(Equal([]byte("test-message-0")))
				Expect(receivedLogMessages[1].Message).To(Equal([]byte("test-message-1")))
			})
		})
Example #6
0
			connection = noaa.NewConsumer(trafficControllerUrl, nil, nil)
			receivedLogMessages, recentError = connection.RecentLogs(appGuid, authToken)
		}

		Context("when the connection cannot be established", func() {
			It("invalid urls return error", func() {
				trafficControllerUrl = "invalid-url"
				perform()

				Expect(recentError).ToNot(BeNil())
			})
		})

		Context("when the connection can be established", func() {
			BeforeEach(func() {
				testServer = httptest.NewServer(handlers.NewHttpHandler(messagesToSend, loggertesthelper.Logger()))
				trafficControllerUrl = "ws://" + testServer.Listener.Addr().String()
			})

			It("returns messages from the server", func() {
				messagesToSend <- marshalMessage(createMessage("test-message-0", 0))
				messagesToSend <- marshalMessage(createMessage("test-message-1", 0))

				perform()

				Expect(recentError).NotTo(HaveOccurred())
				Expect(receivedLogMessages).To(HaveLen(2))
				Expect(receivedLogMessages[0].GetMessage()).To(Equal([]byte("test-message-0")))
				Expect(receivedLogMessages[1].GetMessage()).To(Equal([]byte("test-message-1")))
			})
		})
		It("sets required CORS headers", func() {
			req, _ := http.NewRequest("POST", "/set-cookie", nil)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Origin", "fake-origin-string")

			proxy.ServeHTTP(recorder, req)

			Expect(recorder.Header().Get("Access-Control-Allow-Origin")).To(Equal("fake-origin-string"))
			Expect(recorder.Header().Get("Access-Control-Allow-Credentials")).To(Equal("true"))
		})
	})
})

var _ = Describe("DefaultHandlerProvider", func() {
	It("returns an HTTP handler for .../recentlogs", func() {
		httpHandler := handlers.NewHttpHandler(make(chan []byte), loggertesthelper.Logger())

		target := doppler_endpoint.HttpHandlerProvider(make(chan []byte), loggertesthelper.Logger())

		Expect(target).To(BeAssignableToTypeOf(httpHandler))
	})

	It("returns a Websocket handler for .../stream", func() {
		wsHandler := handlers.NewWebsocketHandler(make(chan []byte), time.Minute, loggertesthelper.Logger())

		target := doppler_endpoint.WebsocketHandlerProvider(make(chan []byte), loggertesthelper.Logger())

		Expect(target).To(BeAssignableToTypeOf(wsHandler))
	})

	It("returns a Websocket handler for anything else", func() {
	"github.com/cloudfoundry/dropsonde/emitter"
	"github.com/cloudfoundry/dropsonde/factories"
	"github.com/cloudfoundry/loggregatorlib/server/handlers"
	"github.com/gogo/protobuf/proto"
	"time"
	"trafficcontroller/doppler_endpoint"

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

var _ = Describe("NewDopplerEndpoint", func() {
	Context("when endpoint is 'recentlogs'", func() {
		It("uses an HTTP handler", func() {
			dopplerEndpoint := doppler_endpoint.NewDopplerEndpoint("recentlogs", "abc123", true)
			knownHttpHandler := handlers.NewHttpHandler(nil, nil)
			Expect(dopplerEndpoint.HProvider(nil, nil)).To(BeAssignableToTypeOf(knownHttpHandler))
		})

		It("sets a timeout of five seconds", func() {
			dopplerEndpoint := doppler_endpoint.NewDopplerEndpoint("recentlogs", "abc123", true)
			Expect(dopplerEndpoint.Timeout).To(Equal(5 * time.Second))
		})
	})

	Context("when endpoint is 'containermetrics'", func() {
		It("uses an HTTP Container metrics handler", func() {
			dopplerEndpoint := doppler_endpoint.NewDopplerEndpoint("containermetrics", "abc123", true)
			handler := handlers.NewHttpHandler(nil, nil)
			closedChan := make(chan []byte)
			close(closedChan)
	"github.com/cloudfoundry/loggregatorlib/loggertesthelper"
	"github.com/cloudfoundry/loggregatorlib/server/handlers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("HttpHandler", func() {
	var boundaryRegexp = regexp.MustCompile("boundary=(.*)")
	var handler http.Handler
	var fakeResponseWriter *httptest.ResponseRecorder
	var messagesChan chan []byte

	BeforeEach(func() {
		fakeResponseWriter = httptest.NewRecorder()
		messagesChan = make(chan []byte, 10)
		handler = handlers.NewHttpHandler(messagesChan, loggertesthelper.Logger())
	})

	It("grabs recent logs and creates a multi-part HTTP response", func(done Done) {
		r, _ := http.NewRequest("GET", "ws://loggregator.place/dump/?app=abc-123", nil)

		for i := 0; i < 5; i++ {
			messagesChan <- []byte("message")
		}

		close(messagesChan)
		handler.ServeHTTP(fakeResponseWriter, r)

		matches := boundaryRegexp.FindStringSubmatch(fakeResponseWriter.Header().Get("Content-Type"))
		Expect(matches).To(HaveLen(2))
		Expect(matches[1]).NotTo(BeEmpty())
Example #10
0
	hashers   []hasher.Hasher
	logger    *gosteno.Logger
	authorize authorization.LogAccessAuthorizer
	listener  net.Listener
}

type WebsocketHandler interface {
	HandleWebSocket(string, string, []hasher.Hasher)
}

var NewWebsocketHandlerProvider = func(messages <-chan []byte) http.Handler {
	return handlers.NewWebsocketHandler(messages, 30*time.Second)
}

var NewHttpHandlerProvider = func(messages <-chan []byte) http.Handler {
	return handlers.NewHttpHandler(messages)
}

var NewWebsocketListener = func() listener.Listener {
	return listener.NewWebsocket()
}

func NewProxy(hashers []hasher.Hasher, authorizer authorization.LogAccessAuthorizer, logger *gosteno.Logger) *Proxy {
	return &Proxy{hashers: hashers, authorize: authorizer, logger: logger}
}

func (proxy *Proxy) isAuthorized(appId, authToken string, clientAddress string) (bool, *logmessage.LogMessage) {
	newLogMessage := func(message []byte) *logmessage.LogMessage {
		currentTime := time.Now()
		messageType := logmessage.LogMessage_ERR
Example #11
0
			Expect(err.Error()).To(ContainSubstring(http.StatusText(http.StatusBadRequest)))
		})
	})

	Describe("Recent", func() {
		var httpTestServer *httptest.Server
		var incomingMessages []*logmessage.LogMessage

		perform := func() {
			close(messagesToSend)
			connection = consumer.New(endpoint, tlsSettings, consumerProxyFunc)
			incomingMessages, err = connection.Recent(appGuid, authToken)
		}

		BeforeEach(func() {
			httpTestServer = httptest.NewServer(handlers.NewHttpHandler(messagesToSend))
			endpoint = "ws://" + httpTestServer.Listener.Addr().String()
		})

		AfterEach(func() {
			httpTestServer.Close()
		})

		It("returns messages from the server", func() {
			messagesToSend <- marshalMessage(createMessage("test-message-0", 0))
			messagesToSend <- marshalMessage(createMessage("test-message-1", 0))

			perform()

			Expect(err).NotTo(HaveOccurred())
			Expect(incomingMessages).To(HaveLen(2))