swagalicious 0.5.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5ca94c771cd7880a7ca5067494dbeb2191f24135cac0632a1a80b4b446b44c5
4
- data.tar.gz: 50be7a011f75c16a812d7674ab226abe1506b20e08f86e8d9f9385887bb4c9f2
3
+ metadata.gz: 7b591f97affd8dc1fa23c0ba79eb43aeccfc0b954d6a8eb8a3dbaae10d4e5da8
4
+ data.tar.gz: 6d5b20f69b8543406a99e5d1e59848d02b1299558f14be5f9023e473a7b1e903
5
5
  SHA512:
6
- metadata.gz: 11eaddf8649effe8652269a2257a9438c766960c59e00d406172daa33639b10a4ba9bae05f6d792d3ca7feb4602c4a230e0c13cc7489e45d771496a3679f9a19
7
- data.tar.gz: 9f187376ca50e6ea1333c432c88cbb872f6c0ba841be7018957624b5c01105bb7b4b897483ab45503f37e8b80c42abf3d0c5613f8b65b8fc84eec3a70539a37e
6
+ metadata.gz: 233c014d7df4f7625275193eda7fe58ae8cfd7ad3dea95ce8a4e19aabad3d9b9ef4ad082042bc4ba873d698a64b0b262298f2859fc13f1408729462dc478a6dc
7
+ data.tar.gz: 93f9dcb0fb104aaac7a54cfbda195dcd27d8b47598f7176cc65f6b5b0f0ac6b260f2f760ee7b9355ad429c91c1ebf164bff5ae28c7aace7686559ea9b89b6095
data/Gemfile.lock CHANGED
@@ -1,12 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swagalicious (0.5.3)
4
+ swagalicious (1.0.0)
5
5
  faraday (~> 2.0)
6
6
  faraday-rack
7
7
  json-schema
8
8
  oj (~> 3.11.0)
9
- rack-test (~> 2.0.2)
9
+ ox (~> 2.14.14)
10
+ rack-test
10
11
  rspec (~> 3)
11
12
 
12
13
  GEM
@@ -20,6 +21,7 @@ GEM
20
21
  zeitwerk (~> 2.3)
21
22
  addressable (2.8.1)
22
23
  public_suffix (>= 2.0.2, < 6.0)
24
+ ast (2.4.2)
23
25
  byebug (11.1.3)
24
26
  coderay (1.1.3)
25
27
  concurrent-ruby (1.1.10)
@@ -34,11 +36,16 @@ GEM
34
36
  faraday (~> 2.0)
35
37
  i18n (1.10.0)
36
38
  concurrent-ruby (~> 1.0)
39
+ json (2.6.3)
37
40
  json-schema (3.0.0)
38
41
  addressable (>= 2.8)
39
42
  method_source (1.0.0)
40
43
  minitest (5.16.1)
41
44
  oj (3.11.8)
45
+ ox (2.14.14)
46
+ parallel (1.22.1)
47
+ parser (3.2.1.0)
48
+ ast (~> 2.4.1)
42
49
  pry (0.13.1)
43
50
  coderay (~> 1.1)
44
51
  method_source (~> 1.0)
@@ -49,7 +56,10 @@ GEM
49
56
  rack (3.0.4.1)
50
57
  rack-test (2.0.2)
51
58
  rack (>= 1.3)
59
+ rainbow (3.1.1)
52
60
  rake (12.3.3)
61
+ regexp_parser (2.7.0)
62
+ rexml (3.2.5)
53
63
  rspec (3.11.0)
54
64
  rspec-core (~> 3.11.0)
55
65
  rspec-expectations (~> 3.11.0)
@@ -63,9 +73,28 @@ GEM
63
73
  diff-lcs (>= 1.2.0, < 2.0)
64
74
  rspec-support (~> 3.11.0)
65
75
  rspec-support (3.11.0)
76
+ rubocop (1.48.0)
77
+ json (~> 2.3)
78
+ parallel (~> 1.10)
79
+ parser (>= 3.2.0.0)
80
+ rainbow (>= 2.2.2, < 4.0)
81
+ regexp_parser (>= 1.8, < 3.0)
82
+ rexml (>= 3.2.5, < 4.0)
83
+ rubocop-ast (>= 1.26.0, < 2.0)
84
+ ruby-progressbar (~> 1.7)
85
+ unicode-display_width (>= 2.4.0, < 3.0)
86
+ rubocop-ast (1.27.0)
87
+ parser (>= 3.2.1.0)
88
+ rubocop-capybara (2.17.1)
89
+ rubocop (~> 1.41)
90
+ rubocop-rspec (2.19.0)
91
+ rubocop (~> 1.33)
92
+ rubocop-capybara (~> 2.17)
93
+ ruby-progressbar (1.13.0)
66
94
  ruby2_keywords (0.0.5)
