commit ee5f4558af82365a512e4283625aa2ea6d2722df
Author: Serene Han <[email protected]>
Date:   Mon Feb 15 11:31:57 2016 -0800

    Second async test for Broker's proxy answer handler
---
 broker/broker.go                |  2 +-
 broker/snowflake-broker_test.go | 43 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/broker/broker.go b/broker/broker.go
index d9aa156..7c14f63 100644
--- a/broker/broker.go
+++ b/broker/broker.go
@@ -210,7 +210,7 @@ func answerHandler(ctx *BrokerContext, w 
http.ResponseWriter, r *http.Request) {
                return
        }
        body, err := ioutil.ReadAll(r.Body)
-       if nil != err {
+       if nil != err || nil == body || len(body) <= 0 {
                log.Println("Invalid data.")
                w.WriteHeader(http.StatusBadRequest)
                return
diff --git a/broker/snowflake-broker_test.go b/broker/snowflake-broker_test.go
index 1d41a27..06d2316 100644
--- a/broker/snowflake-broker_test.go
+++ b/broker/snowflake-broker_test.go
@@ -7,6 +7,7 @@ import (
        "net/http"
        "net/http/httptest"
        "testing"
+       "fmt"
 )
 
 func TestBroker(t *testing.T) {
@@ -25,7 +26,7 @@ func TestBroker(t *testing.T) {
                Convey("Responds to client offers...", func() {
                        w := httptest.NewRecorder()
                        data := bytes.NewReader([]byte("test"))
-                       r, err := http.NewRequest("POST", "broker.com/client", 
data)
+                       r, err := http.NewRequest("POST", 
"snowflake.broker/client", data)
                        So(err, ShouldBeNil)
 
                        Convey("with 503 when no snowflakes are available.", 
func() {
@@ -73,7 +74,7 @@ func TestBroker(t *testing.T) {
                        done := make(chan bool)
                        w := httptest.NewRecorder()
                        data := bytes.NewReader([]byte("test"))
-                       r, err := http.NewRequest("POST", "broker.com/proxy", 
data)
+                       r, err := http.NewRequest("POST", 
"snowflake.broker/proxy", data)
                        r.Header.Set("X-Session-ID", "test")
                        So(err, ShouldBeNil)
 
@@ -105,6 +106,44 @@ func TestBroker(t *testing.T) {
                                So(w.Code, ShouldEqual, 
http.StatusGatewayTimeout)
                        })
                })
+
+               Convey("Responds to proxy answers...", func() { 
+                       w := httptest.NewRecorder()
+                       data := bytes.NewReader([]byte("fake answer"))
+                       s := ctx.AddSnowflake("test")
+
+                       Convey("by passing to the client if valid.", func() {
+                               r, err := http.NewRequest("POST", 
"snowflake.broker/answer", data)
+                               So(err, ShouldBeNil)
+                               r.Header.Set("X-Session-ID", "test")
+                               go func(ctx *BrokerContext) {
+                                       answerHandler(ctx, w, r)
+                               }(ctx)
+                               answer := <- s.answerChannel
+                               So(w.Code, ShouldEqual, http.StatusOK)
+                               So(answer, ShouldResemble, []byte("fake 
answer"))
+                       })
+
+                       Convey("with error if the proxy is not recognized", 
func() {
+                               r, err := http.NewRequest("POST", 
"snowflake.broker/answer", nil)
+                               So(err, ShouldBeNil)
+                               r.Header.Set("X-Session-ID", "invalid")
+                               answerHandler(ctx, w, r)
+                               So(w.Code, ShouldEqual, http.StatusGone)
+                               fmt.Println("omg")
+                       })
+
+                       Convey("with error if the proxy gives invalid answer", 
func() {
+                               data := bytes.NewReader(nil)
+                               r, err := http.NewRequest("POST", 
"snowflake.broker/answer", data)
+                               r.Header.Set("X-Session-ID", "test")
+                               So(err, ShouldBeNil)
+                               answerHandler(ctx, w, r)
+                               So(w.Code, ShouldEqual, http.StatusBadRequest)
+                       })
+
+               })
+
        })
 }
 



_______________________________________________
tor-commits mailing list
[email protected]
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to