poisol 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/lib/poisol/domain.rb +13 -11
  3. data/lib/poisol/server.rb +45 -37
  4. data/lib/poisol/stub/request/query_builder.rb +3 -1
  5. data/lib/poisol/stub/request/request_body_builder.rb +89 -87
  6. data/lib/poisol/stub/request/url_builder.rb +15 -12
  7. data/lib/poisol/stub/response/array_response_body.rb +75 -0
  8. data/lib/poisol/stub/response/response_body_builder.rb +49 -116
  9. data/lib/poisol/stub/response/status_builder.rb +2 -0
  10. data/lib/poisol/stub/stub.rb +22 -21
  11. data/lib/poisol/stub/stub_class_builder.rb +24 -23
  12. data/lib/poisol/stub/stub_instance.rb +62 -44
  13. data/lib/poisol/stub/webrick_stub_builder.rb +34 -32
  14. data/lib/poisol/stub_config/stub_config.rb +10 -8
  15. data/lib/poisol/stub_config/stub_config_builder.rb +91 -89
  16. data/lib/poisol/stub_factory.rb +39 -38
  17. data/lib/poisol/stub_mapper/request_matcher.rb +68 -0
  18. data/lib/poisol/stub_mapper/response_mapper.rb +33 -0
  19. data/lib/poisol/stub_mapper/stubs.rb +27 -0
  20. data/spec/functional/key_value/explicit_inclusion_spec.rb +15 -1
  21. data/spec/functional/key_value/implicit_inclusion_spec.rb +1 -1
  22. data/spec/functional/multi_domain_spec.rb +1 -1
  23. data/spec/functional/post_request_spec.rb +1 -2
  24. data/spec/functional/query/query_explicit_spec.rb +1 -1
  25. data/spec/functional/query/query_implicit_spec.rb +1 -1
  26. data/spec/functional/response/array_spec.rb +14 -7
  27. data/spec/functional/response/dumb_response_spec.rb +1 -1
  28. data/spec/functional/response/hash_params_spec.rb +1 -1
  29. data/spec/functional/response/nested_array_spec.rb +2 -2
  30. data/spec/functional/url_spec.rb +1 -1
  31. data/spec/spec_helper.rb +0 -1
  32. metadata +5 -3
  33. data/lib/poisol/response_mapper.rb +0 -31
  34. data/lib/poisol/stubs.rb +0 -74
@@ -0,0 +1,68 @@
1
+ module Poisol
2
+ module RequestMatcher
3
+ extend self
4
+
5
+ def matches? actual_req,stub_req
6
+ type_matches?(actual_req,stub_req) &&
7
+ url_macthes?(actual_req,stub_req) &&
8
+ query_matches?(actual_req,stub_req) &&
9
+ body_matches?(actual_req,stub_req)
10
+ end
11
+
12
+ def type_matches? actual_req,stub_req
13
+ actual_req.type == stub_req.type
14
+ end
15
+
16
+ def url_macthes? actual_req,stub_req
17
+ actual_req.url == stub_req.url
18
+ end
19
+
20
+ def query_matches? actual_req,stub_req
21
+ return actual_req.query==stub_req.query
22
+ end
23
+
24
+ def body_matches? actual_req,stub_req
25
+ return true if actual_req.body == stub_req.body
26
+ actual_req_body = load_as_json actual_req.body
27
+ stub_req_body = load_as_json stub_req.body
28
+ return false unless actual_req_body.class == stub_req_body.class
29
+ return true if actual_req_body == stub_req_body
30
+ return matching_hashes? actual_req_body,stub_req_body if actual_req_body.is_a?(Hash)
31
+ return matching_array? actual_req_body,stub_req_body if actual_req_body.is_a?(Array)
32
+ end
33
+
34
+ def load_as_json input
35
+ begin
36
+ return Parse.json_to_hash input
37
+ rescue
38
+ return input
39
+ end
40
+ end
41
+
42
+
43
+ def matching_hashes?(actuals, expected)
44
+ return false unless actuals.keys.sort == expected.keys.sort
45
+ actuals.each do |key, actual|
46
+ expected = expected[key]
47
+
48
+ if actual.is_a?(Hash) && expected.is_a?(Hash)
49
+ return false unless matching_hashes?(actual, expected)
50
+ else
51
+ return false unless expected === actual
52
+ end
53
+ end
54
+ true
55
+ end
56
+
57
+ def matching_array actuals,expected
58
+ return false unless actuals.size == expected.size
59
+ return actuals.sort == expected.sort unless actual[0].is_a(Hash)
60
+ expect = expected.clone
61
+ actuals.each do |actual|
62
+ match = expect.detect {|expected| matching_hashes? actual,expect}
63
+ return false if match.blank?
64
+ end
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,33 @@
1
+ module Poisol
2
+ module ResponseMapper
3
+ extend self
4
+
5
+ def map webrick_request
6
+ stub_request = get_stub_request webrick_request
7
+ PoisolLog.info stub_request.to_s
8
+ stub_response = get_stub_response stub_request
9
+ end
10
+
11
+ def get_stub_request webrick_request
12
+ stub_request = Request.new
13
+ stub_request.type = webrick_request.request_method.downcase
14
+ uri = webrick_request.request_uri
15
+ stub_request.url = uri.query.blank? ? uri.to_s : uri.to_s.sub(uri.query,"").sub("?","")
16
+ stub_request.query = webrick_request.query_string
17
+ stub_request.body = webrick_request.body
18
+ stub_request
19
+ end
20
+
21
+
22
+ def get_stub_response stub_request
23
+ stub = Stubs.get_match stub_request
24
+ if stub.blank?
25
+ PoisolLog.error "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^No match found for above request"
26
+ PoisolLog.error "Registered requests are: #{Stubs.all.map{|stub| "\n#{stub.request.to_s}"}.join}\n--end--"
27
+ raise "No match found for request: #{stub_request.to_s} "
28
+ end
29
+ return stub.response if stub.present?
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ require_relative './request_matcher'
2
+ module Poisol
3
+ module Stubs
4
+ extend self
5
+
6
+ def all
7
+ @stubs
8
+ end
9
+
10
+ def add stub
11
+ @stubs = [] if @stubs.blank?
12
+ @stubs << stub
13
+ end
14
+
15
+ def reset
16
+ @stubs = []
17
+ end
18
+
19
+ def get_match actual_request
20
+ return nil if @stubs.blank?
21
+ matches = @stubs.select{|stub| Poisol::RequestMatcher.matches? actual_request,stub.request}
22
+ return matches.present? ? matches[0] : nil
23
+ end
24
+
25
+
26
+ end
27
+ end
@@ -1,4 +1,4 @@
1
- describe Stub, "#key_value" do
1
+ describe Poisol::Stub, "#key_value" do
2
2
 
