api_sim 3.1.0 → 3.2.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
  SHA1:
3
- metadata.gz: 4cf6e3d93abfa7755f45ddc06e60609e6a6a7bcd
4
- data.tar.gz: e5c2e34b46e497cc7245ccb4bcc3d90310a0c28b
3
+ metadata.gz: 39744282ba52dd613a6cd9481717892a39482f3b
4
+ data.tar.gz: 67e3e2d58c0e5e2110521924ddc5c99114a5ce83
5
5
  SHA512:
6
- metadata.gz: ff1de54e38862120efbd8399d97168df9f8e75aab2222ca1efdd94dbed61ef20c6effd20679f25be36fe0436c2c005c54686fbd8d731c96ad7ad4e8341e4566a
7
- data.tar.gz: b6c5a64d7f2a325de35ae9395779b5e8d4cc9c59ab8cb0bd3a8d6b3897f973fb85f62a192530b0d7a7eaed3caf99d1f4aff796d007a1d2105054865e11968643
6
+ metadata.gz: a88815795d88e7c6f6adc25dfff2b2e7f79c1eda450079d212e7f7fddab4adb20ffc29e2a5b9aa50aee4bbc81c14f67c9ebf92ca1d842740c3ff8394c9dc0348
7
+ data.tar.gz: 738f62d178070ebebe67302d4762aae8c40c34eb8c74834520a3654e2b9bc3139f67edda7c9ad5a9902f56bff186f25a7674f540875eb89450f3e693bed29a40
data/README.md CHANGED
@@ -52,6 +52,14 @@ cd examples/basic && bundle check || bundle install && bundle exec rackup -I../.
52
52
 
53
53
  After which the simulators should be running on port 9292.
54
54
 
55
+ ## API
56
+
57
+ The API that these simulators generate can get pretty smart. To help you, the user,
58
+ distinguish between failures and "smarts", we've made up an HTTP status code: 498.
59
+ This code means "we received a request that did not match an expected schema". If you
60
+ provide the simulators with a request schema for an endpoint, all requests must match
61
+ that schema. If they do not, they'll receive our fictional status code.
62
+
55
63
  ## Development
56
64
 
57
65
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -12,7 +12,7 @@ module ApiSim
12
12
  end
13
13
  end
14
14
 
15
- def configure_endpoint(http_method, route, response_body, response_code=200, headers={}, schema_string='')
15
+ def configure_endpoint(http_method, route, response_body, response_code=200, headers={}, schema_string='', request_schema: nil)
16
16
  endpoint_configurations.push(
17
17
  Matchers::StaticRequestMatcher.new(
18
18
  http_method: http_method,
@@ -21,7 +21,8 @@ module ApiSim
21
21
  headers: headers,
22
22
  default: true,
23
23
  response_body: response_body,
24
- schema: schema_string
24
+ schema: schema_string,
25
+ request_schema: request_schema
25
26
  )
26
27
  )
27
28
  end
@@ -41,7 +41,6 @@ module ApiSim
41
41
  return erb :'responses/form.html', layout: :'layout.html'
42
42
  end
43
43
  end
44
-
45
44
  new_config = create_matcher_override(mimicked_request)
46
45
 
47
46
  self.class.endpoints.unshift(new_config)
@@ -69,11 +68,10 @@ module ApiSim
69
68
  ''
70
69
  end
71
70
 
72
-
73
71
  get '/requests/*' do
74
72
  endpoint_name = parse_endpoint_from_request(request) || halt(404)
75
- http_method = params.fetch('method', 'GET')
76
- endpoint = find_matching_endpoint(endpoint_name, http_method)
73
+ http_method = params.fetch('method', 'GET')
74
+ endpoint = find_matching_endpoint(endpoint_name, http_method)
77
75
 
78
76
  halt(404) unless endpoint
79
77
  endpoint.requests.to_json
@@ -82,6 +80,7 @@ module ApiSim
82
80
  %i(get post put patch delete options).each do |http_method|
83
81
  public_send(http_method, '/*') do
84
82
  endpoint = matcher(request)
83
+ halt(498) unless schema_validates?(endpoint)
85
84
  endpoint.record_request(request)
86
85
  endpoint.response(request)
87
86
  end
@@ -95,12 +94,12 @@ module ApiSim
95
94
  end
96
95
 
97
96
  case matching_endpoints.size
