Fix the ProxyPair tests exposed by the previous commit.

This was mainly a matter of more complete mocking.
This commit is contained in:
David Fifield 2018-12-04 15:07:42 -07:00
parent fce32bf292
commit 3cd8519ec9
2 changed files with 33 additions and 8 deletions

View file

@ -7,24 +7,32 @@ describe 'ProxyPair', ->
fakeRelay = Parse.address '0.0.0.0:12345' fakeRelay = Parse.address '0.0.0.0:12345'
rateLimit = new DummyRateLimit() rateLimit = new DummyRateLimit()
destination = [] destination = []
# Using the mock PeerConnection definition from spec/snowflake.spec.coffee.
pp = new ProxyPair(fakeRelay, rateLimit) pp = new ProxyPair(fakeRelay, rateLimit)
it 'begins webrtc connection', -> beforeEach ->
pp.begin() pp.begin()
it 'begins webrtc connection', ->
expect(pp.pc).not.toBeNull() expect(pp.pc).not.toBeNull()
describe 'accepts WebRTC offer from some client', -> describe 'accepts WebRTC offer from some client', ->
beforeEach ->
pp.begin()
it 'rejects invalid offers', -> it 'rejects invalid offers', ->
expect(typeof(pp.pc.setRemoteDescription)).toBe("function")
expect(pp.pc).not.toBeNull()
expect(pp.receiveWebRTCOffer {}).toBe false expect(pp.receiveWebRTCOffer {}).toBe false
expect pp.receiveWebRTCOffer { expect(pp.receiveWebRTCOffer {
type: 'answer' type: 'answer'
}.toBeFalse() }).toBe false
it 'accepts valid offers', -> it 'accepts valid offers', ->
goodOffer = { expect(pp.pc).not.toBeNull()
expect(pp.receiveWebRTCOffer {
type: 'offer' type: 'offer'
sdp: 'foo' sdp: 'foo'
} }).toBe true
expect(pp.receiveWebRTCOffer goodOffer).toBe true
it 'responds with a WebRTC answer correctly', -> it 'responds with a WebRTC answer correctly', ->
spyOn snowflake.broker, 'sendAnswer' spyOn snowflake.broker, 'sendAnswer'
@ -59,20 +67,31 @@ describe 'ProxyPair', ->
describe 'flushes data between client and relay', -> describe 'flushes data between client and relay', ->
it 'proxies data from client to relay', -> it 'proxies data from client to relay', ->
pp.pc.ondatachannel {
channel: {
bufferedAmount: 0
readyState: "open"
send: (data) ->
}
}
spyOn pp.client, 'send'
spyOn pp.relay, 'send' spyOn pp.relay, 'send'
pp.c2rSchedule.push { data: 'foo' } pp.c2rSchedule.push 'foo'
pp.flush() pp.flush()
expect(pp.client.send).not.toHaveBeenCalled() expect(pp.client.send).not.toHaveBeenCalled()
expect(pp.relay.send).toHaveBeenCalledWith 'foo' expect(pp.relay.send).toHaveBeenCalledWith 'foo'
it 'proxies data from relay to client', -> it 'proxies data from relay to client', ->
spyOn pp.client, 'send' spyOn pp.client, 'send'
pp.r2cSchedule.push { data: 'bar' } spyOn pp.relay, 'send'
pp.r2cSchedule.push 'bar'
pp.flush() pp.flush()
expect(pp.client.send).toHaveBeenCalledWith 'bar' expect(pp.client.send).toHaveBeenCalledWith 'bar'
expect(pp.relay.send).not.toHaveBeenCalled() expect(pp.relay.send).not.toHaveBeenCalled()
it 'sends nothing with nothing to flush', -> it 'sends nothing with nothing to flush', ->
spyOn pp.client, 'send'
spyOn pp.relay, 'send'
pp.flush() pp.flush()
expect(pp.client.send).not.toHaveBeenCalled() expect(pp.client.send).not.toHaveBeenCalled()
expect(pp.relay.send).not.toHaveBeenCalled() expect(pp.relay.send).not.toHaveBeenCalled()

View file

@ -5,11 +5,17 @@ jasmine tests for Snowflake
query = {} query = {}
# Fake browser functionality: # Fake browser functionality:
class PeerConnection class PeerConnection
setRemoteDescription: ->
true
send: (data) ->
class SessionDescription class SessionDescription
type: 'offer' type: 'offer'
class WebSocket class WebSocket
OPEN: 1 OPEN: 1
CLOSED: 0 CLOSED: 0
constructor: ->
@bufferedAmount = 0
send: (data) ->
log = -> log = ->
class FakeUI class FakeUI
log: -> log: ->