yafs 0.0.6 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fbee2989f0f9f1ea770e5ff1b352373caf72f2e
4
- data.tar.gz: 3fb3a22d5ff95b6afd49a4169b6dcbbe4294b37e
3
+ metadata.gz: 92873718936b67d9105e769541532b8f6ddf927d
4
+ data.tar.gz: 8edff93dc54b1fecad4716d3f4d1cbdc2e602b1d
5
5
  SHA512:
6
- metadata.gz: 559a21fd429505365299fc7c8360b99314469fb2ff64996832ad3044a902a481a7bc6f0d89a14408d175920748f57a3f77ff6cd40f6cc2207d305e0248af7108
7
- data.tar.gz: fba1d3c614c4431e8223bda47004173ce4eae9b99dbbc60b816390a917bc910c389d8a7510c12a47d8ca23ab6bd211b57fe0d0af68d6e6768badabc69b482038
6
+ metadata.gz: 77689755ac037feacb34545ac83c65650de96181d80efa6bec45651a3a529901a618ba984df1b28ef1e5038f99c6fcee2248c7ddc6abfa5a974181e63704d374
7
+ data.tar.gz: 9990bc6d017672556d40b27a30625468f2578466bcdf6acf52b13e6f62608d361a0edd810957407cc9229a636fc5d3c1f47bcd3bd01c019376f4c4fbb45ec379
@@ -6,6 +6,9 @@ module Fake
6
6
  end
7
7
 
8
8
  def stop
9
+ # todo: something better, stop fails if start and stop comes almost the same
10
+ # needs some check that if thread is alive and if not then yield (or something...)
11
+ sleep 0.5
9
12
  @fake_service.stop
10
13
  end
11
14
 
@@ -4,11 +4,15 @@ module Fake
4
4
  def POST
5
5
  params = super
6
6
  if content_type && content_type.downcase == 'application/json'
7
- hash = JSON.parse(body.gets)
7
+ hash = JSON.parse(body_string)
8
8
  params.merge!(hash)
9
9
  end
10
10
  params
11
11
  end
12
12
 
13
+ def body_string
14
+ @body_string ||= body.gets
15
+ end
16
+
13
17
  end
14
18
  end
@@ -1,6 +1,7 @@
1
1
  module Fake
2
2
  class RequestHandler
3
3
  attr_reader :responses
4
+ attr_accessor :body
4
5
 
5
6
  #
6
7
  # Creates handler for http request
@@ -10,10 +11,11 @@ module Fake
10
11
  @method = method
11
12
  @path = Path.new(path)
12
13
  @responses = InfiniteQueue.new
14
+ @body = nil
13
15
  end
14
16
 
15
17
  def call(request)
16
- if @path.eql?(request.path) && request.request_method.eql?(@method.to_s.upcase)
18
+ if should_serve?(request)
17
19
  current_response = @responses.next
18
20
  raise "FAKE service: No response set for request #{presentation}" if current_response.nil?
19
21
  current_response.evaluate()
@@ -22,6 +24,14 @@ module Fake
22
24
  end
23
25
 
24
26
  private
27
+ def should_serve?(request)
28
+ should_serve = @path.eql?(request.path) && request.request_method.eql?(@method.to_s.upcase)
29
+ if should_serve && @body != nil
30
+ should_serve = @body == request.body_string
31
+ end
32
+ should_serve
33
+ end
34
+
25
35
  def presentation
26
36
  @path
27
37
  end
@@ -37,9 +37,17 @@ module Fake
37
37
  @request_handler = request_handler
38
38
  end
39
39
 
40
+
40
41
  #
41
42
  # DSL
42
43
  #
44
+
45
+ # request body, which must match
46
+ def body(body)
47
+ @request_handler.body = body
48
+ self
49
+ end
50
+
43
51
  def respond(body:nil, status:200, headers:{}, &block)
44
52
  @request_handler.responses << Response.new(body, status, headers, &block)
45
53
  self
@@ -86,10 +86,24 @@ describe 'Fake Service' do
86
86
  end
87
87
 
88
88
  describe "Responding specific request" do
89
+ before do
90
+ Fake.start(port:4567)
91
+ end
92
+ after do
93
+ Fake.stop
94
+ end
95
+
89
96
  xit "can respond based on query parameter" do
90
97
  # what about specific parameters vs some parameter
91
98
  fs.get('/cart/option').param('id=5').respond("ok") # Matches /cart/option?id=5,
92
99
  end
100
+
101
+ it "can resbond based on specific body" do
102
+ Fake.post('/cart').body('{"var":1,"var2":"3"}').respond(body:"1")
103
+ Fake.post('/cart').body('{"var":1,"var2":"4"}').respond(body:"2")
104
+ expect(HTTParty.post('http://localhost:4567/cart', body:'{"var":1,"var2":"4"}').response.body).to eq "2"
105
+ expect(HTTParty.post('http://localhost:4567/cart', body:'{"var":1,"var2":"3"}').response.body).to eq "1"
106
+ end
93
107
  end
94
108
 
95
109
  describe "Checking request parameters" do
@@ -19,6 +19,7 @@
19
19
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[.. lib]))
20
20
  require 'fake_service'
21
21
  require 'helpers'
22
+ require 'byebug'
22
23
  RSpec.configure do |config|
23
24
  # rspec-expectations config goes here. You can use an alternate
24
25
  # assertion/expectation library such as wrong or the stdlib/minitest
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yafs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mika Lackman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-22 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack