move CORS early return into the ServeHTTP wrapper, rename handlers

This commit is contained in:
Serene Han 2016-02-16 21:00:30 -08:00
parent 791f6925ec
commit 2ae6559001
2 changed files with 103 additions and 112 deletions

View file

@ -16,24 +16,26 @@ func TestBroker(t *testing.T) {
Convey("Adds Snowflake", func() {
So(ctx.snowflakes.Len(), ShouldEqual, 0)
So(len(ctx.snowflakeMap), ShouldEqual, 0)
So(len(ctx.idToSnowflake), ShouldEqual, 0)
ctx.AddSnowflake("foo")
So(ctx.snowflakes.Len(), ShouldEqual, 1)
So(len(ctx.snowflakeMap), ShouldEqual, 1)
So(len(ctx.idToSnowflake), ShouldEqual, 1)
})
/*
Convey("Broker goroutine matches clients with proxies", func() {
ctx2 := NewBrokerContext()
p := new(ProxyPoll)
p.id = "test"
p.offerChannel = make(chan []byte)
go func() {
ctx.proxyPolls <- p
close(ctx.proxyPolls)
ctx2.proxyPolls <- p
close(ctx2.proxyPolls)
}()
ctx.Broker()
So(ctx.snowflakes.Len(), ShouldEqual, 1)
So(ctx.snowflakes.Len(), ShouldEqual, 1)
ctx2.Broker()
So(ctx2.snowflakes.Len(), ShouldEqual, 1)
So(ctx2.idToSnowflake["test"], ShouldNotBeNil)
})
*/
Convey("Responds to client offers...", func() {
w := httptest.NewRecorder()
@ -42,9 +44,7 @@ func TestBroker(t *testing.T) {
So(err, ShouldBeNil)
Convey("with 503 when no snowflakes are available.", func() {
clientHandler(ctx, w, r)
h := w.Header()
So(h["Access-Control-Allow-Headers"], ShouldNotBeNil)
clientOffers(ctx, w, r)
So(w.Code, ShouldEqual, http.StatusServiceUnavailable)
So(w.Body.String(), ShouldEqual, "")
})
@ -54,7 +54,7 @@ func TestBroker(t *testing.T) {
// Prepare a fake proxy to respond with.
snowflake := ctx.AddSnowflake("fake")
go func() {
clientHandler(ctx, w, r)
clientOffers(ctx, w, r)
done <- true
}()
offer := <-snowflake.offerChannel
@ -72,7 +72,8 @@ func TestBroker(t *testing.T) {
done := make(chan bool)
snowflake := ctx.AddSnowflake("fake")
go func() {
clientHandler(ctx, w, r)
clientOffers(ctx, w, r)
// Takes a few seconds here...
done <- true
}()
offer := <-snowflake.offerChannel
@ -92,7 +93,7 @@ func TestBroker(t *testing.T) {
Convey("with a client offer if available.", func() {
go func(ctx *BrokerContext) {
proxyHandler(ctx, w, r)
proxyPolls(ctx, w, r)
done <- true
}(ctx)
// Pass a fake client offer to this proxy
@ -106,7 +107,7 @@ func TestBroker(t *testing.T) {
Convey("times out when no client offer is available.", func() {
go func(ctx *BrokerContext) {
proxyHandler(ctx, w, r)
proxyPolls(ctx, w, r)
done <- true
}(ctx)
p := <-ctx.proxyPolls
@ -129,7 +130,7 @@ func TestBroker(t *testing.T) {
So(err, ShouldBeNil)
r.Header.Set("X-Session-ID", "test")
go func(ctx *BrokerContext) {
answerHandler(ctx, w, r)
proxyAnswers(ctx, w, r)
}(ctx)
answer := <-s.answerChannel
So(w.Code, ShouldEqual, http.StatusOK)
@ -140,7 +141,7 @@ func TestBroker(t *testing.T) {
r, err := http.NewRequest("POST", "snowflake.broker/answer", nil)
So(err, ShouldBeNil)
r.Header.Set("X-Session-ID", "invalid")
answerHandler(ctx, w, r)
proxyAnswers(ctx, w, r)
So(w.Code, ShouldEqual, http.StatusGone)
})
@ -149,7 +150,7 @@ func TestBroker(t *testing.T) {
r, err := http.NewRequest("POST", "snowflake.broker/answer", data)
r.Header.Set("X-Session-ID", "test")
So(err, ShouldBeNil)
answerHandler(ctx, w, r)
proxyAnswers(ctx, w, r)
So(w.Code, ShouldEqual, http.StatusBadRequest)
})
})
@ -167,7 +168,7 @@ func TestBroker(t *testing.T) {
So(err, ShouldBeNil)
rP.Header.Set("X-Session-ID", "test")
go func() {
proxyHandler(ctx, wP, rP)
proxyPolls(ctx, wP, rP)
polled <- true
}()
@ -179,7 +180,7 @@ func TestBroker(t *testing.T) {
offer := <-s.offerChannel
p.offerChannel <- offer
}()
So(ctx.snowflakeMap["test"], ShouldNotBeNil)
So(ctx.idToSnowflake["test"], ShouldNotBeNil)
// Client request blocks until proxy answer arrives.
dataC := bytes.NewReader([]byte("fake offer"))
@ -187,21 +188,21 @@ func TestBroker(t *testing.T) {
rC, err := http.NewRequest("POST", "snowflake.broker/client", dataC)
So(err, ShouldBeNil)
go func() {
clientHandler(ctx, wC, rC)
clientOffers(ctx, wC, rC)
done <- true
}()
<-polled
So(wP.Code, ShouldEqual, http.StatusOK)
So(wP.Body.String(), ShouldResemble, "fake offer")
So(ctx.snowflakeMap["test"], ShouldNotBeNil)
So(ctx.idToSnowflake["test"], ShouldNotBeNil)
// Follow up with the answer request afterwards
wA := httptest.NewRecorder()
dataA := bytes.NewReader([]byte("fake answer"))
rA, err := http.NewRequest("POST", "snowflake.broker/proxy", dataA)
So(err, ShouldBeNil)
rA.Header.Set("X-Session-ID", "test")
answerHandler(ctx, wA, rA)
proxyAnswers(ctx, wA, rA)
So(wA.Code, ShouldEqual, http.StatusOK)
<-done