Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
[#69457338] Use HttpHandler for /recent
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Jackson <ajackson@pivotallabs.com>
  • Loading branch information
John Tuley authored and Alex Jackson committed Apr 28, 2014
1 parent 9beb8be commit 8a44372
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/trafficcontroller/output_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type WebsocketHandler interface {
HandleWebSocket(string, string, []*hasher.Hasher)
}

var NewProxyHandlerProvider func(*websocket.Conn, *gosteno.Logger) WebsocketHandler
var NewProxyHandlerProvider func(*websocket.Conn, *gosteno.Logger) WebsocketHandler = func(ws *websocket.Conn, logger *gosteno.Logger) WebsocketHandler {
return NewProxyHandler(ws, logger)
}

func init() {
NewProxyHandlerProvider = func(ws *websocket.Conn, logger *gosteno.Logger) WebsocketHandler {
return NewProxyHandler(ws, logger)
}
var NewHttpHandlerProvider func(hashers []*hasher.Hasher, logger *gosteno.Logger) http.Handler = func(hashers []*hasher.Hasher, logger *gosteno.Logger) http.Handler {
return NewHttpHandler(hashers, logger)
}

func NewProxy(host string, hashers []*hasher.Hasher, authorizer authorization.LogAccessAuthorizer, logger *gosteno.Logger) *Proxy {
Expand Down Expand Up @@ -113,6 +113,13 @@ func (proxy *Proxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "You are not authorized. %s", errorMessage)
return
}

if recentViaHttp(r) {
h := NewHttpHandlerProvider(proxy.hashers, proxy.logger)
h.ServeHTTP(rw, r)
return
}

ws := upgrade(rw, r)
if ws == nil {
return
Expand All @@ -132,3 +139,7 @@ func extractAuthTokenFromUrl(u *url.URL) string {
}
return authorization
}

func recentViaHttp(r *http.Request) bool {
return r.URL.Path == "/recent"
}
34 changes: 33 additions & 1 deletion src/trafficcontroller/output_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,23 @@ func (f *fakeProxyHandler) CallParams() [][]interface{} {
copy(copyOfParams, f.callParams)
return copyOfParams
}

func (f *fakeProxyHandler) HandleWebSocket(appid, requestUri string, hashers []*hasher.Hasher) {
f.Lock()
defer f.Unlock()
f.callParams = append(f.callParams, []interface{}{appid, requestUri, hashers})
f.WebsocketConnection.Close()
}

var _ = Describe("OutPutProxy", func() {
type fakeHttpHandler struct {
called bool
}

func (f *fakeHttpHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
f.called = true
}

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

var fph *fakeProxyHandler
var hashers []*hasher.Hasher
Expand All @@ -47,6 +56,7 @@ var _ = Describe("OutPutProxy", func() {
fph.WebsocketConnection = ws
return fph
}

hashers = []*hasher.Hasher{hasher.NewHasher([]string{"localhost:62038"})}
proxy := trafficcontroller.NewProxy(
"localhost:"+PORT,
Expand Down Expand Up @@ -106,6 +116,28 @@ var _ = Describe("OutPutProxy", func() {
})
})

Context("/recent", func() {
var fhh *fakeHttpHandler

BeforeEach(func() {
fhh = &fakeHttpHandler{}

trafficcontroller.NewHttpHandlerProvider = func(hashers []*hasher.Hasher, logger *gosteno.Logger) http.Handler {
return fhh
}
})

It("should use HttpHandler instead of ProxyHandler", func() {
url := "http://localhost:" + PORT + "/recent?app=myApp"
r, _ := http.NewRequest("GET", url, nil)
r.Header = http.Header{"Authorization": []string{testhelpers.VALID_AUTHENTICATION_TOKEN}}
client := &http.Client{}
_, err := client.Do(r)

Expect(err).NotTo(HaveOccurred())
Expect(fhh.called).To(BeTrue())
})
})
})

func assertAuthorizationError(resp *http.Response, err error, msg string) {
Expand Down

0 comments on commit 8a44372

Please sign in to comment.