servme 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,6 @@
1
- require "servme/version"
1
+ require 'servme/version'
2
+ require 'servme/stubber'
3
+ require 'servme/responder'
2
4
  require 'servme/service'
3
5
  require 'servme/service_stubbing'
4
- require 'servme/dsl'
6
+ require 'servme/dsl'
@@ -16,4 +16,4 @@ module Servme
16
16
  Service.clear
17
17
  end
18
18
 
19
- end
19
+ end
@@ -0,0 +1,61 @@
1
+ module Servme
2
+ class Responder
3
+
4
+ DEFAULT_HEADERS = {
5
+ 'Content-Type' => 'application/json'
6
+ }
7
+
8
+ DEFAULT_OPTIONS = {
9
+ :static_file_root_path => "dist"
10
+ }
11
+
12
+ attr_accessor :sinatra_app, :options
13
+
14
+ def initialize(sinatra_app, opts)
15
+ @sinatra_app = sinatra_app
16
+ @options = DEFAULT_OPTIONS.merge(opts)
17
+ end
18
+
19
+ def stubber
20
+ Stubber.instance
21
+ end
22
+
23
+ def respond(request)
24
+ static_file = File.join(options[:static_file_root_path], request.path)
25
+ stub = stubber.stub_for_request(request)
26
+ if (stub)
27
+ format_response(stub)
28
+ elsif(request.path == '/')
29
+ sinatra_app.send_file("#{options[:static_file_root_path]}/index.html")
30
+ elsif(File.exists?(static_file))
31
+ sinatra_app.send_file(static_file)
32
+ else
33
+ format_response(default_response(request))
34
+ end
35
+ end
36
+
37
+ def format_response(response)
38
+ body = json?(response) ? JSON::dump(response[:data]) : response[:data]
39
+ [response[:status_code], response[:headers], body]
40
+ end
41
+
42
+ def json?(response)
43
+ response[:headers]['Content-Type'] == 'application/json'
44
+ end
45
+
46
+ def default_response(request)
47
+ {
48
+ :headers => DEFAULT_HEADERS,
49
+ :status_code => 404,
50
+ :data => {
51
+ :error => "Servme doesn't know how to respond to this request",
52
+ :request => {
53
+ :method => request.request_method.downcase.to_sym, #FIXME: duplication -- stubber.rb
54
+ :params => request.params,
55
+ :path => request.path
56
+ }
57
+ }
58
+ }
59
+ end
60
+ end
61
+ end
@@ -8,79 +8,20 @@ module Servme
8
8
  set :server, 'thin'
9
9
 
10
10
  get '*' do
11
- handle_request(:get)
11
+ responder.respond(request)
12
12
  end
13
13
 
14
14
  post '*' do
15
- handle_request(:post)
15
+ responder.respond(request)
16
16
  end
17
17
 
18
- def handle_request(type = :get)
19
- format_response(
20
- self.class.satisfy(
21
- request.path,
22
- type,
23
- request.send(type.to_s.upcase)
24
- )
25
- )
18
+ def responder
19
+ @responder ||= Responder.new(self, {})
26
20
  end
27
21
 
28
- def format_response(response)
29
- body = json?(response) ? JSON::dump(response[:data]) : response[:data]
30
- [response[:status_code], response[:headers], body]
31
- end
32
-
33
- def json?(response)
34
- response[:headers]['Content-Type'] == 'application/json'
35
- end
36
-
37
- @@stubbings = {}
38
-
39
22
  def self.clear
40
- @@stubbings = {}
41
- end
42
-
43
-
44
- DEFAULT_HEADERS = {
45
- 'Content-Type' => 'application/json'
46
- }
47
-
48
- def self.stub(config)
49
- (@@stubbings[config[:url]] ||= {}).tap do |urls|
50
- (urls[config[:method] || :get] ||= {}).tap do |methods|
51
- methods[stringify_keys(config[:params] || {})] = {
52
- :data => config[:response],
53
- :headers => DEFAULT_HEADERS.merge(config[:headers] || {}),
54
- :status_code => config[:status_code] || 200
55
- }
56
- end
57
- end
58
- end
59
-
60
- def self.satisfy(path, method, params)
61
- @@stubbings[path][method][params] || default_response(path, method, params)
62
- rescue
63
- default_response(path, method, params)
64
- end
65
-
66
- def self.default_response(path, method, params)
67
- {
68
- :headers => DEFAULT_HEADERS,
69
- :status_code => 404,
70
- :data => {
71
- :error => "Servme doesn't know how to respond to this request",
72
- :request => {
73
- :method => method,
74
- :params => params,
75
- :path => path
76
- }
77
- }
78
- }
79
- end
80
-
81
- def self.stringify_keys(params)
82
- Hash[params.map {|(k,v)| [k.to_s, v]}]
23
+ Stubber.instance.clear
83
24
  end
84
25
  end
85
26
 
86
- end
27
+ end
@@ -5,11 +5,11 @@ module Servme
5
5
  end