3
3
  it "partial dynamic request" do
4
4
  Explicit.new.by_name("ram").build()
@@ -6,10 +6,24 @@ describe Stub, "#key_value" do
6
6
  expect(response.body).to eq({"hi"=>1}.to_json)
7
7
  end
8
8
 
9
+ it "partial hash param" do
10
+ Explicit.new.by(:name=>"ram").build()
11
+ response = RestClient.post "http://localhost:3030/explicit","name=ram"
12
+ expect(response.body).to eq({"hi"=>1}.to_json)
13
+ end
14
+
15
+
9
16
  it "dynamic request" do
10
17
  Explicit.new.by_name("ram").by_age(11).build()
11
18
  response = RestClient.post "http://localhost:3030/explicit","name=ram&age=11"
12
19
  expect(response.body).to eq({"hi"=>1}.to_json)
13
20
  end
14
21
 
22
+ it "as hash param" do
23
+ Explicit.new.by(:name=>"ram",:age=>11).build()
24
+ response = RestClient.post "http://localhost:3030/explicit","name=ram&age=11"
25
+ expect(response.body).to eq({"hi"=>1}.to_json)
26
+ end
27
+
28
+
15
29
  end
@@ -1,4 +1,4 @@
1
- describe Stub, "#key_value" do
1
+ describe Poisol::Stub, "#key_value" do
2
2
 
3
3
  it "default request" do
4
4
  KeyValue.new.build()
@@ -1,4 +1,4 @@
1
- describe Stub, "#multi domain" do
1
+ describe Poisol::Stub, "#multi domain" do
2
2
 
3
3
  it "support multi domain" do
4
4
  First.new.build
@@ -1,4 +1,4 @@
1
- describe Stub, "#post_user" do
1
+ describe Poisol::Stub, "#post_user" do
2
2
 
3
3
  it "default request and response" do
4
4
  User.new.build()
@@ -15,4 +15,3 @@ describe Stub, "#post_user" do
15
15
  end
16
16
 
17
17
  end
18
-
@@ -1,4 +1,4 @@
1
- describe Stub, "#query_explicit" do
1
+ describe Poisol::Stub, "#query_explicit" do
2
2
 
3
3
  it "partial" do
4
4
  BookExplicit.new.for_author('bha').build()
@@ -1,4 +1,4 @@
1
- describe Stub, "#implicit query params" do
1
+ describe Poisol::Stub, "#implicit query params" do
2
2
 
3
3
  it "dynamic response" do
4
4
  Book.new.for_author("bha").has_category({"age_group"=>"11", "publisher"=>{"name"=>"oxford"}}).build()