98
- when 0
99
- nil
100
- when 1
101
- matching_endpoints.first
102
- else
103
- matching_endpoints.find { |endpoint| endpoint.http_method =~ /#{http_method}/i }
97
+ when 0
98
+ nil
99
+ when 1
100
+ matching_endpoints.first
101
+ else
102
+ matching_endpoints.find { |endpoint| endpoint.http_method =~ /#{http_method}/i }
104
103
  end
105
104
  end
106
105
 
@@ -121,7 +120,8 @@ module ApiSim
121
120
  headers: parsed_body.fetch('headers', old_config[1]),
122
121
  response_body: parsed_body.fetch('body', old_config[2]),
123
122
  matcher: parsed_body.fetch('match', ''),
124
- schema: parsed_body.fetch('schema', '')
123
+ schema: parsed_body.fetch('schema', ''),
124
+ request_schema: parsed_body.fetch('request-schema', nil)
125
125
  )
126
126
  end
127
127
 
@@ -172,8 +172,20 @@ module ApiSim
172
172
  end
173
173
 
174
174
  def parse_endpoint_from_request(request)
175
- path_matcher = request.path.match(/\/requests\/([\w\/_-]*)/)
175
+ path_matcher = request.path.match(/\/requests\/([\w\/_-]*)/)
176
176
  path_matcher.size == 2 ? path_matcher[1] : nil
177
177
  end
178
+
179
+ def schema_validates?(endpoint)
180
+ if blank?(endpoint.request_schema) || request.env['CONTENT_TYPE'] != 'application/json'
181
+ true
182
+ else
183
+ JSON::Validator.validate_json(JSON.parse(endpoint.request_schema), parsed_body.to_json)
184
+ end
185
+ end
186
+
187
+ def blank?(item)
188
+ item.nil? || item.empty?
189
+ end
178
190
  end
179
191
  end
@@ -5,6 +5,7 @@ module ApiSim
5
5
  class BaseMatcher
6
6
  DEFAULT_RACK_RESPONSE=[200, {}, '']
7
7
  ALWAYS_TRUE_MATCHER = ->(request) { true }
8
+ attr_reader :request_schema
8
9
 
9
10
  def custom_matcher?
10
11
  matcher != ALWAYS_TRUE_MATCHER
@@ -6,15 +6,16 @@ module ApiSim
6
6
  class StaticRequestMatcher < BaseMatcher
7
7
  attr_reader :http_method, :route, :headers, :response_code, :matcher, :response_body, :default, :schema
8
8
 
9
- def initialize(http_method:, route:, response_code: 200, response_body: '', headers: {}, default: false, matcher: ALWAYS_TRUE_MATCHER, schema: nil)
10
- @default = default
11
- @matcher = matcher
12
- @headers = headers
13
- @response_body = response_body
14
- @response_code = response_code
15
- @route = route
16
- @http_method = http_method
17
- @schema = schema
9
+ def initialize(**args)
10
+ @default = args.fetch(:default, false)
11
+ @matcher = args.fetch(:matcher, ALWAYS_TRUE_MATCHER)
12
+ @headers = args.fetch(:headers, {})
13
+ @response_body = args.fetch(:response_body, '')
14
+ @response_code = args.fetch(:response_code, 200)
15
+ @route = args.fetch(:route)
16
+ @http_method = args.fetch(:http_method)
17
+ @schema = args.fetch(:schema, nil)
18
+ @request_schema = args.fetch(:request_schema, nil)
18
19
  end
19
20
 
20
21
  def matches?(request)
@@ -18,7 +18,7 @@ module ApiSim
18
18
  response_code: overrides.fetch(:response_code),
19
19
  headers: overrides.fetch(:headers),
20
20
  response_body: overrides.fetch(:response_body),
21
- body_matches: overrides.fetch('matcher', old_matcher.matcher)
21
+ body_matches: overrides.fetch('matcher', old_matcher.matcher),
22
22
  )
23
23
  else
24
24
  Matchers::StaticRequestMatcher.new(
@@ -28,6 +28,7 @@ module ApiSim
28
28
  headers: overrides.fetch(:headers),
29
29
  response_body: overrides.fetch(:response_body),
30
30
  schema: overrides.fetch(:schema),
31
+ request_schema: overrides.fetch(:request_schema)
31
32
  )
32
33
  end
33
34
  end
@@ -1,3 +1,3 @@
1
1
  module ApiSim
2
- VERSION = "3.1.0"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -32,7 +32,11 @@
32
32
 
33
33
  <div class="form-group">
34
34
  <label for="schema">Response schema</label>
35
- <textarea type="text" class="form-control" id="schema" name="schema" data-prettify rows="25"><%= h (params['schema'] || config.schema) %></textarea>
35
+ <textarea type="text" class="form-control" id="schema" name="schema" data-prettify rows="25"><%= h (params[:schema] || config.schema) %></textarea>
36
+ </div>
37
+ <div class="form-group">
38
+ <label for="request-schema">Request schema</label>
39
+ <textarea class="form-control" id="request-schema" name="request-schema" data-prettify rows="12"><%= h (params[:request_schema] || config.request_schema) %></textarea>
36
40
  </div>
37
41
  </div>
38
42
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_sim
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-29 00:00:00.000000000 Z
11
+ date: 2016-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra