tshield 0.11.7.0 → 0.11.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd161c62a3f2ed18d05c36ba7bacfff84a96f92fa7460d9702611af7ac60f33a
4
- data.tar.gz: 046ff96f18c8504bce45c7cac21457b75fe46c6f836031279ec481e1e6a4a44c
3
+ metadata.gz: d2f5af220b6e8aed08b79c006af7975d48416a651c24345b30d20ac7db67e5ca
4
+ data.tar.gz: a1cc446bac420e431ec099b027c624e33bb5bcd8291605bdd6876d5aaa04b0b7
5
5
  SHA512:
6
- metadata.gz: ce930e5ee1b0a6964c1e6a4255236fb4925fbc87779729aefdc594016a2d71d69afd2ad5b7ada1083314fb2eab8aefb9ce121181f44b2b2a0e957edf10a277bb
7
- data.tar.gz: 585dc9bf34107431b2d93cfc46fe92ef2584f4614d0de9c82a41659f855e374a88cc441b4eedfabffa79aa1b4f17151594f98f4ef0daaa3f095a7ce688eef7de
6
+ metadata.gz: c4bcbfd8ce65cac9dfb6db41879f9237b6aa26b865b5fb9fee27423ea2352a81d5bf713812b2699d5656df933f2bfe96546ad5cd767b0177cf41226dfc7e2539
7
+ data.tar.gz: 18b671f8d124aa3237051ed9e516054ddb466510a192107c9016cdba8e11111d770e1603e788daa13218ec8f02dac13d73b14e6a4da7c19463805b0c9431758c
data/README.md CHANGED
@@ -10,22 +10,22 @@ TShield
10
10
  ## API mocks for development and testing
11
11
  TShield is an open source proxy for mocks API responses.
12
12
 
13
- * REST
14
- * SOAP
15
- * Session manager to separate multiple scenarios (success, error, sucess variation, ...)
16
- * Lightweight
17
- * MIT license
13
+ * REST
14
+ * SOAP
15
+ * Session manager to separate multiple scenarios (success, error, sucess variation, ...)
16
+ * Lightweight
17
+ * MIT license
18
18
 