@@ -1,9 +1,9 @@
1
- describe Stub, "#array" do
1
+ describe Poisol::Stub, "#array" do
2
2
 
3
- it "empty column array" do
3
+ it "default column array" do
4
4
  Columns.new.build
5
5
  response = RestClient.get "http://localhost:3030/column"
6
- expect(response.body).to eq("[]")
6
+ expect(response.body).to eq({"title"=>["independance"], "category"=>["10"]}.to_json)
7
7
  end
8
8
 
9
9
  it "column array" do
@@ -24,16 +24,23 @@ describe Stub, "#array" do
24
24
  expect(response.body).to eq({"title"=>["abc", "c"], "category"=>["10","1"]}.to_json)
25
25
  end
26
26
 
27
- it "empty row array" do
27
+ it "default row array" do
28
28
  Rows.new.build
29
29
  response = RestClient.get "http://localhost:3030/row"
30
- expect(response.body).to eq("[]")
30
+ expect(response.body).to eq([{"title"=>"independance","items"=> [1,2],"category"=>{"age_group"=>"10"}}].to_json)
31
31
  end
32
32
 
33
33
  it "row array" do
34
- Rows.new.has_row.with_item(3).has_row.with_title("abc").with_category("age_group" => "12").with_items([2,3]).build
34
+ Rows.new.
35
+ has_row.with_item(3).with_item(4).
36
+ has_row.with_item(3).with_item(4).
37
+ has_row.with_title("abc").with_category("age_group" => "12").with_items([2,3]).build
35
38
  response = RestClient.get "http://localhost:3030/row"
36
- expect(response.body).to eq([{"title"=>"independance","items"=> [1,2,3],"category"=>{"age_group"=>"10"}}, {"title"=>"abc","items"=> [2,3],"category"=>{"age_group"=>"12"}}].to_json)
39
+ expect(response.body).to eq([
40
+ {"title"=>"independance","items"=> [3,4],"category"=>{"age_group"=>"10"}},
41
+ {"title"=>"independance","items"=> [3,4],"category"=>{"age_group"=>"10"}},
42
+ {"title"=>"abc","items"=> [2,3],"category"=>{"age_group"=>"12"}}
43
+ ].to_json)
37
44
  end
38
45
 
39
46
  it "row array hash_params" do
@@ -1,4 +1,4 @@
1
- describe Stub, "#dumb response" do
1
+ describe Poisol::Stub, "#dumb response" do
2
2
 
3
3
  it "column array" do
4
4
  Columns.new.set_dumb_response("spec/data/main/user/response.json").build
@@ -1,4 +1,4 @@
1
- describe StubInstance, "hash params" do
1
+ describe Poisol::StubInstance, "hash params" do
2
2
  it "default" do
3
3
  SimpleResponse.new.build
4
4
  response = RestClient.get "http://localhost:3030/simple_response"
@@ -1,4 +1,4 @@
1
- describe Stub, "#nested array" do
1
+ describe Poisol::Stub, "#nested array" do
2
2
 
3
3
  it "empty" do
4
4
  NestedArray.new.has_no_role.build()
@@ -25,7 +25,7 @@ describe Stub, "#nested array" do
25
25
  end
26
26
 
27
27
  it "multiple fields with altered values" do
28
- NestedArray.new.has_role.has_another_role(:role_id=>"test").build()
28
+ NestedArray.new.has_role.has_role(:role_id=>"test").build()
29
29
  response = RestClient.get "http://localhost:3030/nested_array"
30
30
  expect(response.body).to eq({"title"=>"ind", "roles"=>[{"role_id"=>"chumma", "role_name"=>"sol"},{"role_id"=>"test", "role_name"=>"sol"}]}.to_json)
31
31
  end
@@ -1,4 +1,4 @@
1
- describe Stub, "#url" do
1
+ describe Poisol::Stub, "#url" do
2
2
 
3
3
  it "default" do
4
4
  Url.new.build()
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,6 @@ require_relative '../lib/poisol'
5
5
  require 'pry'
6
6
 
7
7
  require 'simplecov'
8
- SimpleCov.minimum_coverage 40
9
8
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
10
9
  SimpleCov::Formatter::HTMLFormatter
11
10
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poisol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Deepak
@@ -133,11 +133,11 @@ files:
133
133
  - lib/poisol/domain.rb
134
134
  - lib/poisol/extensions/hash.rb
135
135
  - lib/poisol/extensions/webmock_extensions.rb
136
- - lib/poisol/response_mapper.rb
137
136
  - lib/poisol/server.rb
138
137
  - lib/poisol/stub/request/query_builder.rb
139
138
  - lib/poisol/stub/request/request_body_builder.rb
140
139
  - lib/poisol/stub/request/url_builder.rb
140
+ - lib/poisol/stub/response/array_response_body.rb
141
141
  - lib/poisol/stub/response/response_body_builder.rb
142
142
  - lib/poisol/stub/response/status_builder.rb
143
143
  - lib/poisol/stub/stub.rb
@@ -147,7 +147,9 @@ files:
147
147
  - lib/poisol/stub_config/stub_config.rb
148
148
  - lib/poisol/stub_config/stub_config_builder.rb
149
149
  - lib/poisol/stub_factory.rb
150
- - lib/poisol/stubs.rb
150
+ - lib/poisol/stub_mapper/request_matcher.rb
151
+ - lib/poisol/stub_mapper/response_mapper.rb
152
+ - lib/poisol/stub_mapper/stubs.rb
151
153
  - lib/poisol/utils/file_util.rb
152
154
  - lib/poisol/utils/logger.rb
153
155
  - lib/poisol/utils/parse.rb
@@ -1,31 +0,0 @@
1
- module ResponseMapper
2
- extend self
3
-
4
- def map webrick_request
5
- stub_request = get_stub_request webrick_request
6
- PoisolLog.info stub_request.to_s
7
- stub_response = get_stub_response stub_request
8
- end
9
-
10
- def get_stub_request webrick_request
11
- stub_request = Request.new
12
- stub_request.type = webrick_request.request_method.downcase
13
- uri = webrick_request.request_uri
14
- stub_request.url = uri.query.blank? ? uri.to_s : uri.to_s.sub(uri.query,"").sub("?","")
15
- stub_request.query = webrick_request.query_string
16
- stub_request.body = webrick_request.body
17
- stub_request
18
- end
19
-
20
-
21
- def get_stub_response stub_request
22
- stub = Stubs.get_match stub_request
23
- if stub.blank?
24
- PoisolLog.error "^^^^^^^^^^^^\nNo match found for above request"
25
- PoisolLog.error "Registered requests are #{Stubs.all.map{|stub| "#{stub.request.to_s}\n"}.join}"
26
- raise "No match found for request: #{stub_request.to_s} "
27
- end
28
- return stub.response if stub.present?
29
- end
30
-
31
- end
data/lib/poisol/stubs.rb DELETED
@@ -1,74 +0,0 @@
1
- module Stubs
2
- extend self
3
-
4
- def all
5
- @stubs
6
- end
7
-
8
- def add stub
9
- @stubs = [] if @stubs.blank?
10
- @stubs << stub
11
- end
12
-
13
- def reset
14
- @stubs = []
15
- end
16
-
17
- def get_match request
18
- return nil if @stubs.blank?
19
- matches = @stubs.select{|stub| stub.request.url == request.url }
20
- matches = matches.select{|stub|query_matches?(request.query,stub.request.query)} if request.query.present? && matches.present?
21
- matches = matches.select{|stub|body_matches? request.body,stub.request.body} if request.body.present? && matches.present?
22
- return matches.present? ? matches[0] : nil
23
- end
24
-
25
- def query_matches? (actual,stub)
26
- return false if stub.blank?
27
- return actual==stub
28
- end
29
-
30
- def body_matches? (actual_request_body,stub_request_body)
31
- return true if stub_request_body.present? && actual_request_body == stub_request_body
32
- return false if stub_request_body.blank?
33
- actual_request_body = load_as_json actual_request_body
34
- stub_request_body = load_as_json stub_request_body
35
- return false unless actual_request_body.class == stub_request_body.class
36
- return matching_hashes? actual_request_body,stub_request_body if actual_request_body.is_a?(Hash)
37
- return matching_array? actual_request_body,stub_request_body if actual_request_body.is_a?(Array)
38
- return false
39
- end
40
-
41
- def load_as_json input
42
- begin
43
- return Parse.json_to_hash input
44
- rescue
45
- return input
46
- end
47
- end
48
-
49
-
50
- def matching_hashes?(query_parameters, pattern)
51
- return false unless query_parameters.is_a?(Hash)
52
- return false unless query_parameters.keys.sort == pattern.keys.sort
53
- query_parameters.each do |key, actual|
54
- expected = pattern[key]
55
-
56
- if actual.is_a?(Hash) && expected.is_a?(Hash)
57
- return false unless matching_hashes?(actual, expected)
58
- else
59
- return false unless expected === actual
60
- end
61
- end
62
- true
63
- end
64
-
65
- def matching_array actuals,expected
66
- return false unless actuals.size == expected.size
67
- return actuals.sort == expected.sort unless actual[0].is_a(Hash)
68
- expect = expected.clone
69
- actuals.each do |actual|
70
- match = expect.detect {|pattern| matching_hashes? actual,expect}
71
- return false if match.blank?
72
- end
73
- end
74
- end