mock-server-thin 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- # MockServer #
1
+ # MockServerThin #
2
2
 
3
3
  A quick way of mocking an external web service you want to consume.
4
4
 
@@ -2,13 +2,48 @@ require "sinatra/base"
2
2
  require "logger"
3
3
 
4
4
  class MockServerThin
5
+
5
6
  class App < Sinatra::Base
6
7
  use Rack::ShowExceptions
7
8
  end
8
9
 
9
- def initialize(app, port = 4000, &block)
10
+ class << self
11
+ attr_accessor :config
12
+ end
13
+
14
+ class Config
15
+ attr_accessor :timeout, :port, :host
16
+
17
+ def initialize
18
+ @timeout = 5
19
+ @port = 4000
20
+ @host = "0.0.0.0"
21
+ end
22
+
23
+ def to_hash
24
+ Hash[instance_variables.map { |var| [var[1..-1].to_sym, instance_variable_get(var)] }]
25
+ end
26
+ end
27
+
28
+ def self.configure
29
+ yield(config)
30
+ end
31
+
32
+ def self.config
33
+ @config ||= Config.new
34
+ end
35
+
36
+ def config
37
+ self.class.config
38
+ end
39
+
40
+ def initialize(app, opts = {}, &block)
10
41
  @app = app
11
- @port = port
42
+ options = config.to_hash.merge!(opts)
43
+ puts "SERVER OPTIONS: %s" % options.inspect
44
+ @port = options[:port]
45
+ @timeout = options[:timeout]
46
+ @host = options[:host]
12
47
  end
13
48
 
14
49
  def start
@@ -18,16 +53,16 @@ class MockServerThin
18
53
  end
19
54
  end
20
55
 
21
- wait_for_service("0.0.0.0", @port)
56
+ wait_for_service(@host, @port)
22
57
 
23
58
  self
24
59
  end
25
60
 
26
61
  module Methods
27
- def mock_server(*args, &block)
62
+ def mock_server(opts = {}, &block)
28
63
  app = Class.new(Sinatra::Base)
29
64
  app.class_eval(&block)
30
- @server = MockServerThin.new(app, *args, &block).start
65
+ @server = MockServerThin.new(app, opts, &block).start
31
66
  end
32
67
  end
33
68
 
@@ -51,12 +86,12 @@ protected
51
86
  end
52
87
  end
53
88
 
54
- def wait_for_service(host, port, timeout = 5)
89
+ def wait_for_service(host, port)
55
90
  start_time = Time.now
56
91
 
57
92
  until listening?(host, port)
58
- if timeout && (Time.now > (start_time + timeout))
59
- raise SocketError.new("Socket did not open within #{timeout} seconds")
93
+ if @timeout && (Time.now > (start_time + @timeout))
94
+ raise SocketError.new("Socket did not open within #{@timeout} seconds")
60
95
  end
61
96
  end
62
97
 
@@ -29,18 +29,19 @@ end
29
29
 
30
30
  class MockServerRackBuilderTest < Test::Unit::TestCase
31
31
  def setup
32
- @server = MockServerThin.new(HelloWorldRackBuilder, 4001)
32
+ @server = MockServerThin.new(HelloWorldRackBuilder, :port => 4001)
33
33
  @server.start
34
34
  end
35
35
 
36
36
  def test_server
37
37
  assert_equal "Rackup!", open("http://localhost:4001").read
38
- end end
38
+ end
39
+ end
39
40
 
40
41
  class MockServerMethodsTest < Test::Unit::TestCase
41
42
  extend MockServerThin::Methods
42
43
 
43
- mock_server(4002) {
44
+ mock_server(:port => 4002) {
44
45
  get "/" do
45
46
  "Goodbye"
46
47
  end
@@ -50,3 +51,21 @@ class MockServerMethodsTest < Test::Unit::TestCase
50
51
  assert_equal "Goodbye", open("http://localhost:4002").read
51
52
  end
52
53
  end
54
+
55
+ class MockServerConfigTest < Test::Unit::TestCase
56
+ def setup
57
+ MockServerThin.configure do |config|
58
+ config.port = 5067
59
+ config.timeout = 10
60
+ config.host = "127.0.0.1"
61
+ end
62
+ @server = MockServerThin.new(HelloWorldSinatra)
63
+ @server.start
64
+ end
65
+
66
+ def test_server_config
67
+ assert_equal 5067, @server.config.port
68
+ assert_equal 10, @server.config.timeout
69
+ assert_equal "127.0.0.1", @server.config.host
70
+ end
71
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock-server-thin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-11-18 00:00:00.000000000Z
13
+ date: 2012-01-02 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
17
- requirement: &2158204160 !ruby/object:Gem::Requirement
17
+ requirement: &2153247480 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2158204160
25
+ version_requirements: *2153247480
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: thin
28
- requirement: &2158203160 !ruby/object:Gem::Requirement
28
+ requirement: &2153246160 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2158203160
36
+ version_requirements: *2153246160
37
37
  description:
38
38
  email:
39
39
  - djanowski@dimaion.com