6
6
 
7
7
  def respond_with(response)
8
- Service.stub(@request.merge({ :response => response }))
8
+ Stubber.instance.stub(@request.merge({ :response => response }))
9
9
  end
10
10
 
11
11
  def error_with(status_code)
12
- Service.stub(@request.merge({ :status_code => status_code }))
12
+ Stubber.instance.stub(@request.merge({ :status_code => status_code }))
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -0,0 +1,39 @@
1
+ require 'singleton'
2
+ module Servme
3
+ class Stubber
4
+ include Singleton
5
+
6
+ def initialize
7
+ @stubbings = {}
8
+ end
9
+
10
+ def clear
11
+ @stubbings = {}
12
+ end
13
+
14
+ def stub(config)
15
+ (@stubbings[config[:url]] ||= {}).tap do |urls|
16
+ (urls[config[:method] || :get] ||= {}).tap do |methods|
17
+ methods[stringify_keys(config[:params] || {})] = {
18
+ :data => config[:response],
19
+ :headers => Responder::DEFAULT_HEADERS.merge(config[:headers] || {}),
20
+ :status_code => config[:status_code] || 200
21
+ }
22
+ end
23
+ end
24
+ end
25
+
26
+ def stub_for_request(req)
27
+ method = req.request_method.downcase.to_sym
28
+ begin
29
+ @stubbings[req.path][method][req.params]
30
+ rescue NoMethodError
31
+ nil
32
+ end
33
+ end
34
+
35
+ def stringify_keys(params)
36
+ Hash[params.map {|(k,v)| [k.to_s, v]}]
37
+ end
38
+ end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module Servme
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ describe Servme::Responder do
3
+
4
+ let(:stubber) { Servme::Stubber.instance }
5
+ let(:sinatra_app) { stub }
6
+ subject { Servme::Responder.new(sinatra_app, {}) }
7
+
8
+ before { stubber.clear }
9
+
10
+ it "returns stubs" do
11
+ stubber.stub(:url => "/foo", :method => :get, :params => {}, :response => {"foo" => "bar"})
12
+
13
+ response = subject.respond(stub(:path => "/foo", :request_method => "GET", :params => {}))
14
+
15
+ #response is a Rack response, its last entry is the resopnse body
16
+ JSON.parse(response.last).should == {"foo" => "bar"}
17
+ end
18
+
19
+ it "sends static files when there is no stub" do
20
+ File.stub(:exists? => true)
21
+ sinatra_app.should_receive(:send_file).with("dist/foo")
22
+
23
+ subject.respond(stub(:path => "/foo", :request_method => "GET"))
24
+ end
25
+
26
+ it "responds with the static index.html if the request is /" do
27
+ sinatra_app.should_receive(:send_file).with("dist/index.html")
28
+
29
+ subject.respond(stub(:path => "/", :request_method => "GET"))
30
+ end
31
+
32
+ it "allows you to specify an alternate static_file_root_path" do
33
+ responder = Servme::Responder.new(sinatra_app, :static_file_root_path => "public")
34
+ File.stub(:exists? => true)
35
+ sinatra_app.should_receive(:send_file).with("public/style.css")
36
+
37
+ responder.respond(stub(:path => "/style.css", :request_method => "GET"))
38
+ end
39
+
40
+ it "returns the stub if there is both a stub and a static file" do
41
+ stubber.stub(:url => "/foo", :method => :get, :params => {}, :response => {"foo" => "bar"})
42
+ File.stub(:exists? => true)
43
+ sinatra_app.should_not_receive(:send_file)
44
+
45
+ response = subject.respond(stub(:path => "/foo", :request_method => "GET", :params => {}))
46
+
47
+ JSON.parse(response.last).should == {"foo" => "bar"}
48
+ end
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-22 00:00:00.000000000 Z
12
+ date: 2012-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -121,10 +121,13 @@ files:
121
121
  - Rakefile
122
122
  - lib/servme.rb
123
123
  - lib/servme/dsl.rb
124
+ - lib/servme/responder.rb
124
125
  - lib/servme/service.rb
125
126
  - lib/servme/service_stubbing.rb
127
+ - lib/servme/stubber.rb
126
128
  - lib/servme/version.rb
127
129
  - servme.gemspec
130
+ - spec/lib/servme/responder_spec.rb
128
131
  - spec/lib/servme/service_spec.rb
129
132
  - spec/lib/servme/service_stubbing_spec.rb
130
133
  - spec/spec_helper.rb
@@ -154,6 +157,7 @@ specification_version: 3
154
157
  summary: Servme lets you stub server responses by standing-in for some remote system
155
158
  your code under test depends on
156
159
  test_files:
160
+ - spec/lib/servme/responder_spec.rb
157
161
  - spec/lib/servme/service_spec.rb
158
162
  - spec/lib/servme/service_stubbing_spec.rb
159
163
  - spec/spec_helper.rb