webspicy 0.15.8 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -24
  3. data/bin/webspicy +30 -14
  4. data/examples/restful/Gemfile.lock +38 -23
  5. data/examples/restful/Rakefile +0 -1
  6. data/examples/restful/app.rb +4 -1
  7. data/examples/restful/webspicy/config.rb +8 -0
  8. data/examples/restful/webspicy/rack.rb +1 -1
  9. data/examples/restful/webspicy/real.rb +1 -1
  10. data/examples/restful/webspicy/schema.fio +2 -2
  11. data/examples/restful/webspicy/support/must_be_authenticated.rb +2 -2
  12. data/examples/restful/webspicy/support/todo_removed.rb +18 -0
  13. data/examples/restful/webspicy/todo/deleteTodo.yml +4 -1
  14. data/examples/restful/webspicy/todo/getTodoSingleServiceFormat.yml +46 -0
  15. data/examples/restful/webspicy/todo/options.yml +1 -1
  16. data/examples/restful/webspicy/todo/patchTodo.yml +3 -0
  17. data/examples/restful/webspicy/todo/postFile.yml +1 -1
  18. data/examples/single_spec/spec.yml +59 -0
  19. data/examples/website/config.rb +2 -0
  20. data/examples/website/schema.fio +1 -0
  21. data/examples/website/specification/get-http.yml +34 -0
  22. data/examples/website/specification/get-https.yml +34 -0
  23. data/lib/finitio/webspicy/scalars.fio +25 -0
  24. data/lib/webspicy.rb +48 -17
  25. data/lib/webspicy/checker.rb +2 -2
  26. data/lib/webspicy/configuration.rb +70 -14
  27. data/lib/webspicy/configuration/scope.rb +162 -0
  28. data/lib/webspicy/configuration/single_url.rb +58 -0
  29. data/lib/webspicy/configuration/single_yml_file.rb +30 -0
  30. data/lib/webspicy/formaldoc.fio +23 -8
  31. data/lib/webspicy/mocker.rb +8 -8
  32. data/lib/webspicy/openapi.rb +1 -0
  33. data/lib/webspicy/openapi/generator.rb +127 -0
  34. data/lib/webspicy/{resource.rb → specification.rb} +26 -5
  35. data/lib/webspicy/specification/file_upload.rb +37 -0
  36. data/lib/webspicy/specification/postcondition.rb +16 -0
  37. data/lib/webspicy/specification/precondition.rb +19 -0
  38. data/lib/webspicy/specification/precondition/global_request_headers.rb +35 -0
  39. data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +68 -0
  40. data/lib/webspicy/{resource → specification}/service.rb +11 -6
  41. data/lib/webspicy/specification/test_case.rb +139 -0
  42. data/lib/webspicy/support.rb +1 -0
  43. data/lib/webspicy/support/colorize.rb +28 -0
  44. data/lib/webspicy/support/status_range.rb +6 -1
  45. data/lib/webspicy/tester.rb +16 -11
  46. data/lib/webspicy/tester/asserter.rb +3 -2
  47. data/lib/webspicy/tester/assertions.rb +5 -1
  48. data/lib/webspicy/tester/client.rb +63 -0
  49. data/lib/webspicy/tester/client/http_client.rb +154 -0
  50. data/lib/webspicy/tester/client/rack_test_client.rb +188 -0
  51. data/lib/webspicy/tester/client/support.rb +65 -0
  52. data/lib/webspicy/tester/invocation.rb +218 -0
  53. data/lib/webspicy/tester/rspec_asserter.rb +108 -0
  54. data/lib/webspicy/tester/rspec_matchers.rb +104 -0
  55. data/lib/webspicy/version.rb +2 -2
  56. data/spec/{unit/spec_helper.rb → spec_helper.rb} +0 -0
  57. data/spec/unit/configuration/scope/test_each_service.rb +49 -0
  58. data/spec/unit/configuration/scope/test_each_specification.rb +68 -0
  59. data/spec/unit/configuration/scope/test_expand_example.rb +65 -0
  60. data/spec/unit/configuration/scope/test_to_real_url.rb +82 -0
  61. data/spec/unit/openapi/test_generator.rb +28 -0
  62. data/spec/unit/specification/precondition/test_global_request_headers.rb +42 -0
  63. data/spec/unit/{resource → specification}/service/test_dress_params.rb +2 -2
  64. data/spec/unit/specification/test_case/test_mutate.rb +24 -0
  65. data/spec/unit/{resource → specification}/test_instantiate_url.rb +5 -5
  66. data/spec/unit/{resource → specification}/test_url_placeholders.rb +4 -4
  67. data/spec/unit/test_configuration.rb +24 -7
  68. data/spec/unit/tester/client/test_around.rb +61 -0
  69. data/spec/unit/tester/test_asserter.rb +51 -0
  70. data/spec/unit/tester/test_assertions.rb +4 -4
  71. data/tasks/test.rake +3 -1
  72. metadata +83 -34
  73. data/lib/webspicy/client.rb +0 -61
  74. data/lib/webspicy/client/http_client.rb +0 -145
  75. data/lib/webspicy/client/rack_test_client.rb +0 -181
  76. data/lib/webspicy/client/support.rb +0 -48
  77. data/lib/webspicy/file_upload.rb +0 -35
  78. data/lib/webspicy/postcondition.rb +0 -14
  79. data/lib/webspicy/precondition.rb +0 -15
  80. data/lib/webspicy/resource/service/invocation.rb +0 -212
  81. data/lib/webspicy/resource/service/test_case.rb +0 -132
  82. data/lib/webspicy/scope.rb +0 -160
  83. data/spec/unit/client/test_around.rb +0 -59
  84. data/spec/unit/scope/test_each_resource.rb +0 -66
  85. data/spec/unit/scope/test_each_service.rb +0 -47
  86. data/spec/unit/scope/test_expand_example.rb +0 -63
  87. data/spec/unit/scope/test_to_real_url.rb +0 -80
