pac_generator.coffee 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. chai = require 'chai'
  2. should = chai.should()
  3. describe 'PacGenerator', ->
  4. PacGenerator = require '../src/pac_generator.coffee'
  5. options =
  6. '+auto':
  7. name: 'auto'
  8. profileType: 'SwitchProfile'
  9. revision: 'test'
  10. defaultProfileName: 'direct'
  11. rules: [
  12. {profileName: 'proxy', condition:
  13. conditionType: 'UrlRegexCondition'
  14. pattern: '^http://(www|www2)\\.example\\.com/'
  15. }
  16. {profileName: 'direct', condition:
  17. conditionType: 'HostLevelsCondition'
  18. minValue: 3
  19. maxValue: 8
  20. }
  21. {
  22. profileName: 'proxy'
  23. condition: {conditionType: 'KeywordCondition', pattern: 'keyword'}
  24. }
  25. {profileName: 'proxy', condition:
  26. conditionType: 'UrlWildcardCondition'
  27. pattern: 'https://ssl.example.com/*'
  28. }
  29. ]
  30. '+proxy':
  31. name: 'proxy'
  32. profileType: 'FixedProfile'
  33. revision: 'test'
  34. fallbackProxy: {scheme: 'http', host: '127.0.0.1', port: 8888}
  35. bypassList: [
  36. {conditionType: 'BypassCondition', pattern: '127.0.0.1:8080'}
  37. {conditionType: 'BypassCondition', pattern: '127.0.0.1'}
  38. {conditionType: 'BypassCondition', pattern: '<local>'}
  39. ]
  40. it 'should generate pac scripts from options', ->
  41. ast = PacGenerator.script(options, 'auto')
  42. pac = ast.print_to_string(beautify: true, comments: true)
  43. pac.should.not.be.empty
  44. func = eval("(function () { #{pac}\n return FindProxyForURL; })()")
  45. result = func('http://www.example.com/', 'www.example.com')
  46. result.should.equal('PROXY 127.0.0.1:8888')
  47. it 'should be able to compress pac scripts', ->
  48. ast = PacGenerator.script(options, 'auto')
  49. pac = PacGenerator.compress(ast).print_to_string()
  50. pac.should.not.be.empty
  51. func = eval("(function () { #{pac}\n return FindProxyForURL; })()")
  52. result = func('http://www.example.com/', 'www.example.com')
  53. result.should.equal('PROXY 127.0.0.1:8888')