[tor-commits] [snowflake/master] Fix jasmine spec for broker async request

serene at torproject.org serene at torproject.org
Wed Feb 10 22:04:10 UTC 2016


commit bb9eb721e26f6809cc596746112298c646b74775
Author: Serene Han <keroserene+git at gmail.com>
Date:   Tue Feb 9 18:13:54 2016 -0800

    Fix jasmine spec for broker async request
---
 proxy/broker.coffee           | 35 +++++++++++++++++--------------
 proxy/spec/broker.spec.coffee | 48 +++++++++++++++++++++++++++----------------
 2 files changed, 50 insertions(+), 33 deletions(-)

diff --git a/proxy/broker.coffee b/proxy/broker.coffee
index b196df5..71fe6eb 100644
--- a/proxy/broker.coffee
+++ b/proxy/broker.coffee
@@ -32,23 +32,13 @@ class Broker
   # Snowflake registers with the broker using an HTTP POST request, and expects
   # a response from the broker containing some client offer.
   # TODO: Actually support multiple clients.
-  getClientOffer: ->
+  getClientOffer: =>
     new Promise (fulfill, reject) =>
       xhr = new XMLHttpRequest()
       @request = xhr
-      try
-        xhr.open 'POST', @url + 'proxy'
-        xhr.setRequestHeader('X-Session-ID', @id)
-      catch err
-        ###
-        An exception happens here when, for example, NoScript allows the domain
-        on which the proxy badge runs, but not the domain to which it's trying
-        to make the HTTP request. The exception message is like "Component
-        returned failure code: 0x805e0006 [nsIXMLHttpRequest.open]" on Firefox.
-        ###
-        log 'Broker: exception while connecting: ' + err.message
-        return
-      xhr.onreadystatechange = ->
+      @fulfill = fulfill
+      # @request.onreadystatechange = @processOffer
+      xhr.onreadystatechange = =>
         return if xhr.DONE != xhr.readyState
         switch xhr.status
           when STATUS_OK
@@ -59,7 +49,22 @@ class Broker
             log 'Broker ERROR: Unexpected ' + xhr.status +
                 ' - ' + xhr.statusText
             Status.set ' failure. Please refresh.'
-      xhr.send @id
+      @sendRequest()
+
+  sendRequest: =>
+    try
+      @request.open 'POST', @url + 'proxy'
+      @request.setRequestHeader('X-Session-ID', @id)
+    catch err
+      ###
+      An exception happens here when, for example, NoScript allows the domain
+      on which the proxy badge runs, but not the domain to which it's trying
+      to make the HTTP request. The exception message is like "Component
+      returned failure code: 0x805e0006 [nsIXMLHttpRequest.open]" on Firefox.
+      ###
+      log 'Broker: exception while connecting: ' + err.message
+      return
+    @request.send @id
 
   sendAnswer: (answer) ->
     dbg @id + ' - Sending answer back to broker...\n'
diff --git a/proxy/spec/broker.spec.coffee b/proxy/spec/broker.spec.coffee
index 2844cdb..3b47cd3 100644
--- a/proxy/spec/broker.spec.coffee
+++ b/proxy/spec/broker.spec.coffee
@@ -3,36 +3,48 @@ jasmine tests for Snowflake broker
 ###
 
 # fake xhr
+# class XMLHttpRequest
 class XMLHttpRequest
+  constructor: ->
+    @onreadystatechange = null
   open: ->
+  setRequestHeader: ->
   send: ->
-  onreadystatechange: ->
   DONE: 1
 
 describe 'Broker', ->
 
   it 'can be created', ->
-    b = new Broker('fake')
+    b = new Broker 'fake'
     expect(b.url).toEqual 'https://fake/'
     expect(b.id).not.toBeNull()
 
-  it 'polls for client offer', (done) ->
-    b = new Broker('fake')
-    # TODO: fix this
+  it 'polls and promises a client offer', (done) ->
+    b = new Broker 'fake'
+    # fake successful request
+    spyOn(b, 'sendRequest').and.callFake ->
+      b.request.readyState = b.request.DONE
+      b.request.status = STATUS_OK
+      b.request.responseText = 'test'
+      b.request.onreadystatechange()
     poll = b.getClientOffer()
-    spyOn(b.request, 'open')
-    spyOn(b.request, 'send').and.callFake ->
-      b.onreadystatechange()
-    poll.then = ->
-      done()
     expect(poll).not.toBeNull()
-    # expect(b.request.open).toHaveBeenCalled()
-    # expect(b.request.send).toHaveBeenCalled()
-    # fake successful poll
-    b.request.readyState = XMLHttpRequest.DONE
-    b.request.status = STATUS_OK
-    b.request.responseText = 'test'
-    done()
+    poll.then (desc) =>
+      expect(desc).toEqual 'test'
+      done()
+
+  it 'requests correctly', ->
+    b = new Broker 'fake'
+    b.request = new XMLHttpRequest()
+    spyOn(b.request, 'open')
+    spyOn(b.request, 'setRequestHeader')
+    spyOn(b.request, 'send')
+    b.sendRequest()
+    expect(b.request.open).toHaveBeenCalled()
+    expect(b.request.setRequestHeader).toHaveBeenCalled()
+    expect(b.request.send).toHaveBeenCalled()
 
   it 'responds to the broker with answer', ->
-    # TODO
+    # TODO: fix
+    b = new Broker 'fake'
+    b.sendAnswer 'foo'





More information about the tor-commits mailing list