webspicy 0.15.8 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +76 -24
  3. data/bin/webspicy +30 -14
  4. data/examples/restful/Gemfile.lock +40 -24
  5. data/examples/restful/Rakefile +0 -1
  6. data/examples/restful/app.rb +4 -1
  7. data/examples/restful/webspicy/config.rb +9 -0
  8. data/examples/restful/webspicy/{todo/deleteTodo.yml → formaldef/todo/_one/delete.yml} +7 -1
  9. data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +46 -0
  10. data/examples/restful/webspicy/{todo/getTodo.yml → formaldef/todo/_one/get.yml} +0 -0
  11. data/examples/restful/webspicy/{todo/patchTodo.yml → formaldef/todo/_one/patch.yml} +3 -0
  12. data/examples/restful/webspicy/{todo/putTodo.yml → formaldef/todo/_one/put.yml} +0 -0
  13. data/examples/restful/webspicy/{todo/getTodos.yml → formaldef/todo/get.yml} +0 -0
  14. data/examples/restful/webspicy/{todo → formaldef/todo}/options.yml +1 -1
  15. data/examples/restful/webspicy/{todo/postCsv.yml → formaldef/todo/post.csv.yml} +0 -0
  16. data/examples/restful/webspicy/{todo/postFile.yml → formaldef/todo/post.file.yml} +1 -1
  17. data/examples/restful/webspicy/{todo/postTodos.yml → formaldef/todo/post.yml} +0 -0
  18. data/examples/restful/webspicy/{todo → formaldef/todo}/todos.csv +0 -0
  19. data/examples/restful/webspicy/rack.rb +1 -1
  20. data/examples/restful/webspicy/real.rb +1 -1
  21. data/examples/restful/webspicy/schema.fio +2 -2
  22. data/examples/restful/webspicy/support/must_be_authenticated.rb +2 -2
  23. data/examples/restful/webspicy/support/todo_not_removed.rb +21 -0
  24. data/examples/restful/webspicy/support/todo_removed.rb +20 -0
  25. data/examples/single_spec/spec.yml +59 -0
  26. data/examples/website/config.rb +2 -0
  27. data/examples/website/schema.fio +1 -0
  28. data/examples/website/specification/get-http.yml +30 -0
  29. data/examples/website/specification/get-https.yml +30 -0
  30. data/lib/finitio/webspicy/scalars.fio +25 -0
  31. data/lib/webspicy.rb +49 -17
  32. data/lib/webspicy/checker.rb +5 -20
  33. data/lib/webspicy/configuration.rb +79 -14
  34. data/lib/webspicy/configuration/scope.rb +154 -0
  35. data/lib/webspicy/configuration/single_url.rb +58 -0
  36. data/lib/webspicy/configuration/single_yml_file.rb +30 -0
  37. data/lib/webspicy/formaldoc.fio +25 -8
  38. data/lib/webspicy/mocker.rb +8 -8
  39. data/lib/webspicy/mocker/config.ru +5 -0
  40. data/lib/webspicy/openapi.rb +1 -0
  41. data/lib/webspicy/openapi/generator.rb +127 -0
  42. data/lib/webspicy/rspec/checker.rb +2 -0
  43. data/lib/webspicy/rspec/checker/rspec_checker.rb +24 -0
  44. data/lib/webspicy/rspec/support/rspec_runnable.rb +27 -0
  45. data/lib/webspicy/rspec/tester.rb +4 -0
  46. data/lib/webspicy/rspec/tester/rspec_asserter.rb +121 -0
  47. data/lib/webspicy/rspec/tester/rspec_matchers.rb +114 -0
  48. data/lib/webspicy/rspec/tester/rspec_tester.rb +63 -0
  49. data/lib/webspicy/{resource.rb → specification.rb} +31 -10
  50. data/lib/webspicy/specification/errcondition.rb +16 -0
  51. data/lib/webspicy/specification/file_upload.rb +37 -0
  52. data/lib/webspicy/specification/postcondition.rb +16 -0
  53. data/lib/webspicy/specification/precondition.rb +19 -0
  54. data/lib/webspicy/specification/precondition/global_request_headers.rb +35 -0
  55. data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +68 -0
  56. data/lib/webspicy/{resource → specification}/service.rb +38 -25
  57. data/lib/webspicy/specification/test_case.rb +133 -0
  58. data/lib/webspicy/support.rb +2 -0
  59. data/lib/webspicy/support/colorize.rb +28 -0
  60. data/lib/webspicy/support/data_object.rb +25 -0
  61. data/lib/webspicy/support/status_range.rb +6 -1
  62. data/lib/webspicy/tester.rb +8 -77
  63. data/lib/webspicy/tester/asserter.rb +11 -5
  64. data/lib/webspicy/tester/assertions.rb +13 -10
  65. data/lib/webspicy/tester/client.rb +63 -0
  66. data/lib/webspicy/tester/client/http_client.rb +154 -0
  67. data/lib/webspicy/tester/client/rack_test_client.rb +188 -0
  68. data/lib/webspicy/tester/client/support.rb +65 -0
  69. data/lib/webspicy/tester/failure.rb +6 -0
  70. data/lib/webspicy/tester/invocation.rb +70 -0
  71. data/lib/webspicy/version.rb +2 -2
  72. data/spec/{unit/spec_helper.rb → spec_helper.rb} +0 -0
  73. data/spec/unit/configuration/scope/test_each_service.rb +49 -0
  74. data/spec/unit/configuration/scope/test_each_specification.rb +68 -0
  75. data/spec/unit/configuration/scope/test_expand_example.rb +65 -0
  76. data/spec/unit/configuration/scope/test_to_real_url.rb +82 -0
  77. data/spec/unit/openapi/test_generator.rb +28 -0
  78. data/spec/unit/specification/precondition/test_global_request_headers.rb +42 -0
  79. data/spec/unit/{resource → specification}/service/test_dress_params.rb +2 -2
  80. data/spec/unit/specification/test_case/test_mutate.rb +24 -0
  81. data/spec/unit/{resource → specification}/test_instantiate_url.rb +5 -5
  82. data/spec/unit/{resource → specification}/test_url_placeholders.rb +4 -4
  83. data/spec/unit/test_configuration.rb +24 -7
  84. data/spec/unit/tester/client/test_around.rb +61 -0
  85. data/spec/unit/tester/test_asserter.rb +246 -0
  86. data/spec/unit/tester/test_assertions.rb +12 -10
  87. data/tasks/test.rake +3 -1
  88. metadata +106 -48
  89. data/LICENSE.md +0 -22
  90. data/lib/webspicy/client.rb +0 -61
  91. data/lib/webspicy/client/http_client.rb +0 -145
  92. data/lib/webspicy/client/rack_test_client.rb +0 -181
  93. data/lib/webspicy/client/support.rb +0 -48
  94. data/lib/webspicy/file_upload.rb +0 -35
  95. data/lib/webspicy/postcondition.rb +0 -14
  96. data/lib/webspicy/precondition.rb +0 -15
  97. data/lib/webspicy/resource/service/invocation.rb +0 -212
  98. data/lib/webspicy/resource/service/test_case.rb +0 -132
  99. data/lib/webspicy/scope.rb +0 -160
  100. data/spec/unit/client/test_around.rb +0 -59
  101. data/spec/unit/scope/test_each_resource.rb +0 -66
  102. data/spec/unit/scope/test_each_service.rb +0 -47
  103. data/spec/unit/scope/test_expand_example.rb +0 -63
  104. data/spec/unit/scope/test_to_real_url.rb +0 -80
