Fix jasmine spec for broker async request

This commit is contained in:
Serene Han 2016-02-09 18:13:54 -08:00
parent 548c100160
commit bb9eb721e2
2 changed files with 50 additions and 33 deletions

View file

@ -32,23 +32,13 @@ class Broker
# Snowflake registers with the broker using an HTTP POST request, and expects # Snowflake registers with the broker using an HTTP POST request, and expects
# a response from the broker containing some client offer. # a response from the broker containing some client offer.
# TODO: Actually support multiple clients. # TODO: Actually support multiple clients.
getClientOffer: -> getClientOffer: =>
new Promise (fulfill, reject) => new Promise (fulfill, reject) =>
xhr = new XMLHttpRequest() xhr = new XMLHttpRequest()
@request = xhr @request = xhr
try @fulfill = fulfill
xhr.open 'POST', @url + 'proxy' # @request.onreadystatechange = @processOffer
xhr.setRequestHeader('X-Session-ID', @id) xhr.onreadystatechange = =>
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 = ->
return if xhr.DONE != xhr.readyState return if xhr.DONE != xhr.readyState
switch xhr.status switch xhr.status
when STATUS_OK when STATUS_OK
@ -59,7 +49,22 @@ class Broker
log 'Broker ERROR: Unexpected ' + xhr.status + log 'Broker ERROR: Unexpected ' + xhr.status +
' - ' + xhr.statusText ' - ' + xhr.statusText
Status.set ' failure. Please refresh.' 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) -> sendAnswer: (answer) ->
dbg @id + ' - Sending answer back to broker...\n' dbg @id + ' - Sending answer back to broker...\n'

View file

@ -3,36 +3,48 @@ jasmine tests for Snowflake broker
### ###
# fake xhr # fake xhr
# class XMLHttpRequest
class XMLHttpRequest class XMLHttpRequest
constructor: ->
@onreadystatechange = null
open: -> open: ->
setRequestHeader: ->
send: -> send: ->
onreadystatechange: ->
DONE: 1 DONE: 1
describe 'Broker', -> describe 'Broker', ->
it 'can be created', -> it 'can be created', ->
b = new Broker('fake') b = new Broker 'fake'
expect(b.url).toEqual 'https://fake/' expect(b.url).toEqual 'https://fake/'
expect(b.id).not.toBeNull() expect(b.id).not.toBeNull()
it 'polls for client offer', (done) -> it 'polls and promises a client offer', (done) ->
b = new Broker('fake') b = new Broker 'fake'
# TODO: fix this # fake successful request
poll = b.getClientOffer() spyOn(b, 'sendRequest').and.callFake ->
spyOn(b.request, 'open') b.request.readyState = b.request.DONE
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.status = STATUS_OK
b.request.responseText = 'test' b.request.responseText = 'test'
b.request.onreadystatechange()
poll = b.getClientOffer()
expect(poll).not.toBeNull()
poll.then (desc) =>
expect(desc).toEqual 'test'
done() 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', -> it 'responds to the broker with answer', ->
# TODO # TODO: fix
b = new Broker 'fake'
b.sendAnswer 'foo'