67
95
  tzinfo (2.0.4)
68
96
  concurrent-ruby (~> 1.0)
97
+ unicode-display_width (2.4.2)
69
98
  zeitwerk (2.6.0)
70
99
 
71
100
  PLATFORMS
@@ -76,6 +105,8 @@ DEPENDENCIES
76
105
  pry-byebug (~> 3.9.0)
77
106
  rake (~> 12.0)
78
107
  rspec (~> 3.0)
108
+ rubocop (~> 1.48.0)
109
+ rubocop-rspec (~> 2.19.0)
79
110
  swagalicious!
80
111
 
81
112
  BUNDLED WITH
@@ -1,7 +1,11 @@
1
1
  require "faraday"
2
2
  require "faraday/rack"
3
3
  require "rack"
4
+ require "json"
4
5
  require "oj"
6
+ require "ox"
7
+ require "yaml"
8
+ require "active_support/core_ext/hash/indifferent_access"
5
9
 
6
10
  require_relative "response_validator"
7
11
 
@@ -9,6 +13,55 @@ class Swagalicious
9
13
  module ExampleHelpers
10
14
  include Rack::Test::Methods
11
15
 
16
+ def self.raise_invalid_response(response:, request:, message:)
17
+ raise InvalidResponseTypeError.new(
18
+ headers: response.headers,
19
+ message: message,
20
+ request: request,
21
+ response: response,
22
+ status: response.status,
23
+ )
24
+ end
25
+
26
+ class InvalidResponseTypeError < RuntimeError
27
+ attr_reader :status, :_message, :headers, :response, :request
28
+
29
+ def initialize(status:, message:, headers:, response:, request:)
30
+ @headers = headers
31
+ @_message = message
32
+ @request = request
33
+ @response = response
34
+ @status = status
35
+ end
36
+
37
+ def inspect
38
+ JSON.pretty_generate(to_h)
39
+ end
40
+
41
+ def to_h
42
+ hash = {
43
+ headers: headers.to_h,
44
+ message: _message,
45
+ request: request.slice(:verb, :path, :headers),
46
+ status: status,
47
+ }
48
+
49
+ if parsed_body = Parser.new(request: request, response: response).parse(raise_on_invalid: false)
50
+ hash[:parsed_response] = parsed_body
51
+ end
52
+
53
+ hash
54
+ end
55
+
56
+ def to_s
57
+ "Received unexpected or unparseable response with status code #{status} for #{request[:verb].upcase} #{request[:path]}: #{_message}"
58
+ end
59
+
60
+ def message
61
+ inspect
62
+ end
63
+ end
64
+
12
65
  class MockResponse
13
66
  attr_reader :body, :status, :headers
14
67
 
@@ -19,6 +72,44 @@ class Swagalicious
19
72
  end
20
73
  end
21
74
 
75
+ class Parser
76
+ attr_accessor :body
77
+ attr_reader :content_type, :request, :response
78
+
79
+ def initialize(response:, request:)
80
+ @content_type = response.headers["Content-Type"]
81
+ @body = response.body
82
+ @response = response
83
+ @request = request
84
+ end
85
+
86
+ def parse(raise_on_invalid: true)
87
+ # Redirections shouldnt be parsed
88
+ if response.status >= 300 && response.status <= 399
89
+ return
90
+ end
91
+
92
+ case content_type
93
+ when /json/
94
+ self.body = "{}" if self.body.empty?
95
+
96
+ Oj.load(self.body, symbol_keys: true)
97
+ when /ya?ml/
98
+ body = "---" if self.body.empty?
99
+
100
+ (YAML.load(self.body) || {}).with_indifferent_access
101
+ when /xml/
102
+ (Ox.load(self.body, mode: :hash_no_attrs) || {}).with_indifferent_access
103
+ when "", nil
104
+ self.body
105
+ else
106
+ return unless raise_on_invalid
107
+
108
+ Swagalicious::ExampleHelpers.raise_invalid_response(response: response, request: request, message: "Invalid Content-Type header #{response.headers["Content-Type"]}")
109
+ end
110
+ end
111
+ end
112
+
22
113
  def app