@@ -0,0 +1,188 @@
1
+ module Webspicy
2
+ class Tester
3
+ class RackTestClient < Client
4
+
5
+ def self.for(app)
6
+ Factory.new(app)
7
+ end
8
+
9
+ def initialize(scope, app)
10
+ super(scope)
11
+ @api = Api.new(scope, app)
12
+ end
13
+ attr_reader :api
14
+
15
+ def call(test_case)
16
+ service, specification = test_case.service, test_case.specification
17
+
18
+ # Instantiate the parameters
19
+ headers = test_case.headers.dup
20
+ params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
21
+ body = test_case.body || test_case.located_file_upload
22
+
23
+ # Instantiate the url and strip parameters
24
+ url, params = specification.instantiate_url(params)
25
+ url = scope.to_real_url(url, test_case){|u,_| u }
26
+
27
+ # Invoke the service now
28
+ api.public_send(service.method.to_s.downcase.to_sym, url, params, headers, body)
29
+
30
+ # Return the last response
31
+ api.last_response
32
+ end
33
+
34
+ class Factory
35
+
36
+ def initialize(app)
37
+ @app = app
38
+ end
39
+ attr_reader :app
40
+
41
+ def new(scope)
42
+ RackTestClient.new(scope, app)
43
+ end
44
+
45
+ end # class Factory
46
+
47
+ class RackHandler
48
+ include Rack::Test::Methods
49
+
50
+ def initialize(app)
51
+ @app = app
52
+ end
53
+ attr_reader :app
54
+
55
+ end # class RackHandler
56
+
57
+ class Api
58
+ include Client::Support
59
+
60
+ attr_reader :last_response
61
+
62
+ def initialize(scope, app)
63
+ @scope = scope
64
+ @app = app
65
+ end
66
+
67
+ def config
68
+ @scope.config
69
+ end
70
+
71
+ def options(url, params = {}, headers = nil, body = nil)
72
+ handler = get_handler(headers)
73
+
74
+ info_request("OPTIONS", url, params, headers, body)
75
+
76
+ handler.options(url, params)
77
+ @last_response = handler.last_response
78
+
79
+ debug_response(@last_response)
80
+
81
+ @last_response
82
+ end
83
+
84
+ def get(url, params = {}, headers = nil, body = nil)
85
+ handler = get_handler(headers)
86
+
87
+ params = Hash[params.map{|k,v| [k, v.nil? ? "" : v] }]
88
+ info_request("GET", url, params, headers, body)
89
+
90
+ handler.get(url, params)
91
+ @last_response = handler.last_response
92
+
93
+ debug_response(@last_response)
94
+
95
+ @last_response
96
+ end
97
+
98
+ def post(url, params = {}, headers = nil, body = nil)
99
+ handler = get_handler(headers)
100
+
101
+ url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
102
+
103
+ case body
104
+ when NilClass
105
+ info_request("POST", url, params, headers, body)
106
+ handler.post(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
107
+ when FileUpload
108
+ file = Rack::Test::UploadedFile.new(body.path, body.content_type)
109
+ info_request("POST", url, params, headers, body)
110
+ handler.post(url, body.param_name.to_sym => file)
111
+ else
112
+ info_request("POST", url, params, headers, body)
113
+ handler.post(url, body)
114
+ end
115
+ @last_response = handler.last_response
116
+
117
+ debug_response(@last_response)
118
+
119
+ @last_response
120
+ end
121
+
122
+ def patch(url, params = {}, headers = nil, body = nil)
123
+ handler = get_handler(headers)
124
+
125
+ info_request("PATCH", url, params, headers, body)
126
+
127
+ handler.patch(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
128
+ @last_response = handler.last_response
129
+
130
+ debug_response(@last_response)
131
+
132
+ @last_response
133
+ end
134
+
135
+ def put(url, params = {}, headers = nil, body = nil)
136
+ handler = get_handler(headers)
137
+
138
+ info_request("PUT", url, params, headers, body)
139
+
140
+ handler.put(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
141
+ @last_response = handler.last_response
142
+
143
+ debug_response(@last_response)
144
+
145
+ @last_response
146
+ end
147
+
148
+ def post_form(url, params = {}, headers = nil, body = nil)
149
+ handler = get_handler(headers)
150
+
151
+ info_request("POST", url, params, headers, body)
152
+
153
+ handler.post(url, params)
154
+ @last_response = handler.last_response
155
+
156
+ debug_response(@last_response)
157
+
158
+ @last_response
159
+ end
160
+
161
+ def delete(url, params = {}, headers = nil, body = nil)
162
+ handler = get_handler(headers)
163
+
164
+ info_request("DELETE", url, params, headers, body)
165
+
166
+ handler.delete(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
167
+ @last_response = handler.last_response
168
+
169
+ debug_response(@last_response)
170
+
171
+ @last_response
172
+ end
173
+
174
+ private
175
+
176
+ def get_handler(hs)
177
+ handler = RackHandler.new(@app)
178
+ hs.each_pair do |k,v|
179
+ handler.header(k,v)
180
+ end if hs
181
+ handler
182
+ end
183
+
184
+ end # class Api
185
+
186
+ end # class RackTestClient
187
+ end # class Client
188
+ end # module Webspicy
@@ -0,0 +1,65 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Client
4
+ module Support
5
+ include Webspicy::Support::Colorize
6
+
7
+ NONE = Object.new
8
+
9
+ def querystring_params(params)
10
+ Hash[params.each_pair.map{|k,v| [k.to_s,v.to_s] }]
11
+ end
12
+
13
+ def info_request(kind, url, params, headers, body)
14
+ Webspicy.info(colorize_highlight("~> #{kind} #{url}"))
15
+ debug(" Req params", json_pretty(params)) if params
16
+ debug(" Req headers", json_pretty(headers)) if headers
17
+ debug(" Req body", request_body_to_s(body)) if body
18
+ end
19
+
20
+ def debug_response(response)
21
+ debug(colorize_highlight("."))
22
+ debug(" Res status", status_to_s(@last_response.status))
23
+ debug(" Res headers", json_pretty(last_response.headers.to_h))
24
+ debug(" Res body", response_body_to_s(last_response))
25
+ Webspicy.debug("")
26
+ end
27
+
28
+ def debug(what, value = NONE)
29
+ return Webspicy.debug(" #{what}") if value == NONE
30
+ Webspicy.debug(" #{what}: " + value_to_s(value))
31
+ end
32
+
33
+ def request_body_to_s(body)
34
+ body = body.to_info if body.is_a?(Webspicy::FileUpload)
35
+ json_pretty(body)
36
+ end
37
+
38
+ def response_body_to_s(response)
39
+ case response.content_type.to_s
40
+ when /json/
41
+ json_pretty(JSON.load(response.body))
42
+ else
43
+ response.body.to_s
44
+ end
45
+ end
46
+
47
+ def value_to_s(value)
48
+ value.to_s.gsub(/\n/, "\n ")
49
+ end
50
+
51
+ def status_to_s(status)
52
+ case status
53
+ when 0 ... 400 then colorize_success(status.to_s)
54
+ else colorize_error(status.to_s)
55
+ end
56
+ end
57
+
58
+ def json_pretty(s)
59
+ JSON.pretty_generate(s)
60
+ end
61
+
62
+ end # module Support
63
+ end # class Client
64
+ end # class Tester
65
+ end # module Webspicy
@@ -0,0 +1,6 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Failure < Exception
4
+ end # class Failure
5
+ end # class Tester
6
+ end # module Webspicy
@@ -0,0 +1,70 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Invocation
4
+
5
+ def initialize(test_case, response, client)
6
+ @test_case = test_case
7
+ @response = response
8
+ @client = client
9
+ end
10
+
11
+ attr_reader :test_case, :response, :client
12
+
13
+ def service
14
+ test_case.service
15
+ end
16
+
17
+ ### Getters on response
18
+
19
+ def response_code
20
+ code = response.status
21
+ code = code.code unless code.is_a?(Integer)
22
+ code
23
+ end
24
+
25
+ ### Query methods
26
+
27
+ def done?
28
+ !response.nil?
29
+ end
30
+
31
+ def is_expected_success?
32
+ test_case.expected_status.to_i >= 200 && test_case.expected_status.to_i < 300
33
+ end
34
+
35
+ def is_success?
36
+ response_code >= 200 && response_code < 300
37
+ end
38
+
39
+ def is_empty_response?
40
+ response_code == 204
41
+ end
42
+
43
+ def is_redirect?
44
+ response_code >= 300 && response_code < 400
45
+ end
46
+
47
+ def loaded_body
48
+ ct = response.content_type || test_case.expected_content_type
49
+ ct = ct.mime_type if ct.respond_to?(:mime_type)
50
+ case ct
51
+ when %r{json}
52
+ raise "Body empty while expected" if response.body.to_s.empty?
53
+ @loaded_body ||= ::JSON.parse(response.body)
54
+ else
55
+ response.body.to_s
56
+ end
57
+ end
58
+
59
+ def dressed_body
60
+ schema = is_expected_success? ? service.output_schema : service.error_schema
61
+ begin
62
+ schema.dress(loaded_body)
63
+ rescue Finitio::TypeError => ex
64
+ ex
65
+ end
66
+ end
67
+
68
+ end # class Invocation
69
+ end # class Tester
70
+ end # module Webspicy
@@ -1,8 +1,8 @@
1
1
  module Webspicy
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 15
5
- TINY = 8
4
+ MINOR = 17
5
+ TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ module Webspicy
3
+ class Configuration
4
+ describe Scope, 'each_service' do
5
+
6
+ let(:scope) {
7
+ Scope.new(configuration)
8
+ }
9
+
10
+ let(:specification) {
11
+ scope.each_specification.first
12
+ }
13
+
14
+ subject {
15
+ scope.each_service(specification).to_a
16
+ }
17
+
18
+ context 'without any filter' do
19
+
20
+ let(:configuration) {
21
+ Configuration.new(restful_folder){|c|
22
+ c.file_filter = /get.yml/
23
+ }
24
+ }
25
+
26
+ it 'returns all services' do
27
+ expect(subject.size).to eql(1)
28
+ end
29
+ end
30
+
31
+ context 'with a service filter as a proc' do
32
+
33
+ let(:configuration) {
34
+ Configuration.new(restful_folder){|c|
35
+ c.file_filter = /get.yml/
36
+ c.service_filter = ->(s) {
37
+ s.method == "POST"
38
+ }
39
+ }
40
+ }
41
+
42
+ it 'returns nothing' do
43
+ expect(subject.size).to eql(0)
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ module Webspicy
3
+ class Configuration
4
+ describe Scope, 'each_specification' do
5
+
6
+ let(:scope) {
7
+ Scope.new(configuration)
8
+ }
9
+
10
+ subject {
11
+ scope.each_specification.to_a
12
+ }
13
+
14
+ context 'without any filter' do
15
+
16
+ let(:configuration) {
17
+ Configuration.new(restful_folder)
18
+ }
19
+
20
+ it 'returns all files' do
21
+ expect(subject.size).to eql(restful_folder.glob('**/*.yml').size)
22
+ end
23
+ end
24
+
25
+ context 'with a file filter as a proc' do
26
+
27
+ let(:configuration) {
28
+ Configuration.new(restful_folder){|c|
29
+ c.file_filter = ->(f) {
30
+ f.basename.to_s == "get.yml"
31
+ }
32
+ }
33
+ }
34
+
35
+ it 'returns only that files' do
36
+ expect(subject.size).to eql(2)
37
+ end
38
+ end
39
+
40
+ context 'with a file filter as a Regex' do
41
+
42
+ let(:configuration) {
43
+ Configuration.new(restful_folder){|c|
44
+ c.file_filter = /get.yml/
45
+ }
46
+ }
47
+
48
+ it 'returns only that files' do
49
+ expect(subject.size).to eql(2)
50
+ end
51
+ end
52
+
53
+ context 'when having children folders' do
54
+
55
+ let(:configuration) {
56
+ Configuration.new(restful_folder) do |c|
57
+ c.folder 'formaldef/todo'
58
+ end
59
+ }
60
+
61
+ it 'returns all files' do
62
+ expect(subject.size).to eql(restful_folder.glob('**/*.yml').size)
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ end