convert all coffeescript tests to jasmine

This commit is contained in:
Serene Han 2016-02-04 14:13:03 -08:00
parent 10d24accad
commit e38bed8be3
8 changed files with 205 additions and 338 deletions

180
proxy/spec.coffee Normal file
View file

@ -0,0 +1,180 @@
###
jasmine tests for Snowflake
###
# Stubs to fake browser functionality.
class WebSocket
OPEN: 1
CLOSED: 0
ui = {}
describe 'BuildUrl', ->
it 'should parse just protocol and host', ->
expect(buildUrl('http', 'example.com')).toBe 'http://example.com'
it 'should handle different ports', ->
expect buildUrl 'http', 'example.com', 80
.toBe 'http://example.com'
expect buildUrl 'http', 'example.com', 81
.toBe 'http://example.com:81'
expect buildUrl 'http', 'example.com', 443
.toBe 'http://example.com:443'
expect buildUrl 'http', 'example.com', 444
.toBe 'http://example.com:444'
it 'should handle paths', ->
expect buildUrl 'http', 'example.com', 80, '/'
.toBe 'http://example.com/'
expect buildUrl 'http', 'example.com', 80,'/test?k=%#v'
.toBe 'http://example.com/test%3Fk%3D%25%23v'
expect buildUrl 'http', 'example.com', 80, '/test'
.toBe 'http://example.com/test'
it 'should handle params', ->
expect buildUrl 'http', 'example.com', 80, '/test', [['k', '%#v']]
.toBe 'http://example.com/test?k=%25%23v'
expect buildUrl 'http', 'example.com', 80, '/test', [['a', 'b'], ['c', 'd']]
.toBe 'http://example.com/test?a=b&c=d'
it 'should handle ips', ->
expect buildUrl 'http', '1.2.3.4'
.toBe 'http://1.2.3.4'
expect buildUrl 'http', '1:2::3:4'
.toBe 'http://[1:2::3:4]'
it 'should handle bogus', ->
expect buildUrl 'http', 'bog][us'
.toBe 'http://bog%5D%5Bus'
expect buildUrl 'http', 'bog:u]s'
.toBe 'http://bog%3Au%5Ds'
describe 'Parse', ->
describe 'cookie', ->
it 'parses correctly', ->
expect Parse.cookie ''
.toEqual {}
expect Parse.cookie 'a=b'
.toEqual { a: 'b' }
expect Parse.cookie 'a=b=c'
.toEqual { a: 'b=c' }
expect Parse.cookie 'a=b; c=d'
.toEqual { a: 'b', c: 'd' }
expect Parse.cookie 'a=b ; c=d'
.toEqual { a: 'b', c: 'd' }
expect Parse.cookie 'a= b'
.toEqual { a: 'b' }
expect Parse.cookie 'a='
.toEqual { a: '' }
expect Parse.cookie 'key'
.toBeNull()
expect Parse.cookie 'key=%26%20'
.toEqual { key: '& ' }
expect Parse.cookie 'a=\'\''
.toEqual { a: '\'\'' }
describe 'address', ->
it 'parses IPv4', ->
expect Parse.address ''
.toBeNull()
expect Parse.address '3.3.3.3:4444'
.toEqual { host: '3.3.3.3', port: 4444 }
expect Parse.address '3.3.3.3'
.toBeNull()
expect Parse.address '3.3.3.3:0x1111'
.toBeNull()
expect Parse.address '3.3.3.3:-4444'
.toBeNull()
expect Parse.address '3.3.3.3:65536'
.toBeNull()
it 'parses IPv6', ->
expect Parse.address '[1:2::a:f]:4444'
.toEqual { host: '1:2::a:f', port: 4444 }
expect Parse.address '[1:2::a:f]'
.toBeNull()
expect Parse.address '[1:2::a:f]:0x1111'
.toBeNull()
expect Parse.address '[1:2::a:f]:-4444'
.toBeNull()
expect Parse.address '[1:2::a:f]:65536'
.toBeNull()
expect Parse.address '[1:2::ffff:1.2.3.4]:4444'
.toEqual { host: '1:2::ffff:1.2.3.4', port: 4444 }
describe 'query string', ->
it 'should parse correctly', ->
expect Query.parse ''
.toEqual {}
expect Query.parse 'a=b'
.toEqual { a: 'b' }
expect Query.parse 'a=b=c'
.toEqual { a: 'b=c' }
expect Query.parse 'a=b&c=d'
.toEqual { a: 'b', c: 'd' }
expect Query.parse 'client=&relay=1.2.3.4%3A9001'
.toEqual { client: '', relay: '1.2.3.4:9001' }
expect Query.parse 'a=b%26c=d'
.toEqual { a: 'b&c=d' }
expect Query.parse 'a%3db=d'
.toEqual { 'a=b': 'd' }
expect Query.parse 'a=b+c%20d'
.toEqual { 'a': 'b c d' }
expect Query.parse 'a=b+c%2bd'
.toEqual { 'a': 'b c+d' }
expect Query.parse 'a+b=c'
.toEqual { 'a b': 'c' }
expect Query.parse 'a=b+c+d'
.toEqual { a: 'b c d' }
it 'uses the first appearance of duplicate key', ->
expect Query.parse 'a=b&c=d&a=e'
.toEqual { a: 'b', c: 'd' }
expect Query.parse 'a'
.toEqual { a: '' }
expect Query.parse '=b'
.toEqual { '': 'b' }
expect Query.parse '&a=b'
.toEqual { '': '', a: 'b' }
expect Query.parse 'a=b&'
.toEqual { a: 'b', '':'' }
expect Query.parse 'a=b&&c=d'
.toEqual { a: 'b', '':'', c: 'd' }
describe 'Params', ->
describe 'bool', ->
getBool = (query) ->
Params.getBool (Query.parse query), 'param', false
it 'parses correctly', ->
expect(getBool 'param=true').toEqual true
expect(getBool 'param').toEqual true
expect(getBool 'param=').toEqual true
expect(getBool 'param=1').toEqual true
expect(getBool 'param=0').toEqual false
expect(getBool 'param=false').toEqual false
expect(getBool 'param=unexpected').toBeNull()
expect(getBool 'pram=true').toEqual false
describe 'address', ->
DEFAULT = { host: '1.1.1.1', port: 2222 }
getAddress = (query) ->
Params.getAddress query, 'addr', DEFAULT
it 'parses correctly', ->
expect(getAddress {}).toEqual DEFAULT
expect getAddress { addr: '3.3.3.3:4444' }
.toEqual { host: '3.3.3.3', port: 4444 }
expect getAddress { x: '3.3.3.3:4444' }
.toEqual DEFAULT
expect getAddress { addr: '---' }
.toBeNull()
describe 'ProxyPair', ->
fakeRelay = Parse.address '0.0.0.0:12345'
rateLimit = new DummyRateLimit()
destination = []
fakeClient = send: (d) -> destination.push d
pp = new ProxyPair(fakeClient, fakeRelay, rateLimit)
it 'handles relay correctly', ->
pp.connectRelay()
expect(pp.relay.onopen).not.toBeNull()
expect(pp.relay.onclose).not.toBeNull()
expect(pp.relay.onerror).not.toBeNull()
expect(pp.relay.onmessage).not.toBeNull()
# TODO: Test for flush
# pp.c2rSchedule.push { data: 'omg' }
# pp.flush()
# if destination == ['omg'] then pass 'flush'
# else fail 'flush', ['omg'], destination