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
|
# 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'
|
||||||
|
|
|
@ -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
|
||||||
|
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()
|
poll = b.getClientOffer()
|
||||||
spyOn(b.request, 'open')
|
|
||||||
spyOn(b.request, 'send').and.callFake ->
|
|
||||||
b.onreadystatechange()
|
|
||||||
poll.then = ->
|
|
||||||
done()
|
|
||||||
expect(poll).not.toBeNull()
|
expect(poll).not.toBeNull()
|
||||||
# expect(b.request.open).toHaveBeenCalled()
|
poll.then (desc) =>
|
||||||
# expect(b.request.send).toHaveBeenCalled()
|
expect(desc).toEqual 'test'
|
||||||
# fake successful poll
|
done()
|
||||||
b.request.readyState = XMLHttpRequest.DONE
|
|
||||||
b.request.status = STATUS_OK
|
it 'requests correctly', ->
|
||||||
b.request.responseText = 'test'
|
b = new Broker 'fake'
|
||||||
done()
|
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'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue