Kaspar Schiess avatar Kaspar Schiess committed 8ea7283

Adds a test for configuration of proxy

Comments (0)

Files changed (2)

lib/event_shipper/proxy.rb

       fail "Please specify a configuration file to use on the command line. (--config)" \
         unless config
 
-      configuration = YAML.load_file(config)
+      configure_with YAML.load_file(config)
 
-      listen = configuration['listen']
-      host, port = listen['host'], listen['port']
-      transport = UDP.new(host, port)
+      begin
+        trap('SIGINFO') { puts transport.stats }
+      rescue ArgumentError
+        # unsupported signal SIGINFO
+      end
+
+      transport.dispatch { |queue, message| 
+        # puts message }
+        redis.lpush queue, message }
+    end
+
+    attr_reader :host, :port
+    attr_reader :listen
+    attr_reader :userdb
+    attr_reader :redis
+    attr_reader :transport
+
+    def configure_with configuration
+      @listen = configuration['listen']
+      @host, @port = listen['host'], listen['port']
+      @transport = UDP.new(host, port)
 
       if configuration['encrypt']
         users = configuration['users']
-        userdb = Hash[users.map { |str| str.split('/') }]
+        @userdb = Hash[users.map { |str| str.partition('/').values_at(0,2) }]
 
         transport.wrap Filter::Decrypt.new(userdb)
       end
 
       redis_config = configuration['redis']
-      redis = Redis.new(
+      @redis = Redis.new(
         host: redis_config['host'], 
         port: redis_config['port'])
-
-      begin
-        trap('SIGINFO') { puts transport.stats }
-      rescue ArgumentError
-        # unsupported signal SIGINFO
-      end
-
-      transport.dispatch { |queue, message| 
-        # puts message }
-        redis.lpush queue, message }
     end
   end
 end

spec/lib/event_shipper/proxy_spec.rb

+
+require 'spec_helper'
+
+require 'event_shipper'
+
+describe EventShipper::Proxy do
+  let(:proxy) { described_class.new nil }
+
+  describe "#configure_with" do
+    it "configures all connections" do
+      proxy.configure_with(
+        'listen' => {
+          'host' => '127.0.0.1', 
+          'port' => 80 }, 
+        'redis' => {
+          'host' => '127.0.0.1', 
+          'port' => '4567' }, 
+        'encrypt' => true, 
+        'users' => [
+          'test/pass/word' ])
+
+      proxy.userdb.should == { 'test' => 'pass/word' }
+    end
+  end
+end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.