mock-server 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ # MockServer #
2
+
3
+ A quick way of mocking an external web service you want to consume.
4
+
5
+ ## Usage ##
6
+
7
+ You're writing a feature that needs to connect to an external web service
8
+ (anything served by HTTP). You wonder how to test that. Your options
9
+ are to stub methods in Net::HTTP and equivalents, but by doing that you
10
+ are tying yourself to an implementation detail. The ideal thing to do
11
+ is to lay out an environment where your code can still run, connect to
12
+ a web server, send out requests and get responses back. Enter MockServer.
13
+
14
+ class RSSFeedTest < Test::Unit::TestCase
15
+ extend MockServer::Methods
16
+
17
+ mock_server {
18
+ get "/feed.xml" do
19
+ <<-EOS
20
+ <?xml version="1.0"?>
21
+ <rss version="2.0">
22
+ <channel>
23
+ <title>A mock website</title>
24
+ <link>http://example.com/</link>
25
+ </channel>
26
+ </rss>
27
+ EOS
28
+ end
29
+ }
30
+
31
+ def test_rss_feed
32
+ # YourAwesomeComponent should connect to http://localhost:4000.
33
+ # (you *are* putting those URLs in an environment-aware config file, right?)
34
+
35
+ posts = YourAwesomeComponent.load_posts
36
+
37
+ assert_equal "A mock website", post.first.channel.title
38
+ end
39
+ end
40
+
41
+ Yes, things happening inside the `mock_server` call are just a regular Sinatra application. w00t!
42
+
43
+ ## License ##
44
+
45
+ MIT.
@@ -0,0 +1,65 @@
1
+ require "sinatra/base"
2
+ require "logger"
3
+
4
+ class MockServer
5
+ class App < Sinatra::Base
6
+ use Rack::ShowExceptions
7
+ end
8
+
9
+ def initialize(port = 4000, &block)
10
+ @port = port
11
+
12
+ @app = Class.new(Sinatra::Base)
13
+ @app.class_eval(&block)
14
+ end
15
+
16
+ def start
17
+ Thread.new do
18
+ with_quiet_logger do |logger|
19
+ Rack::Handler::WEBrick.run(@app, :Port => @port, :Logger => logger, :AccessLog => [])
20
+ end
21
+ end
22
+
23
+ wait_for_service("0.0.0.0", @port)
24
+
25
+ self
26
+ end
27
+
28
+ module Methods
29
+ def mock_server(*args, &block)
30
+ @server = MockServer.new(*args, &block).start
31
+ end
32
+ end
33
+
34
+ protected
35
+ def with_quiet_logger
36
+ io = File.open("/dev/null", "w")
37
+ yield(::Logger.new(io))
38
+ ensure
39
+ io.close
40
+ end
41
+
42
+ def listening?(host, port)
43
+ begin
44
+ socket = TCPSocket.new(host, port)
45
+ socket.close unless socket.nil?
46
+ true
47
+ rescue Errno::ECONNREFUSED,
48
+ Errno::EBADF, # Windows
49
+ Errno::EADDRNOTAVAIL # Windows
50
+ false
51
+ end
52
+ end
53
+
54
+ def wait_for_service(host, port, timeout = 5)
55
+ start_time = Time.now
56
+
57
+ until listening?(host, port)
58
+ if timeout && (Time.now > (start_time + timeout))
59
+ raise SocketError.new("Socket did not open within #{timeout} seconds")
60
+ end
61
+ end
62
+
63
+ true
64
+ end
65
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "mock_server"))
2
+
3
+ require "test/unit"
4
+ require "ruby-debug"
5
+ require "open-uri"
6
+
7
+ class MockServerTest < Test::Unit::TestCase
8
+ @@server = MockServer.new do
9
+ get "/" do
10
+ "Hello"
11
+ end
12
+ end
13
+
14
+ def setup
15
+ @@server.start
16
+ end
17
+
18
+ def test_server
19
+ assert_equal "Hello", open("http://localhost:4000").read
20
+ end
21
+ end
22
+
23
+ class MockServerMethodsTest < Test::Unit::TestCase
24
+ extend MockServer::Methods
25
+
26
+ mock_server(4001) {
27
+ get "/" do
28
+ "Goodbye"
29
+ end
30
+ }
31
+
32
+ def test_server
33
+ assert_equal "Goodbye", open("http://localhost:4001").read
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mock-server
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Damian Janowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-06 00:00:00 -03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ - djanowski@dimaion.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/mock_server.rb
27
+ - README.markdown
28
+ - test/mock_server_test.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/djanowski/mock-server
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: A quick way of mocking an external web service you want to consume.
57
+ test_files: []
58
+