tshield 0.11.4.0 → 0.11.5.0
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 +4 -4
- data/lib/tshield/controllers/requests.rb +4 -1
- data/lib/tshield/matching/filters.rb +56 -0
- data/lib/tshield/request_matching.rb +8 -50
- data/lib/tshield/version.rb +1 -1
- data/spec/tshield/fixtures/matching/example.json +13 -0
- data/spec/tshield/request_matching_spec.rb +12 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7581fed92c9b0af76120edc8d9a4b9eff62e1f37c8eafa6a33a5712e918382d6
|
4
|
+
data.tar.gz: '018a8ca426a9af1bc6e374461e6f4748673cecf24358b332fd6c30a5ec433028'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8afb34fe21d6e9d8813c46c42e2c8d64908d9f72badd51bd11e1c92ea4b37b23e159cc03f2ad85932cceda146446db351f32b3167f5a52200df5af8df7cbaf59
|
7
|
+
data.tar.gz: 5bf25b51e864b9f4b89a05158e5532582f86f21ceca0e24e2dafce2c2eb37235867686ecd8514ee6cecc061f4dd3247d83113f11f24eca25b89e9027103a1519
|
@@ -81,6 +81,9 @@ module TShield
|
|
81
81
|
add_headers(headers, path)
|
82
82
|
|
83
83
|
api_response ||= TShield::RequestVCR.new(path, options).response
|
84
|
+
api_response.headers.reject! do |key, _v|
|
85
|
+
configuration.get_excluded_headers(domain(path)).include?(key)
|
86
|
+
end
|
84
87
|
end
|
85
88
|
|
86
89
|
logger.info(
|
@@ -90,7 +93,7 @@ module TShield
|
|
90
93
|
)
|
91
94
|
|
92
95
|
status api_response.status
|
93
|
-
headers api_response.headers
|
96
|
+
headers api_response.headers
|
94
97
|
body api_response.body
|
95
98
|
end
|
96
99
|
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TShield
|
4
|
+
module Matching
|
5
|
+
# Filters used in request matching
|
6
|
+
module Filters
|
7
|
+
def find_stub(stubs)
|
8
|
+
result = filter_stubs(stubs[@options[:session]] || {})
|
9
|
+
return result if result
|
10
|
+
|
11
|
+
filter_stubs(stubs[DEFAULT_SESSION] || {}) unless @options[:session] == DEFAULT_SESSION
|
12
|
+
end
|
13
|
+
|
14
|
+
def filter_by_method(stubs)
|
15
|
+
stubs.select { |stub| stub['method'] == @options[:method] }
|
16
|
+
end
|
17
|
+
|
18
|
+
def filter_by_headers(stubs)
|
19
|
+
stubs.select { |stub| Filters.include_headers(stub['headers'], @options[:headers]) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def filter_by_query(stubs)
|
23
|
+
stubs.select { |stub| Filters.include_query(stub['query'], @options[:raw_query] || '') }
|
24
|
+
end
|
25
|
+
|
26
|
+
def filter_stubs(stubs)
|
27
|
+
result = filter_by_query(filter_by_headers(filter_by_method(stubs[@path] || [])))
|
28
|
+
result[0]['response'] unless result.empty?
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.include_headers(stub_headers, request_headers)
|
32
|
+
request_headers ||= {}
|
33
|
+
stub_headers ||= {}
|
34
|
+
result = stub_headers.reject { |key, value| request_headers[key.to_rack_name] == value }
|
35
|
+
result.empty? || stub_headers.empty?
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.include_query(stub_query, raw_query)
|
39
|
+
request_query = Filters.build_query_hash(raw_query)
|
40
|
+
stub_query ||= {}
|
41
|
+
result = stub_query.reject { |key, value| request_query[key] == value.to_s }
|
42
|
+
result.empty? || stub_query.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.build_query_hash(raw_query)
|
46
|
+
params = {}
|
47
|
+
raw_query.split('&').each do |query|
|
48
|
+
key, value = query.split('=')
|
49
|
+
params[key] = value
|
50
|
+
end
|
51
|
+
|
52
|
+
params
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'tshield/matching/filters'
|
3
4
|
require 'tshield/request'
|
4
5
|
|
5
6
|
DEFAULT_SESSION = 'no-session'
|
@@ -7,6 +8,8 @@ DEFAULT_SESSION = 'no-session'
|
|
7
8
|
module TShield
|
8
9
|
# Class to check request matching
|
9
10
|
class RequestMatching
|
11
|
+
include TShield::Matching::Filters
|
12
|
+
|
10
13
|
attr_reader :matched
|
11
14
|
|
12
15
|
def initialize(path, options = {})
|
@@ -21,41 +24,14 @@ module TShield
|
|
21
24
|
end
|
22
25
|
|
23
26
|
def match_request
|
24
|
-
@matched = find_stub
|
27
|
+
@matched = find_stub(self.class.stubs)
|
25
28
|
return unless matched
|
26
29
|
|
27
|
-
TShield::Response.new(matched['body'],
|
30
|
+
TShield::Response.new(self.class.read_body(matched['body']),
|
28
31
|
matched['headers'],
|
29
32
|
matched['status'])
|
30
33
|
end
|
31
34
|
|
32
|
-
private
|
33
|
-
|
34
|
-
def find_stub
|
35
|
-
stubs = self.class.stubs
|
36
|
-
result = filter_stubs(stubs[@options[:session]] || {})
|
37
|
-
return result if result
|
38
|
-
|
39
|
-
filter_stubs(stubs[DEFAULT_SESSION] || {}) unless @options[:session] == DEFAULT_SESSION
|
40
|
-
end
|
41
|
-
|
42
|
-
def filter_by_method(stubs)
|
43
|
-
stubs.select { |stub| stub['method'] == @options[:method] }
|
44
|
-
end
|
45
|
-
|
46
|
-
def filter_by_headers(stubs)
|
47
|
-
stubs.select { |stub| self.class.include_headers(stub['headers'], @options[:headers]) }
|
48
|
-
end
|
49
|
-
|
50
|
-
def filter_by_query(stubs)
|
51
|
-
stubs.select { |stub| self.class.include_query(stub['query'], @options[:raw_query] || '') }
|
52
|
-
end
|
53
|
-
|
54
|
-
def filter_stubs(stubs)
|
55
|
-
result = filter_by_query(filter_by_headers(filter_by_method(stubs[@path] || [])))
|
56
|
-
result[0]['response'] unless result.empty?
|
57
|
-
end
|
58
|
-
|
59
35
|
class << self
|
60
36
|
attr_reader :stubs
|
61
37
|
|
@@ -98,28 +74,10 @@ module TShield
|
|
98
74
|
stubs[session_name][item['path']] << item
|
99
75
|
end
|
100
76
|
|
101
|
-
def
|
102
|
-
|
103
|
-
stub_headers ||= {}
|
104
|
-
result = stub_headers.reject { |key, value| request_headers[key.to_rack_name] == value }
|
105
|
-
result.empty? || stub_headers.empty?
|
106
|
-
end
|
107
|
-
|
108
|
-
def include_query(stub_query, raw_query)
|
109
|
-
request_query = build_query_hash(raw_query)
|
110
|
-
stub_query ||= {}
|
111
|
-
result = stub_query.reject { |key, value| request_query[key] == value.to_s }
|
112
|
-
result.empty? || stub_query.empty?
|
113
|
-
end
|
114
|
-
|
115
|
-
def build_query_hash(raw_query)
|
116
|
-
params = {}
|
117
|
-
raw_query.split('&').each do |query|
|
118
|
-
key, value = query.split('=')
|
119
|
-
params[key] = value
|
120
|
-
end
|
77
|
+
def read_body(content)
|
78
|
+
return content.to_json if content.is_a? Hash
|
121
79
|
|
122
|
-
|
80
|
+
content
|
123
81
|
end
|
124
82
|
end
|
125
83
|
end
|
data/lib/tshield/version.rb
CHANGED
@@ -19,6 +19,19 @@
|
|
19
19
|
"body": "body content"
|
20
20
|
}
|
21
21
|
},
|
22
|
+
{
|
23
|
+
"method": "GET",
|
24
|
+
"path": "/matching/example.json",
|
25
|
+
"response": {
|
26
|
+
"status": 200,
|
27
|
+
"headers": {
|
28
|
+
"Content-Type": "application/json"
|
29
|
+
},
|
30
|
+
"body": {
|
31
|
+
"json": "content"
|
32
|
+
}
|
33
|
+
}
|
34
|
+
},
|
22
35
|
{
|
23
36
|
"method": "POST",
|
24
37
|
"path": "/matching/example",
|
@@ -102,6 +102,18 @@ describe TShield::RequestMatching do
|
|
102
102
|
expect(@response.status).to eql(201)
|
103
103
|
end
|
104
104
|
end
|
105
|
+
context 'on match by path and returns json' do
|
106
|
+
before :each do
|
107
|
+
@request_matching = TShield::RequestMatching
|
108
|
+
.new('/matching/example.json', method: 'GET')
|
109
|
+
end
|
110
|
+
it 'should return response object' do
|
111
|
+
@response = @request_matching.match_request
|
112
|
+
expect(@response.body).to eql('{"json":"content"}')
|
113
|
+
expect(@response.headers).to eql('Content-Type' => 'application/json')
|
114
|
+
expect(@response.status).to eql(200)
|
115
|
+
end
|
116
|
+
end
|
105
117
|
context 'on match by path and method and query' do
|
106
118
|
before :each do
|
107
119
|
@request_matching = TShield::RequestMatching
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tshield
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.11.
|
4
|
+
version: 0.11.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego Rubin
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-09-
|
12
|
+
date: 2019-09-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: byebug
|
@@ -354,6 +354,7 @@ files:
|
|
354
354
|
- lib/tshield/counter.rb
|
355
355
|
- lib/tshield/extensions/string_extensions.rb
|
356
356
|
- lib/tshield/logger.rb
|
357
|
+
- lib/tshield/matching/filters.rb
|
357
358
|
- lib/tshield/options.rb
|
358
359
|
- lib/tshield/request.rb
|
359
360
|
- lib/tshield/request_matching.rb
|