ZeroOmega/omega-pac/test/profiles.coffee

212 lines
8.1 KiB
CoffeeScript
Raw Normal View History

2014-09-20 11:49:04 -04:00
chai = require 'chai'
should = chai.should()
describe 'Profiles', ->
Profiles = require '../src/profiles'
Conditions = require '../src/conditions'
U2 = require 'uglify-js'
ruleListResult = (profileName, source) ->
profileName: profileName
source: source
testProfile = (profile, request, expected, expectedCompiled) ->
o_request = request
if typeof request == 'string'
request = Conditions.requestFromUrl(request)
expectedCompiled ?= expected[0] ? Profiles.nameAsKey(expected.profileName)
compiled = Profiles.compile(profile)
compileResult = eval '(' + compiled.print_to_string() + ')'
if typeof compileResult == 'function'
compileResult = compileResult(request.url, request.host, request.scheme)
if expected?
matchResult = Profiles.match(profile, request)
try
if expected.source?
chai.assert.equal(matchResult.profileName, expected.profileName)
chai.assert.equal(matchResult.source, expected.source)
else
chai.assert.deepEqual(matchResult, expected)
catch
printResult = JSON.stringify(matchResult)
msg = ("expect profile to return #{JSON.stringify(expected)} " +
"instead of #{printResult} for request #{o_request}")
chai.assert(false, msg)
if compileResult != expectedCompiled
msg = ("expect COMPILED profile to return #{expectedCompiled} " +
"instead of #{compileResult} for request #{o_request}")
chai.assert(false, msg)
return expected
describe '#pacResult', ->
it 'should return DIRECT for no proxy', ->
Profiles.pacResult().should.equal("DIRECT")
it 'should return a valid PAC result for a proxy', ->
proxy = {scheme: "http", host: "127.0.0.1", port: 8888}
Profiles.pacResult(proxy).should.equal("PROXY 127.0.0.1:8888")
describe '#byName', ->
it 'should get profiles from builtin profiles', ->
profile = Profiles.byName('direct')
profile.should.be.an('object')
profile.profileType.should.equal('DirectProfile')
it 'should get profiles from given options', ->
profile = {}
profile = Profiles.byName('profile', {"+profile": profile})
profile.should.equal(profile)
describe 'SystemProfile', ->
it 'should be builtin with the name "system"', ->
profile = Profiles.byName('system')
profile.should.be.an('object')
profile.profileType.should.equal('SystemProfile')
it 'should not match request to profiles', ->
profile = Profiles.byName('system')
should.not.exist Profiles.match(profile, {})
it 'should throw when trying to compile', ->
profile = Profiles.byName('system')
should.throw(-> Profiles.compile(profile))
describe 'DirectProfile', ->
it 'should be builtin with the name "direct"', ->
profile = Profiles.byName('direct')
profile.should.be.an('object')
profile.profileType.should.equal('DirectProfile')
it 'should return "DIRECT" when compiled', ->
profile = Profiles.byName('direct')
testProfile(profile, {}, null, 'DIRECT')
describe 'FixedProfile', ->
profile =
profileType: 'FixedProfile'
bypassList: [{
conditionType: 'BypassCondition'
pattern: '<local>'
}]
proxyForHttps:
scheme: 'http'
host: '127.0.0.1'
port: 1234
fallbackProxy:
scheme: 'socks5'
host: '127.0.0.1'
port: 1234
it 'should use protocol-specific proxies if suitable', ->
testProfile(profile, 'https://www.example.com/',
['PROXY 127.0.0.1:1234', 'https'])
it 'should use fallback proxies for other protocols', ->
testProfile(profile, 'ftp://www.example.com/',
['SOCKS5 127.0.0.1:1234', ''])
it 'should not use any proxy for requests matching the bypassList', ->
testProfile profile, 'ftp://localhost/', ['DIRECT', profile.bypassList[0]]
describe 'PacProfile', ->
profile = Profiles.create('test', 'PacProfile')
profile.pacScript = '''
function FindProxyForURL(url, host) {
return "PROXY " + host + ":8080";
}
'''
it 'should return the result of the pac script', ->
testProfile(profile, 'ftp://www.example.com:9999/abc', null,
'PROXY www.example.com:8080')
it 'should return includable for non-file pacUrl', ->
Profiles.isIncludable(profile).should.be.true
it 'should return not includable for file: pacUrl', ->
profile = Profiles.create('test', 'PacProfile')
profile.pacUrl = 'file:///proxy.pac'
Profiles.isIncludable(profile).should.be.false
2014-09-20 11:49:04 -04:00
describe 'SwitchProfile', ->
profile = Profiles.create('test', 'SwitchProfile')
profile.rules = [
{
condition:
conditionType: 'HostWildcardCondition'
pattern: 'company.abc.example.com'
profileName: 'company'
},
{
condition:
conditionType: 'HostWildcardCondition'
pattern: '*.example.com'
profileName: 'example'
},
{
condition:
conditionType: 'HostWildcardCondition'
pattern: '*.abc.example.com'
profileName: 'abc'
}
]
profile.defaultProfileName = 'default'
it 'should match requests based on rules', ->
testProfile(profile, 'http://company.abc.example.com:998/abc',
profile.rules[0])
it 'should respect the order of rules', ->
testProfile(profile, 'http://abc.example.com:9999/abc',
profile.rules[1])
testProfile(profile, 'http://www.example.com:9999/abc',
profile.rules[1])
it 'should return defaultProfileName when no rules match', ->
testProfile(profile, 'http://www.example.org:9999/abc',
['+default', null])
it 'should calulate directly referenced profiles correctly', ->
set = Profiles.directReferenceSet(profile)
set.should.eql(
'+company': 'company'
'+example': 'example'
'+abc': 'abc'
'+default': 'default'
)
it 'should clear the reference cache on profile revision change', ->
profile.revision = 'a'
set = Profiles.directReferenceSet(profile)
# Remove 'default' from references.
profile.defaultProfileName = 'abc'
profile.revision = 'b'
newSet = Profiles.directReferenceSet(profile)
newSet.should.eql(
'+company': 'company'
'+example': 'example'
'+abc': 'abc'
)
2014-10-25 11:41:39 -04:00
describe 'VirtualProfile', ->
profile = Profiles.create('test', 'VirtualProfile')
profile.defaultProfileName = 'default'
it 'should always return defaultProfileName', ->
testProfile(profile, 'http://www.example.com/abc',
['+default', null])
2014-09-20 11:49:04 -04:00
describe 'RulelistProfile', ->
profile = Profiles.create('test', 'AutoProxyRuleListProfile')
profile.defaultProfileName = 'default'
profile.matchProfileName = 'example'
profile.ruleList = 'example.com'
profile.revision = 'a'
it 'should calulate directly referenced profiles correctly', ->
set = Profiles.directReferenceSet(profile)
set.should.eql(
'+example': 'example'
'+default': 'default'
)
it 'should match requests based on the rule list', ->
testProfile(profile, 'http://localhost/example.com',
ruleListResult('example', 'example.com'))
testProfile(profile, 'http://localhost/example.org', ['+default', null])
it 'should update rule list on update', ->
Profiles.update(profile, 'example.org')
profile.revision = 'b'
testProfile(profile, 'http://localhost/example.com', ['+default', null])
testProfile(profile, 'http://localhost/example.org',
ruleListResult('example', 'example.org'))
it 'should switch to AutoProxy format on update if detected', ->
profile = Profiles.create('test2', 'RuleListProfile')
profile.format = 'Switchy'
profile.defaultProfileName = 'default'
profile.matchProfileName = 'example'
profile.format.should.equal 'Switchy'
Profiles.update(profile, '[AutoProxy]\nexample.org')
profile.format.should.equal 'AutoProxy'
testProfile(profile, 'http://localhost/example.com',
['+default', null])
testProfile(profile, 'http://localhost/example.org',
ruleListResult('example', 'example.org'))