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
@@ -29,8 +29,8 @@ module Webspicy
29
29
 
30
30
  def has_service?(path)
31
31
  config.each_scope do |scope|
32
- scope.each_resource do |resource|
33
- next unless url_matches?(resource, path)
32
+ scope.each_specification do |specification|
33
+ next unless url_matches?(specification, path)
34
34
  return true
35
35
  end
36
36
  end
@@ -39,9 +39,9 @@ module Webspicy
39
39
 
40
40
  def find_service(method, path)
41
41
  config.each_scope do |scope|
42
- scope.each_resource do |resource|
43
- next unless url_matches?(resource, path)
44
- scope.each_service(resource) do |service|
42
+ scope.each_specification do |specification|
43
+ next unless url_matches?(specification, path)
44
+ scope.each_service(specification) do |service|
45
45
  return service if service.method == method
46
46
  end
47
47
  end
@@ -71,8 +71,8 @@ module Webspicy
71
71
  end
72
72
  end
73
73
 
74
- def url_matches?(resource, path)
75
- resource.url_pattern.match(path)
74
+ def url_matches?(specification, path)
75
+ specification.url_pattern.match(path)
76
76
  end
77
77
 
78
78
  def random_body(service, request)
@@ -85,4 +85,4 @@ module Webspicy
85
85
  end
86
86
 
87
87
  end # class Mocker
