commit 5aae41c9016cb79ee4f7ccb9830ef0d08ed0ee2a Author: Serene Han keroserene+git@gmail.com Date: Sat Feb 27 14:45:53 2016 -0800
rendezvous / BrokerChannel tests for client --- client/client_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ client/rendezvous.go | 12 ++++++----- 2 files changed, 63 insertions(+), 5 deletions(-)
diff --git a/client/client_test.go b/client/client_test.go index 9acf6fc..a3fe078 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -4,6 +4,10 @@ import ( "bytes" "github.com/keroserene/go-webrtc" . "github.com/smartystreets/goconvey/convey" + "net/http" + // "net/http/httptest" + "io/ioutil" + "strings" "testing" )
@@ -21,6 +25,29 @@ func (*MockDataChannel) Close() error { return nil }
+type MockResponse struct{} + +func (m *MockResponse) Read(p []byte) (int, error) { + p = []byte(`{"type":"answer","sdp":"fake"}`) + return 0, nil +} +func (m *MockResponse) Close() error { + return nil +} + +type MockTransport struct { +} + +// Just returns a response with fake SDP answer. +func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { + s := ioutil.NopCloser(strings.NewReader(`{"type":"answer","sdp":"fake"}`)) + r := &http.Response{ + StatusCode: http.StatusOK, + Body: s, + } + return r, nil +} + func TestConnect(t *testing.T) { Convey("Snowflake", t, func() {
@@ -72,6 +99,35 @@ func TestConnect(t *testing.T) { // TODO }) }) + }) + + Convey("Rendezvous", t, func() { + + Convey("BrokerChannel with no front domain", func() { + b := NewBrokerChannel("test.broker", "") + So(b.url, ShouldNotBeNil) + So(b.url.Path, ShouldResemble, "test.broker") + So(b.transport, ShouldNotBeNil) + }) + + Convey("BrokerChannel with front domain", func() { + b := NewBrokerChannel("test.broker", "front") + So(b.url, ShouldNotBeNil) + So(b.url.Path, ShouldResemble, "test.broker") + So(b.url.Host, ShouldResemble, "front") + So(b.transport, ShouldNotBeNil) + }) + + Convey("BrokerChannel Negotiate responds with answer", func() { + b := NewBrokerChannel("test.broker", "") + sdp := webrtc.DeserializeSessionDescription("test") + // Replace transport with a mock. + b.transport = &MockTransport{} + answer, err := b.Negotiate(sdp) + So(err, ShouldBeNil) + So(answer, ShouldNotBeNil) + So(answer.Sdp, ShouldResemble, "fake") + })
}) } diff --git a/client/rendezvous.go b/client/rendezvous.go index fe75b09..29969f0 100644 --- a/client/rendezvous.go +++ b/client/rendezvous.go @@ -18,9 +18,10 @@ import ( type BrokerChannel struct { // The Host header to put in the HTTP request (optional and may be // different from the host name in URL). - Host string - url *url.URL - transport http.Transport // Used to make all requests. + Host string + url *url.URL + // transport http.Transport // Used to make all requests. + transport http.RoundTripper // Used to make all requests. }
// Construct a new BrokerChannel, where: @@ -41,8 +42,9 @@ func NewBrokerChannel(broker string, front string) *BrokerChannel { // We make a copy of DefaultTransport because we want the default Dial // and TLSHandshakeTimeout settings. But we want to disable the default // ProxyFromEnvironment setting. - bc.transport = *http.DefaultTransport.(*http.Transport) - bc.transport.Proxy = nil + transport := http.DefaultTransport.(*http.Transport) + transport.Proxy = nil + bc.transport = transport return bc }
tor-commits@lists.torproject.org