servme 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07c16f51dfc2009674f65d3e0e46b44bba790b64
4
- data.tar.gz: 87adfbbe56a82ce3f6e722cb91d9b1a5afee1608
3
+ metadata.gz: cd06dcf6f7971cbbe2de4f8d22767c70f9c889cb
4
+ data.tar.gz: 08db2e4caf67056ece0230f09fa6ec7925a02ad0
5
5
  SHA512:
6
- metadata.gz: 3c0eed524cc2ab2ef695c60bcb18650fc90a9469e025f807b2e54665cf3fa0116c72bb15094390f189af865d4792d4b44f652873da0808874c3038fec94dcd2e
7
- data.tar.gz: 9f632d7e3b165a8e62d569646f9878ca52461fa9ae73ee01c88d695415894a325064d684780d8da915d3d7a0f847214f85db68e965616122165c65b44a3d9503
6
+ metadata.gz: 01514af01b6918c3143e8e0bdcbb5a28764b02290a45f6e92606402249bc351613db6e50995ed175248aed77eb3c1799e538acb5762f17103b7e33e782c29e7a
7
+ data.tar.gz: 3a63f76ce4c0e22ebcbe0dcd80da943cc01a43d2e4754a9ae29c03a44815626cb8823a3f5087e889912c17be05efeb6e20a8931754a7fd7d77e9dc1f758676a1
@@ -22,6 +22,7 @@ module Servme
22
22
  end
23
23
 
24
24
  def respond(request)
25
+ process_json_request(request)
25
26
  relative_path_on_disk = request.path.sub(options[:static_file_vdir], '')
26
27
 
27
28
  static_file = File.join(options[:static_file_root_path], relative_path_on_disk)
@@ -60,5 +61,17 @@ module Servme
60
61
  }
61
62
  }
62
63
  end
64
+
65
+ private
66
+
67
+ def process_json_request(request)
68
+ if request.params && request.params.empty? && request.env && request.env['CONTENT_TYPE'] =~ /application\/json/
69
+ parsed_json = JSON.parse(request.body.read)
70
+ parsed_json.each do |k,v|
71
+ request.params[k] = v
72
+ end
73
+ end
74
+ end
75
+
63
76
  end
64
77
  end
@@ -4,8 +4,8 @@ module Servme
4
4
  @request = request
5
5
  end
6
6
 
7
- def respond_with(response)
8
- Stubber.instance.stub(@request.merge({ :response => response }))
7
+ def respond_with(response, status_code = nil)
8
+ Stubber.instance.stub(@request.merge({ :response => response, :status_code => status_code }))
9
9
  end
10
10
 
11
11
  def error_with(status_code)
@@ -1,3 +1,3 @@
1
1
  module Servme
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -2,17 +2,40 @@ require 'spec_helper'
2
2
  describe Servme::Responder do
3
3
 
4
4
  let(:stubber) { Servme::Stubber.instance }
5
- let(:sinatra_app) { stub }
5
+ let(:sinatra_app) { double }
6
6
  subject { Servme::Responder.new(sinatra_app, {}) }
7
7
 
8
8
  before { stubber.clear }
9
9
 
10
+ context "when the responder receives a JSON request" do
11
+ let(:params) { {"foo" => "bar"} }
12
+
13
+ it "extracts and parses the json" do
14
+ stubber.stub(:url => "/foo", :method => :get, :params => params, :response => {"bar" => "foo"})
15
+
16
+ response = subject.respond(double(
17
+ :path => "/foo",
18
+ :request_method => "GET",
19
+ :params => {},
20
+ :env => {"CONTENT_TYPE"=>"application/json; charset=UTF-8"},
21
+ :body => StringIO.new(params.to_json)
22
+ ))
23
+
24
+ JSON.parse(response.last).should == {"bar" => "foo"}
25
+ end
26
+ end
27
+
10
28
  it "returns stubs" do
11
29
  stubber.stub(:url => "/foo", :method => :get, :params => {}, :response => {"foo" => "bar"})
12
30
 
13
- response = subject.respond(stub(:path => "/foo", :request_method => "GET", :params => {}))
31
+ response = subject.respond(double(
32
+ :path => "/foo",
33
+ :request_method => "GET",
34
+ :params => {},
35
+ :env => nil
36
+ ))
14
37
 
15
- #response is a Rack response, its last entry is the resopnse body
38
+ #response is a Rack response, its last entry is the response body
16
39
  JSON.parse(response.last).should == {"foo" => "bar"}
17
40
  end
18
41
 
@@ -20,13 +43,21 @@ describe Servme::Responder do
20
43
  File.stub(:exists? => true)
21
44
  sinatra_app.should_receive(:send_file).with("dist/foo")
22
45
 
23
- subject.respond(stub(:path => "/foo", :request_method => "GET"))
46
+ subject.respond(double(
47
+ :path => "/foo",
48
+ :request_method => "GET",
49
+ :params => nil
50
+ ))
24
51
  end
25
52
 
26
53
  it "responds with the static index.html if the request is /" do
27
54
  sinatra_app.should_receive(:send_file).with("dist/index.html")
28
55
 
29
- subject.respond(stub(:path => "/", :request_method => "GET"))
56
+ subject.respond(double(
57
+ :path => "/",
58
+ :request_method => "GET",
59
+ :params => nil
60
+ ))
30
61
  end
31
62
 
32
63
  it "allows you to specify an alternate static_file_root_path" do
@@ -34,7 +65,11 @@ describe Servme::Responder do
34
65
  File.stub(:exists? => true)
35
66
  sinatra_app.should_receive(:send_file).with("public/style.css")
36
67
 
37
- responder.respond(stub(:path => "/style.css", :request_method => "GET"))
68
+ responder.respond(double(
69
+ :path => "/style.css",
70
+ :request_method => "GET",
71
+ :params => nil
72
+ ))
38
73
  end
39
74
 
40
75
  it "returns the stub if there is both a stub and a static file" do
@@ -42,7 +77,12 @@ describe Servme::Responder do
42
77
  File.stub(:exists? => true)
43
78
  sinatra_app.should_not_receive(:send_file)
44
79
 
45
- response = subject.respond(stub(:path => "/foo", :request_method => "GET", :params => {}))
80
+ response = subject.respond(double(
81
+ :path => "/foo",
82
+ :request_method => "GET",
83
+ :params => {},
84
+ :env => nil
85
+ ))
46
86
 
47
87
  JSON.parse(response.last).should == {"foo" => "bar"}
48
88
  end
@@ -60,7 +100,11 @@ describe Servme::Responder do
60
100
  File.stub(:exists? => true)
61
101
  sinatra_app.should_receive(:send_file).with("build/foo")
62
102
 
63
- subject.respond(stub(:path => "/vdir/foo", :request_method => "GET"))
103
+ subject.respond(double(
104
+ :path => "/vdir/foo",
105
+ :request_method => "GET",
106
+ :params => nil
107
+ ))
64
108
  end
65
109
  end
66
110
  end
@@ -80,6 +80,22 @@ module Servme
80
80
  Then { last_response.status.should == 333 }
81
81
  end
82
82
 
83
+ describe "response PLUS status code" do
84
+ Given do
85
+ ServiceStubbing.new({
86
+ :url => "/bizness/stuff",
87
+ :method => :post,
88
+ :params => {
89
+ :bizness => "true",
90
+ :money => "12"
91
+ }
92
+ }).respond_with({ :sad => "panda" }, 430)
93
+ end
94
+ When { post('/bizness/stuff', { :bizness => true, :money => 12 }) }
95
+ Then { last_response.status.should == 430 }
96
+ Then { last_response.body.should be_json :sad => "panda" }
97
+ end
98
+
83
99
  describe "non-json responses" do
84
100
  Given do
85
101
  ServiceStubbing.new({
@@ -96,4 +112,4 @@ module Servme
96
112
  end
97
113
 
98
114
  end
99
- end
115
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Searls
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-09 00:00:00.000000000 Z
11
+ date: 2013-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json