88
- end # module Webspicy
88
+ end # module Webspicy
@@ -0,0 +1,5 @@
1
+ require "webspicy"
2
+ require "webspicy/mocker"
3
+ require 'rack'
4
+ config = Webspicy::Configuration.dress(Path("/formalspec/"))
5
+ run Webspicy::Mocker.new(config)
@@ -0,0 +1 @@
1
+ require_relative 'openapi/generator'
@@ -0,0 +1,127 @@
1
+ require "finitio/generation"
2
+ require "finitio/json_schema"
3
+ module Webspicy
4
+ module Openapi
5
+ class Generator
6
+
7
+ def initialize(config)
8
+ @config = Configuration.dress(config)
9
+ @generator = config.generator || Finitio::Generation.new
10
+ end
11
+ attr_reader :config, :generator
12
+
13
+ def call
14
+ {
15
+ openapi: "3.0.2",
16
+ info: {
17
+ version: "1.0.0",
18
+ title: "Hello API"
19
+ },
20
+ paths: paths
21
+ }
22
+ end
23
+
24
+ private
25
+
26
+ def paths
27
+ config.each_scope.inject({}) do |paths,scope|
28
+ scope.each_specification.inject(paths) do |paths,specification|
29
+ paths.merge(path_for(specification)){|k,ps,qs|
30
+ ps.merge(qs)
31
+ }
32
+ end
33
+ end
34
+ end
35
+
36
+ def path_for(specification)
37
+ {
38
+ specification.url => {
39
+ summary: specification.name
40
+ }.merge(verbs_for(specification))
41
+ }
42
+ end
43
+
44
+ def verbs_for(specification)
45
+ specification.services.inject({}) do |verbs,service|
46
+ verb = service.method.downcase
47
+ verb_defn = {
48
+ description: service.description,
49
+ responses: responses_for(service)
50
+ }
51
+ unless ["get", "options", "delete", "head"].include?(verb)
52
+ verb_defn[:requestBody] = request_body_for(service)
53
+ end
54
+ verbs.merge({ verb => verb_defn })
55
+ end
56
+ end
57
+
58
+ def request_body_for(service)
59
+ schema = actual_input_schema(service)
60
+ {
61
+ required: true,
62
+ content: {
63
+ "application/json" => {
64
+ schema: schema.to_json_schema,
65
+ example: generator.call(schema, {})
66
+ }
67
+ }
68
+ }
69
+ end
70
+
71
+ def responses_for(service)
72
+ result = {}
73
+ service.examples.each_with_object(result) do |test_case, rs|
74
+ rs.merge!(response_for(test_case, false)){|k,r1,r2| r1 }
75
+ end
76
+ service.counterexamples.each_with_object(result) do |test_case, rs|
77
+ rs.merge!(response_for(test_case, true)){|k,r1,r2| r1 }
78
+ end
79
+ result
80
+ end
81
+
82
+ def response_for(test_case, counterexample)
83
+ res = {
84
+ description: test_case.description,
85
+ }
86
+ status = (test_case.expected_status && test_case.expected_status.to_int) || 200
87
+ if test_case.expected_content_type && status != 204
88
+ content = {
89
+ schema: schema_for(test_case, counterexample)
90
+ }
91
+ unless counterexample
92
+ content[:example] = example_for(test_case, counterexample)
93
+ end
94
+ res[:content] = {
95
+ test_case.expected_content_type => content
96
+ }
97
+ end
98
+ {
99
+ status => res
100
+ }
101
+ end
102
+
103
+ def schema_for(test_case, counterexample)
104
+ schema = actual_output_schema(test_case, counterexample)
105
+ schema.to_json_schema
106
+ end
107
+
108
+ def example_for(test_case, counterexample)
109
+ schema = actual_output_schema(test_case, counterexample)
110
+ generator.call(schema, {})
111
+ end
112
+
113
+ def actual_output_schema(test_case, counterexample)
114
+ if counterexample
115
+ test_case.service.error_schema['Main']
116
+ else
117
+ test_case.service.output_schema['Main']
118
+ end
119
+ end
120
+
121
+ def actual_input_schema(service)
122
+ service.input_schema['Main']
123
+ end
124
+
125
+ end # class Generator
126
+ end # module Openapi
127
+ end # module Webspicy
@@ -0,0 +1,2 @@
1
+ require_relative 'support/rspec_runnable'
2
+ require_relative 'checker/rspec_checker'
@@ -0,0 +1,24 @@
1
+ module Webspicy
2
+ class Checker
3
+ class RSpecChecker
4
+ include Webspicy::Support::RSpecRunnable
5
+
6
+ protected
7
+
8
+ def load_rspec_examples
9
+ config.each_scope do |scope|
10
+ scope.each_specification_file do |file, folder|
11
+ RSpec.describe file.relative_to(folder).to_s do
12
+
13
+ it 'meets the formal doc data schema' do
14
+ Webspicy.specification(file.load, file, scope)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ end # class RSpecChecker
23
+ end # class Checker
24
+ end # module Webspicy
@@ -0,0 +1,27 @@
1
+ module Webspicy
2
+ module Support
3
+ module RSpecRunnable
4
+
5
+ def initialize(config)
6
+ @config = Configuration.dress(config)
7
+ end
8
+ attr_reader :config
9
+
10
+ def reset_rspec!
11
+ RSpec.reset
12
+ RSpec.configure do |c|
13
+ c.filter_gems_from_backtrace "rake"
14
+ end
15
+ load_rspec_examples
16
+ end
17
+
18
+ def call(err=$stderr, out=$stdout)
19
+ reset_rspec!
20
+ options = RSpec::Core::ConfigurationOptions.new(config.rspec_options)
21
+ conf = RSpec::Core::Configuration.new
22
+ RSpec::Core::Runner.new(options, conf).run(err, out)
23
+ end
24
+
25
+ end # module RSpecRunnable
26
+ end # module Support
27
+ end # module Webspicy
@@ -0,0 +1,4 @@
1
+ require_relative 'support/rspec_runnable'
2
+ require_relative 'tester/rspec_matchers'
3
+ require_relative 'tester/rspec_asserter'
4
+ require_relative 'tester/rspec_tester'
@@ -0,0 +1,121 @@
1
+ module Webspicy
2
+ class Tester
3
+ class RSpecAsserter
4
+
5
+ def initialize(rspec, invocation)
6
+ @rspec = rspec
7
+ @invocation = invocation
8
+ end
9
+ attr_reader :rspec, :invocation
10
+
11
+ def self.call(rspec, invocation)
12
+ new(rspec, invocation).send(:assert!)
13
+ end
14
+
15
+ def response
16
+ invocation.response
17
+ end
18
+
19
+ def test_case
20
+ invocation.test_case
21
+ end
22
+
23
+ def service
24
+ test_case.service
25
+ end
26
+
27
+ protected
28
+
29
+ def assert!
30
+ rspec.aggregate_failures do
31
+ assert_status_met
32
+ assert_content_type_met
33
+ assert_expected_headers
34
+ end
35
+ assert_output_schema_met
36
+ rspec.aggregate_failures do
37
+ assert_assertions_met
38
+ assert_postconditions_met
39
+ assert_errconditions_met
40
+ end
41
+ end
42
+
43
+ def assert_status_met
44
+ got = response.status
45
+ expected = test_case.expected_status
46
+ rspec.expect(got).to rspec.match_response_status(expected)
47
+ end
48
+
49
+ def assert_content_type_met
50
+ return if test_case.is_expected_status?(204)
51
+ return unless ect = test_case.expected_content_type
52
+ got = response.content_type
53
+ got = got.mime_type if got.respond_to?(:mime_type)
54
+ if ect.nil?
55
+ rspec.expect(ect).to rspec.have_no_response_type
56
+ else
57
+ rspec.expect(got).to rspec.match_content_type(ect)
58
+ end
59
+ end
60
+
61
+ def assert_expected_headers
62
+ return unless test_case.has_expected_headers?
63
+ test_case.expected_headers.each_pair do |k,v|
64
+ got = response.headers[k]
65
+ if got.nil?
66
+ rspec.expect(got).to rspec.be_in_response_headers(k)
67
+ else
68
+ rspec.expect(got).to rspec.match_response_header(k, v)
69
+ end
70
+ end
71
+ end
72
+
73
+ def assert_output_schema_met
74
+ return if test_case.is_expected_status?(204)
75
+ return if invocation.is_redirect?
76
+ if invocation.is_empty_response?
77
+ body = response.body.to_s.strip
78
+ rspec.expect(body).to rspec.be_an_empty_response_body
79
+ else
80
+ b = invocation.dressed_body
81
+ if invocation.is_expected_success?
82
+ rspec.expect(b).to rspec.meet_output_schema
83
+ else
84
+ rspec.expect(b).to rspec.meet_error_schema
85
+ end
86
+ end
87
+ end
88
+
89
+ def assert_assertions_met
90
+ return unless test_case.has_assertions?
91
+ asserter = Tester::Asserter.new(invocation.dressed_body)
92
+ test_case.assert.each do |assert|
93
+ begin
94
+ asserter.instance_eval(assert)
95
+ rescue => ex
96
+ rspec.expect(ex).to rspec.meet_assertion(assert)
97
+ end
98
+ end
99
+ end
100
+
101
+ def assert_postconditions_met
102
+ return unless service.has_postconditions?
103
+ return if test_case.counterexample?
104
+ service.postconditions.each do |post|
105
+ msg = post.check(invocation)
106
+ rspec.expect(msg).to rspec.meet_postcondition(post)
107
+ end
108
+ end
109
+
110
+ def assert_errconditions_met
111
+ return unless service.has_errconditions?
112
+ return unless test_case.counterexample?
113
+ service.errconditions.each do |post|
114
+ msg = post.check(invocation)
115
+ rspec.expect(msg).to rspec.meet_errcondition(post)
116
+ end
117
+ end
118
+
119
+ end # class RSpecAsserter
120
+ end # class Tester
121
+ end # module Webspicy
@@ -0,0 +1,114 @@
1
+ require 'rspec/expectations'
2
+
3
+ RSpec::Matchers.define :match_response_status do |expected|
4
+ match do |actual|
5
+ expected === actual
6
+ end
7
+ failure_message_for_should do |actual|
8
+ "expected response status #{actual} to be #{expected}"
9
+ end
10
+ end
11
+
12
+ RSpec::Matchers.define :have_no_response_type do
13
+ match do |actual|
14
+ actual.nil?
15
+ end
16
+ failure_message_for_should do |actual|
17
+ "expected Content-Type not to be present"
18
+ end
19
+ end
20
+
21
+ RSpec::Matchers.define :match_content_type do |expected|
22
+ match do |actual|
23
+ actual.to_s.start_with?(expected.to_s)
24
+ end
25
+ failure_message_for_should do |actual|
26
+ "expected Content-Type to be `#{expected}`, got `#{actual}`"
27
+ end
28
+ end
29
+
30
+ RSpec::Matchers.define :be_in_response_headers do |header_name|
31
+ match do |actual|
32
+ !actual.nil?
33
+ end
34
+ failure_message_for_should do |actual|
35
+ "expected response header `#{header_name}` to be set"
36
+ end
37
+ end
38
+
39
+ RSpec::Matchers.define :match_response_header do |header_name, expected|
40
+ match do |actual|
41
+ expected == actual
42
+ end
43
+ failure_message_for_should do |actual|
44
+ "expected response header `#{header_name}` to be `#{expected}`, got `#{actual}`"
45
+ end
46
+ end
47
+
48
+ RSpec::Matchers.define :be_an_empty_response_body do
49
+ match do |actual|
50
+ actual.empty?
51
+ end
52
+ failure_message_for_should do |actual|
53
+ "expected response body to be empty, started with `#{actual[0..20]}`"
54
+ end
55
+ end
56
+
57
+ RSpec::Matchers.define :meet_output_schema do
58
+ match do |actual|
59
+ !actual.is_a?(Exception)
60
+ end
61
+ failure_message_for_should do |actual|
62
+ "expected response body to meet output schema, got following error:\n" + \
63
+ " #{actual.message}"
64
+ end
65
+ end
66
+
67
+ RSpec::Matchers.define :meet_error_schema do
68
+ match do |actual|
69
+ !actual.is_a?(Exception)
70
+ end
71
+ failure_message_for_should do |actual|
72
+ "expected response body to meet error schema, got following error:\n" + \
73
+ " #{actual.message}"
74
+ end
75
+ end
76
+
77
+ RSpec::Matchers.define :meet_assertion do |assert|
78
+ match do |actual|
79
+ actual.nil?
80
+ end
81
+ failure_message_for_should do |actual|
82
+ "expected assertion `#{assert}` to be met, got following error:\n" + \
83
+ " #{actual.message}"
84
+ end
85
+ end
86
+
87
+ RSpec::Matchers.define :meet_postcondition do |post|
88
+ match do |actual|
89
+ actual.nil?
90
+ end
91
+ failure_message_for_should do |actual|
92
+ "expected postcondition `#{post.class.name}` to be met, got following error:\n" + \
93
+ " #{actual}"
94
+ end
95
+ end
96
+
97
+ RSpec::Matchers.define :meet_errcondition do |post|
98
+ match do |actual|
99
+ actual.nil?
100
+ end
101
+ failure_message_for_should do |actual|
102
+ "expected errcondition `#{post.class.name}` to be met, got following error:\n" + \
103
+ " #{actual}"
104
+ end
105
+ end
106
+
107
+ RSpec::Matchers.define :be_an_empty_errors_array do
108
+ match do |actual|
109
+ actual.empty?
110
+ end
111
+ failure_message_for_should do |actual|
112
+ "expected no webspicy error, got the following ones:\n" + actual.map{|a| " #{a}" }.join("\n")
113
+ end
114
+ end