@@ -1,132 +0,0 @@
1
- module Webspicy
2
- class Resource
3
- class Service
4
- class TestCase
5
-
6
- def initialize(raw)
7
- @raw = raw
8
- @counterexample = nil
9
- end
10
- attr_reader :service
11
- attr_reader :counterexample
12
-
13
- def bind(service, counterexample)
14
- @service = service
15
- @counterexample = counterexample
16
- self
17
- end
18
-
19
- def counterexample?
20
- !!@counterexample
21
- end
22
-
23
- def resource
24
- service.resource
25
- end
26
-
27
- def self.info(raw)
28
- new(raw)
29
- end
30
-
31
- def description
32
- @raw[:description]
33
- end
34
-
35
- def seeds
36
- @raw[:seeds]
37
- end
38
-
39
- def headers
40
- @raw[:headers] ||= {}
41
- end
42
-
43
- def metadata
44
- @raw[:metadata] ||= {}
45
- end
46
-
47
- def tags
48
- @raw[:tags] ||= []
49
- end
50
-
51
- def dress_params
52
- @raw.fetch(:dress_params){ true }
53
- end
54
- alias :dress_params? :dress_params
55
-
56
- def params
57
- @raw[:params] || {}
58
- end
59
-
60
- def body
61
- @raw[:body]
62
- end
63
-
64
- def file_upload
65
- @raw[:file_upload]
66
- end
67
-
68
- def located_file_upload
69
- file_upload ? file_upload.locate(resource) : nil
70
- end
71
-
72
- def expected
73
- @raw[:expected] || {}
74
- end
75
-
76
- def expected_content_type
77
- expected.fetch(:content_type){ 'application/json' }
78
- end
79
-
80
- def expected_status
81
- expected[:status]
82
- end
83
-
84
- def is_expected_status?(status)
85
- expected_status === status
86
- end
87
-
88
- def expected_error
89
- expected[:error]
90
- end
91
-
92
- def has_expected_error?
93
- !expected_error.nil?
94
- end
95
-
96
- def expected_headers
97
- expected[:headers] || {}
98
- end
99
-
100
- def has_expected_headers?
101
- !expected_headers.empty?
102
- end
103
-
104
- def assert
105
- @raw[:assert] || []
106
- end
107
-
108
- def has_assertions?
109
- !assert.empty?
110
- end
111
-
112
- def to_info
113
- @raw
114
- end
115
-
116
- def instrument(client)
117
- service.preconditions.each do |pre|
118
- pre.instrument(self, client)
119
- end
120
- service.postconditions.each do |post|
121
- post.instrument(self, client) if post.respond_to?(:instrument)
122
- end
123
- end
124
-
125
- def to_s
126
- description
127
- end
128
-
129
- end # class TestCase
130
- end # class Service
131
- end # class Resource
132
- end # module Webspicy
@@ -1,160 +0,0 @@
1
- module Webspicy
2
- class Scope
3
-
4
- def initialize(config)
5
- @config = config
6
- end
7
- attr_reader :config
8
-
9
- def preconditions
10
- config.preconditions
11
- end
12
-
13
- def postconditions
14
- config.postconditions
15
- end
16
-
17
- ###
18
- ### Eachers -- Allow navigating the web service definitions
19
- ###
20
-
21
- # Yields each resource file in the current scope
22
- def each_resource_file(&bl)
23
- return enum_for(:each_resource_file) unless block_given?
24
- _each_resource_file(config, &bl)
25
- end
26
-
27
- # Recursive implementation of `each_resource_file` for each
28
- # folder in the configuration.
29
- def _each_resource_file(config)
30
- folder = config.folder
31
- folder.glob("**/*.yml").select(&to_filter_proc(config.file_filter)).each do |file|
32
- yield file, folder
33
- end
34
- end
35
- private :_each_resource_file
36
-
37
- # Yields each resource in the current scope in turn.
38
- def each_resource(&bl)
39
- return enum_for(:each_resource) unless block_given?
40
- each_resource_file do |file, folder|
41
- yield Webspicy.resource(file.load, file, self)
42
- end
43
- end
44
-
45
- def each_service(resource, &bl)
46
- resource.services.select(&to_filter_proc(config.service_filter)).each(&bl)
47
- end
48
-
49
- def each_example(service, &bl)
50
- service.examples
51
- .map{|e| expand_example(service, e) }
52
- .select(&to_filter_proc(config.test_case_filter))
53
- .each(&bl) if config.run_examples?
54
- end
55
-
56
- def each_counterexamples(service, &bl)
57
- service.counterexamples
58
- .map{|e| expand_example(service, e) }
59
- .select(&to_filter_proc(config.test_case_filter))
60
- .each(&bl) if config.run_counterexamples?
61
- end
62
-
63
- def each_generated_counterexamples(service, &bl)
64
- Webspicy.with_scope(self) do
65
- service.generated_counterexamples
66
- .map{|e| expand_example(service, e) }
67
- .select(&to_filter_proc(config.test_case_filter))
68
- .each(&bl) if config.run_counterexamples?
69
- end if config.run_counterexamples?
70
- end
71
-
72
- def each_testcase(service, &bl)
73
- each_example(service, &bl)
74
- each_counterexamples(service, &bl)
75
- each_generated_counterexamples(service, &bl)
76
- end
77
-
78
- ###
79
- ### Schemas -- For parsing input and output data schemas found in
80
- ### web service definitions
81
- ###
82
-
83
- # Parses a Finitio schema based on the data system.
84
- def parse_schema(fio)
85
- data_system.parse(fio)
86
- end
87
-
88
- # Returns the Data system to use for parsing schemas
89
- def data_system
90
- @data_system ||= config.data_system
91
- end
92
-
93
-
94
- ###
95
- ### Service invocation: abstract the configuration about what client is
96
- ### used and how to instantiate it
97
- ###
98
-
99
- # Returns an instance of the client to use to invoke web services
100
- def get_client
101
- config.client.new(self)
102
- end
103
-
104
- # Convert an instantiated URL found in a webservice definition
105
- # to a real URL, using the configuration host.
106
- #
107
- # When no host resolved on the configuration and the url is not
108
- # already an absolute URL, yields the block if given, or raise
109
- # an exception.
110
- def to_real_url(url, test_case = nil, &bl)
111
- case config.host
112
- when Proc
113
- config.host.call(url, test_case)
114
- when String
115
- url =~ /^http/ ? url : "#{config.host}#{url}"
116
- else
117
- return url if url =~ /^http/
118
- return yield(url) if block_given?
119
- raise "Unable to resolve `#{url}` : no host resolver provided\nSee `Configuration#host="
120
- end
121
- end
122
-
123
- ###
124
- ### Private methods
125
- ###
126
-
127
- private
128
-
129
- def expand_example(service, example)
130
- return example unless service.default_example
131
- h1 = service.default_example.to_info
132
- h2 = example.to_info
133
- ex = Resource::Service::TestCase.new(merge_maps(h1, h2))
134
- ex.bind(service, example.counterexample?)
135
- end
136
-
137
- def merge_maps(h1, h2)
138
- h1.merge(h2) do |k,v1,v2|
139
- case v1
140
- when Hash then merge_maps(v1, v2)
141
- when Array then v1 + v2
142
- else v2
143
- end
144
- end
145
- end
146
-
147
- # Returns a proc that implements file_filter strategy according to the
148
- # type of filter installed
149
- def to_filter_proc(filter)
150
- case ff = filter
151
- when NilClass then ->(f){ true }
152
- when Proc then ff
153
- when Regexp then ->(f){ ff =~ f.to_s }
154
- else
155
- ->(f){ ff === f }
156
- end
157
- end
158
-
159
- end
160
- end
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
- module Webspicy
3
- describe Client, "around" do
4
-
5
- it 'work with no around at all' do
6
- config = Configuration.new
7
-
8
- seen = false
9
- block = ->(){ seen = true }
10
-
11
- scope = Scope.new(config)
12
- Client.new(scope).around(1, 2, 3, &block)
13
- expect(seen).to be(true)
14
- end
15
-
16
- it 'work with one around' do
17
- config = Configuration.new
18
-
19
- seen = false
20
- seen_args = nil
21
- config.around_each do |*args, &bl|
22
- seen_args = args
23
- bl.call
24
- end
25
-
26
- scope = Scope.new(config)
27
- block = ->(){ seen = true }
28
- Client.new(scope).around(1, 2, 3, &block)
29
-
30
- expect(seen_args[0...-1]).to eql([1, 2, 3])
31
- expect(seen).to be(true)
32
- end
33
-
34
- it 'works with two arounds' do
35
- config = Configuration.new
36
-
37
- seen = false
38
-
39
- seen_args = []
40
- config.around_each do |*args, &bl|
41
- seen_args << args
42
- bl.call
43
- end
44
- config.around_each do |*args, &bl|
45
- seen_args << args
46
- bl.call
47
- end
48
-
49
- scope = Scope.new(config)
50
- block = ->(){ seen = true }
51
- Client.new(scope).around(1, 2, 3, &block)
52
-
53
- expect(seen_args.size).to eql(2)
54
- expect(seen_args.all?{|sa| sa[0...-1] == [1,2,3] }).to be(true)
55
- expect(seen).to be(true)
56
- end
57
-
58
- end
59
- end
@@ -1,66 +0,0 @@
1
- require 'spec_helper'
2
- module Webspicy
3
- describe Scope, 'each_resource' do
4
-
5
- let(:scope) {
6
- Scope.new(configuration)
7
- }
8
-
9
- subject {
10
- scope.each_resource.to_a
11
- }
12
-
13
- context 'without any filter' do
14
-
15
- let(:configuration) {
16
- Configuration.new(restful_folder)
17
- }
18
-
19
- it 'returns all files' do
20
- expect(subject.size).to eql(restful_folder.glob('**/*.yml').size)
21
- end
22
- end
23
-
24
- context 'with a file filter as a proc' do
25
-
26
- let(:configuration) {
27
- Configuration.new(restful_folder){|c|
28
- c.file_filter = ->(f) {
29
- f.basename.to_s == "getTodo.yml"
30
- }
31
- }
32
- }
33
-
34
- it 'returns only that file' do
35
- expect(subject.size).to eql(1)
36
- end
37
- end
38
-
39
- context 'with a file filter as a Regex' do
40
-
41
- let(:configuration) {
42
- Configuration.new(restful_folder){|c|
43
- c.file_filter = /getTodo.yml/
44
- }
45
- }
46
-
47
- it 'returns only that file' do
48
- expect(subject.size).to eql(1)
49
- end
50
- end
51
-
52
- context 'when having children folders' do
53
-
54
- let(:configuration) {
55
- Configuration.new(restful_folder) do |c|
56
- c.folder 'todo'
57
- end
58
- }
59
-
60
- it 'returns all files' do
61
- expect(subject.size).to eql(restful_folder.glob('**/*.yml').size)
62
- end
63
- end
64
-
65
- end
66
- end
@@ -1,47 +0,0 @@
1
- require 'spec_helper'
2
- module Webspicy
3
- describe Scope, 'each_service' do
4
-
5
- let(:scope) {
6
- Scope.new(configuration)
7
- }
8
-
9
- let(:resource) {
10
- scope.each_resource.first
11
- }
12
-
13
- subject {
14
- scope.each_service(resource).to_a
15
- }
16
-
17
- context 'without any filter' do
18
-
19
- let(:configuration) {
20
- Configuration.new(restful_folder){|c|
21
- c.file_filter = /getTodo.yml/
22
- }
23
- }
24
-
25
- it 'returns all services' do
26
- expect(subject.size).to eql(1)
27
- end
28
- end
29
-
30
- context 'with a service filter as a proc' do
31
-
32
- let(:configuration) {
33
- Configuration.new(restful_folder){|c|
34
- c.file_filter = /getTodo.yml/
35
- c.service_filter = ->(s) {
36
- s.method == "POST"
37
- }
38
- }
39
- }
40
-
41
- it 'returns nothing' do
42
- expect(subject.size).to eql(0)
43
- end
44
- end
45
-
46
- end
47
- end