23
114
  @app ||= if defined?(Rails)
24
115
  Rails.application
@@ -52,10 +143,9 @@ class Swagalicious
52
143
  end
53
144
  end
54
145
 
55
- body = response.body
56
- body = "{}" if body.empty?
146
+ Swagalicious::ExampleHelpers.raise_invalid_response(response: response, request: request, message: "Received unexpected response code") unless response.status.to_s == metadata[:response][:code].to_s
57
147
 
58
- @body = Oj.load(body, symbol_keys: true)
148
+ @body = Parser.new(request: request, response: response).parse
59
149
 
60
150
  metadata[:paths] ||= []
61
151
  metadata[:paths] << request[:path]
@@ -35,11 +35,11 @@ class Swagalicious
35
35
 
36
36
  def derive_security_params(metadata, swagger_doc)
37
37
  requirements = metadata[:operation][:security] || swagger_doc[:security] || []
38
- scheme_names = requirements.flat_map(&:keys)
38
+ scheme_names = requirements.flat_map { |r| r.has_key?(:type) ? r[:type] : r.keys }
39
39
  schemes = security_version(scheme_names, swagger_doc)
40
40
 
41
41
  schemes.map do |scheme|
42
- param = (scheme[:type] == :apiKey) ? scheme.slice(:name, :in, :required) : { name: "Authorization", in: :header }
42
+ param = (scheme[:type] == :apiKey) ? scheme.slice(:name, :in, :required, :force_name) : { name: "Authorization", in: :header }
43
43
  param.merge(required: requirements.one?) unless param.has_key?(:required)
44
44
  param.merge(type: :string)
45
45
  end
@@ -127,7 +127,7 @@ class Swagalicious
127
127
  .map do |p|
128
128
  variable_name = p[:variable_name] || p[:name]
129
129
 
130
- [p[:name], example.send(variable_name).to_s]
130
+ [p[:name], example.send(variable_name).to_s, p[:force_name]]
131
131
  end
132
132
 
133
133
  # Accept header
@@ -145,15 +145,16 @@ class Swagalicious
145
145
  end
146
146
 
147
147
  # Rails test infrastructure requires rackified headers
148
- rackified_tuples = tuples.map do |pair|
148
+ rackified_tuples = tuples.map do |tuple|
149
149
  [
150
- case pair[0]
150
+ case tuple[0]
151
151
  when "Accept" then "HTTP_ACCEPT"
152
152
  when "Content-Type" then "CONTENT_TYPE"
153
- when "Authorization" then "HTTP_AUTHORIZATION"
154
- else pair[0]
153
+ when "Authorization"
154
+ tuple[2] ? tuple[0] : "HTTP_AUTHORIZATION"
155
+ else tuple[0]
155
156
  end,
156
- pair[1]
157
+ tuple[1]
157
158
  ]
158
159
  end
159
160
 
@@ -1,3 +1,3 @@
1
1
  class Swagalicious
2
- VERSION = "0.5.3"
2
+ VERSION = "1.1.0"
3
3
  end
data/swagalicious.gemspec CHANGED
@@ -28,9 +28,12 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency "faraday-rack"
29
29
  spec.add_dependency "json-schema"
30
30
  spec.add_dependency "oj", "~> 3.11.0"
31
- spec.add_dependency "rack-test", "~> 2.0.2"
31
+ spec.add_dependency "ox", "~> 2.14.14"
32
+ spec.add_dependency "rack-test"
32
33
  spec.add_dependency "rspec", "~> 3"
33
34
 
35
+ spec.add_development_dependency "rubocop", "~> 1.48.0"
36
+ spec.add_development_dependency "rubocop-rspec", "~> 2.19.0"
34
37
  spec.add_development_dependency "factory_bot", "~> 6.1.0"
35
38
  spec.add_development_dependency "pry-byebug", "~> 3.9.0"
36
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagalicious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Howe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-19 00:00:00.000000000 Z
11
+ date: 2023-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -67,19 +67,33 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.11.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: rack-test
70
+ name: ox
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 2.0.2
75
+ version: 2.14.14
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 2.0.2
82
+ version: 2.14.14
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,34 @@ dependencies:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
110
  version: '3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.48.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.48.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 2.19.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 2.19.0
97
139
  - !ruby/object:Gem::Dependency
98
140
  name: factory_bot
99
141
  requirement: !ruby/object:Gem::Requirement