19
- #### Table of Contents:
20
-
21
- * [Basic Usage](#basic-usage)
22
- * [Config options for Pattern Matching](#config-options-for-pattern-matching)
23
- * [Config options for VCR](#config-options-for-vcr)
24
- * [Manage Sessions](#manage-sessions)
25
- * [Custom controllers](#custom-controllers)
26
- * [Features](#features)
27
- * [Examples](#examples)
28
- * [Contributing](#contributing)
19
+ ## Table of Contents
20
+
21
+ * [Basic Usage](#basic-usage)
22
+ * [Config options for Pattern Matching](#config-options-for-pattern-matching)
23
+ * [Config options for VCR](#config-options-for-vcr)
24
+ * [Manage Sessions](#manage-sessions)
25
+ * [Custom controllers](#custom-controllers)
26
+ * [Features](#features)
27
+ * [Examples](#examples)
28
+ * [Contributing](#contributing)
29
29
 
30
30
  ## Basic Usage
31
31
  ### Install
@@ -42,8 +42,8 @@ Default port is `4567`
42
42
 
43
43
  #### Command Line Options
44
44
 
45
- * __-port__: Overwrite default port (4567)
46
- * __-help__: Show all command line options
45
+ * **-port**: Overwrite default port (4567)
46
+ * **-help**: Show all command line options
47
47
 
48
48
  #### Config example
49
49
 
@@ -75,37 +75,38 @@ All files should be in `matching` directory.
75
75
  Each file should be a valid JSON array of objects and each object must contain
76
76
  at least the following attributes:
77
77
 
78
- * __method__: a http method.
79
- * __path__: url path.
80
- * __response__: object with response data. Into session can be used an array of objects to return different responses like vcr mode. See example: [multiples_response.json](https://github.com/diegorubin/tshield/blob/master/component_tests/matching/examples/multiple_response.json)
78
+ * **method**: a http method.
79
+ * **path**: url path. This attribute accept regex, see example in [regex.json](https://github.com/diegorubin/tshield/blob/master/component_tests/matching/examples/regex.json)
80
+ * **response**: object with response data. Into session can be used an array of objects to return different responses like vcr mode. See example: [multiples_response.json](https://github.com/diegorubin/tshield/blob/master/component_tests/matching/examples/multiple_response.json). External file can be used as body content, see example in [file.json](https://github.com/diegorubin/tshield/blob/master/component_tests/matching/examples/file.json).
81
81
 
82
82
  Response must be contain the following attributes:
83
83
 
84
- * __headers__: key value object with expected headers to match. In the evaluation process
85
- this stub will be returned if all headers are in request.
86
- * __status__: integer used http status respose.
87
- * __body__: content to be returned.
84
+ * **headers**: key value object with expected headers to match. In the evaluation process
85
+ this stub will be returned if all headers are in request.
86
+ * **status**: integer used http status respose.
87
+ * **body**: content to be returned.
88
88
 
89
89
  Optional request attributes:
90
90
 
91
- * __headers__: key value object with expected headers to match. In the evaluation process
92
- this stub will be returned if all headers are in request.
93
- * __query__: works like headers but use query params.
91
+ * **headers**: key value object with expected headers to match. In the evaluation process
92
+ this stub will be returned if all headers are in request.
93
+ * **query**: works like headers but use query params.
94
94
 
95
- __Important__: If VCR config conflicts with Matching config Matching will be
95
+ **Important**: If VCR config conflicts with Matching config Matching will be
96
96
  used. Matching config have priority.
97
97
 
98
98
  ### Session Configuration
99
99
 
100
100
  To register stub into a session create an object with following attributes:
101
101
 
102
- * __session__: name of session.
103
- * __stubs__: an array with objects described above.
102
+ * **session**: name of session.
103
+ * **stubs**: an array with objects described above.
104
104
 
105
105
  ### Example of matching configuration
106
106
 
107
107
  ```json
108
- [{
108
+ [
109
+ {
109
110
  "method": "GET",
110
111
  "path": "/matching/example",
111
112
  "query": {
@@ -205,19 +206,19 @@ domains:
205
206
  - /users
206
207
  ```
207
208
  **request**
208
- * **timeout**: wait time for real service in seconds
209
- * **verify_ssl**: ignores invalid ssl if false
209
+ * **timeout**: wait time for real service in seconds
210
+ * **verify_ssl**: ignores invalid ssl if false
210
211
 
211
212
  **domain**
212
- * Define Base URI of service
213
- * **name**: Name to identify the domain in the generated files
214
- * **headers**: github-issue #17
215
- * **not_save_headers**: List of headers that should be ignored in generated file
216
- * **skip_query_params**: List of query params that should be ignored in generated file
217
- * **cache_request**: <<some_description>>
218
- * **filters**: Implementation of before or after filters used in domain requests
219
- * **excluded_headers**: <<some_description>>
220
- * **paths**: Paths list of all services that will be called. Used to filter what domain will "receive the request"
213
+ * Define Base URI of service
214
+ * **name**: Name to identify the domain in the generated files
215
+ * **headers**: github-issue #17
216
+ * **not_save_headers**: List of headers that should be ignored in generated file
217
+ * **skip_query_params**: List of query params that should be ignored in generated file
218
+ * **cache_request**: <<some_description>>
219
+ * **filters**: Implementation of before or after filters used in domain requests
220
+ * **excluded_headers**: <<some_description>>
221
+ * **paths**: Paths list of all services that will be called. Used to filter what domain will "receive the request"
221
222
 
222
223
  ## Manage Sessions
223
224
 
@@ -282,8 +283,8 @@ This features files are used as base for the component tests.
282
283
  #### Basic example for a client app requesting an API
283
284
  [examples/client-api-nodejs](examples/client-api-nodejs)
284
285
 
285
- #### Basic example for component/acceptance test
286
- **[WIP]**
286
+ #### Basic example for component/acceptance test using tshield sessions
287
+ [examples/component-test](examples/component-test)
287
288
 
288
289
  ## Contributing
289
290
  [Hacking or Contributing to TShield](CONTRIBUTING.md)
@@ -5,5 +5,6 @@ require 'tshield/options'
5
5
  require 'tshield/simple_tcp_server'
6
6
  require 'tshield/server'
7
7
 
8
+ # TShield: API mocks for development and testing
8
9
  module TShield
9
10
  end
@@ -23,8 +23,14 @@ module TShield
23
23
  stubs.select { |stub| Filters.include_query(stub['query'], @options[:raw_query] || '') }
24
24
  end
25
25
 
26
+ def filter_by_path(stubs)
27
+ stubs.each do |key, value|
28
+ return value if @path =~ /^#{key}$/
29
+ end
30
+ end
31
+
26
32
  def filter_stubs(stubs)
27
- result = filter_by_query(filter_by_headers(filter_by_method(stubs[@path] || [])))
33
+ result = filter_by_query(filter_by_headers(filter_by_method(filter_by_path(stubs))))
28
34
  result[0]['response'] unless result.empty?
29
35
  end
30
36
 
@@ -87,9 +87,14 @@ module TShield
87
87
 
88
88
  def read_body(content)
89
89
  return content.to_json if content.is_a? Hash
90
+ return read_file_content(content) if content =~ %r{^FILE://}
90
91
 
91
92
  content
92
93
  end
94
+
95
+ def read_file_content(content)
96
+ File.open(File.join('matching', content.gsub('FILE://', '')), 'r').read
97
+ end
93
98
  end
94
99
  end
95
100
  end
@@ -5,7 +5,7 @@ module TShield
5
5
  class Version
6
6
  MAJOR = 0
7
7
  MINOR = 11
8
- PATCH = 7
8
+ PATCH = 8
9
9
  PRE = 0
10
10
 
11
11
  class << self
@@ -53,6 +53,24 @@
53
53
  "body": "post content"
54
54
  }
55
55
  },
56
+ {
57
+ "method": "GET",
58
+ "path": "/matching/file.txt",
59
+ "response": {
60
+ "status": 200,
61
+ "headers": {},
62
+ "body": "FILE://body.json"
63
+ }
64
+ },
65
+ {
66
+ "method": "GET",
67
+ "path": "/matching/regex/\\d+",
68
+ "response": {
69
+ "status": 200,
70
+ "headers": {},
71
+ "body": "stub with regex"
72
+ }
73
+ },
56
74
  {
57
75
  "method": "GET",
58
76
  "path": "/matching/twice",
@@ -160,6 +160,43 @@ describe TShield::RequestMatching do
160
160
  expect(response.status).to eql(200)
161
161
  end
162
162
  end
163
+ context 'on match have file reference' do
164
+ before :each do
165
+ request_matching = TShield::RequestMatching
166
+ .new('/matching/file.txt',
167
+ method: 'GET')
168
+
169
+ file_content_double = double
170
+ allow(File).to receive(:join).with('matching', 'body.json')
171
+ .and_return('matching/body.json')
172
+ allow(File).to receive(:open).with('matching/body.json', 'r')
173
+ .and_return(file_content_double)
174
+
175
+ allow(file_content_double).to receive(:read).and_return("line1\nline2")
176
+
177
+ @response = request_matching.match_request
178
+ end
179
+
180
+ it 'should return content of file' do
181
+ expect(@response.body).to eql("line1\nline2")
182
+ expect(@response.headers).to eql({})
183
+ expect(@response.status).to eql(200)
184
+ end
185
+ end
186
+ context 'on match with regex' do
187
+ before :each do
188
+ @response = request_matching = TShield::RequestMatching
189
+ .new('/matching/regex/1234',
190
+ method: 'GET').match_request
191
+ end
192
+
193
+ it 'should return content of file' do
194
+ expect(@response.body).to eql('stub with regex')
195
+ expect(@response.headers).to eql({})
196
+ expect(@response.status).to eql(200)
197
+ end
198
+ end
199
+
163
200
  context 'on not match' do
164
201
  before :each do
165
202
  @request_matching = TShield::RequestMatching.new('/')
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.7.0
4
+ version: 0.11.8.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-08 00:00:00.000000000 Z
12
+ date: 2019-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: byebug