mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Fix jasmine spec for broker async request
This commit is contained in:
parent
548c100160
commit
bb9eb721e2
2 changed files with 50 additions and 33 deletions
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue