Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tests

  • Loading branch information...
commit 262dbebcadc9ab1643402b036afa0958065283e0 1 parent e44f709
Elad Meidar authored
8 Rakefile
@@ -5,12 +5,14 @@ begin
5 5 require 'jeweler'
6 6 Jeweler::Tasks.new do |gem|
7 7 gem.name = "sinatra_fake_webservice"
8   - gem.summary = %Q{TODO: one-line summary of your gem}
9   - gem.description = %Q{TODO: longer description of your gem}
10   - gem.email = "elad@eizesus.com"
  8 + gem.summary = %Q{TODO: use a sinatra application in your Rails test environment to fake a remote web service that needs more magic than Fakeweb}
  9 + gem.description = %Q{TODO: FakeWeb allows you to fake a response from a specific url, this gem intends to give developers the option to allow several responses from the same url based on parameters (ex: WSDL)}
  10 + gem.email = "elad@nautilus6.com"
11 11 gem.homepage = "http://github.com/eladmeidar/sinatra_fake_webservice"
12 12 gem.authors = ["Elad Meidar"]
13 13 gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
  14 + gem.add_dependency 'sinatra'
  15 +
14 16 # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15 17 end
16 18 Jeweler::GemcutterTasks.new
0  lib/sinatra_fake_webservice.rb
No changes.
36 lib/sinatra_webservice.rb
... ... @@ -0,0 +1,36 @@
  1 +
  2 +
  3 +class SinatraWebService
  4 + attr_reader :services
  5 +
  6 + def initialize
  7 +
  8 + end
  9 +
  10 + def running?
  11 + false
  12 + end
  13 +
  14 + def initialize
  15 + @services = []
  16 + end
  17 +
  18 + def services
  19 + @services.dup.freeze
  20 + end
  21 +
  22 + def run!
  23 + @services.each do |service|
  24 + puts "== Loading service #{service.name}"
  25 + service.run
  26 + end
  27 + end
  28 +
  29 + def add_service(service = nil)
  30 + raise "Tried to add an empty service" if service.nil?
  31 + raise "Only WebService Objects are allowed" if !(service.is_a?(WebService))
  32 +
  33 + @services << service
  34 + end
  35 +
  36 +end
33 lib/web_service.rb
... ... @@ -0,0 +1,33 @@
  1 +class WebService
  2 +
  3 + class App < Sinatra::Base
  4 + get '/' do
  5 + "Hello! i am lindesy lohan!"
  6 + end
  7 + end
  8 +
  9 + attr_accessor :port, :host, :name
  10 +
  11 +
  12 + # Initialize a new face service
  13 + def initialize(options = {})
  14 + @name = options[:name] ||= "App"
  15 + @port = options[:port] ||= "4567"
  16 + @host = options[:host] ||= "localhost"
  17 + end
  18 +
  19 + def run
  20 + app = Thread.new("running_#{self.name}") do
  21 + App.run! :host => @host, :port => @port
  22 + end
  23 + sleep 1
  24 + end
  25 +
  26 + def method_missing(method, *args, &block)
  27 + if App.respond_to?(method)
  28 + App.send(method, args, block)
  29 + else
  30 + super
  31 + end
  32 + end
  33 +end
9 metal/sinatra_stem.rb
... ... @@ -0,0 +1,9 @@
  1 +require 'sinatra/base'
  2 +
  3 +class SinatraStem < Sinatra::Base
  4 +
  5 + get '/' do
  6 + "Hellllllo! i am Lindsey Lohan!!"
  7 + end
  8 +end
  9 +
7 test/helper.rb
... ... @@ -1,10 +1,15 @@
1 1 require 'rubygems'
2 2 require 'test/unit'
3 3 require 'shoulda'
  4 +require 'net/http'
  5 +require 'uri'
4 6
5 7 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  8 +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'metal'))
6 9 $LOAD_PATH.unshift(File.dirname(__FILE__))
7   -require 'sinatra_fake_webservice'
  10 +require 'sinatra_stem'
  11 +require 'sinatra_webservice'
  12 +require 'web_service'
8 13
9 14 class Test::Unit::TestCase
10 15 end
43 test/test_sinatra_fake_webservice.rb
... ... @@ -1,7 +1,46 @@
1 1 require 'helper'
2 2
3 3 class TestSinatraFakeWebservice < Test::Unit::TestCase
4   - should "probably rename this file and start testing for real" do
5   - flunk "hey buddy, you should probably rename this file and start testing for real"
  4 +
  5 + context "a new sinatra web service" do
  6 +
  7 + setup do
  8 + @sinatra_web_service = SinatraWebService.new
  9 + end
  10 +
  11 + should "fails adding a new service explicitly" do
  12 + assert_raises(TypeError) do
  13 + @sinatra_web_service.services << WebService.new
  14 + end
  15 + assert @sinatra_web_service.services.empty?
  16 + end
  17 +
  18 + should "fails assiging a new services array" do
  19 + assert_raises(NoMethodError) do
  20 + @sinatra_web_service.services = [WebService.new]
  21 + end
  22 + assert @sinatra_web_service.services.empty?
  23 + end
  24 +
  25 + should "not be running by default" do
  26 + @sinatra_web_service.add_service(WebService.new)
  27 + assert !(@sinatra_web_service.running?)
  28 + end
  29 +
  30 + should "run when !run is invoked" do
  31 + @sinatra_web_service.add_service(WebService.new)
  32 + @sinatra_web_service.run!
  33 + end
  34 +
  35 + should "allow adding a new service with #add_service" do
  36 + @sinatra_web_service.add_service(WebService.new)
  37 + assert @sinatra_web_service.services.any?
  38 + end
  39 +
  40 + should "allow running all the sinatra subapplications" do
  41 + @sinatra_web_service.add_service(WebService.new)
  42 + end
6 43 end
  44 +
  45 +
7 46 end
39 test/test_web_service.rb
... ... @@ -0,0 +1,39 @@
  1 +require 'helper'
  2 +
  3 +class TestWebservice < Test::Unit::TestCase
  4 +
  5 + context "a new web service" do
  6 + setup do
  7 + @web_service = WebService.new
  8 + end
  9 +
  10 + should "have a default name" do
  11 + assert_equal "App", @web_service.name
  12 + end
  13 +
  14 + should "have be set to localhost on port 4567" do
  15 + assert_equal "localhost", @web_service.host
  16 + assert_equal "4567", @web_service.port
  17 + end
  18 +
  19 + context "with a custom method" do
  20 + setup do
  21 + @web_service = WebService.new
  22 + @webservice.get "/test" do
  23 + "Hiya!"
  24 + end
  25 +
  26 + @webservice.run
  27 + end
  28 +
  29 + should "respond to '/test' with 'Hiya!'" do
  30 + res = Net::HTTP.start(@webservice.host,@webservice.port) do |http|
  31 + http.get('/test')
  32 + end
  33 + puts res.body
  34 + assert_equal "Hiya!", res.body
  35 + end
  36 + end
  37 + end
  38 +
  39 +end

0 comments on commit 262dbeb

Please sign in to comment.
Something went wrong with that request. Please try again.