webspicy 0.15.5 → 0.16.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -24
  3. data/bin/webspicy +30 -13
  4. data/examples/restful/Gemfile +2 -2
  5. data/examples/restful/Gemfile.lock +53 -33
  6. data/examples/restful/Rakefile +0 -1
  7. data/examples/restful/app.rb +19 -1
  8. data/examples/restful/webspicy/config.rb +8 -0
  9. data/examples/restful/webspicy/rack.rb +1 -1
  10. data/examples/restful/webspicy/real.rb +1 -1
  11. data/examples/restful/webspicy/schema.fio +7 -2
  12. data/examples/restful/webspicy/support/must_be_authenticated.rb +2 -2
  13. data/examples/restful/webspicy/support/todo_removed.rb +18 -0
  14. data/examples/restful/webspicy/todo/deleteTodo.yml +4 -1
  15. data/examples/restful/webspicy/todo/getTodoSingleServiceFormat.yml +46 -0
  16. data/examples/restful/webspicy/todo/options.yml +1 -1
  17. data/examples/restful/webspicy/todo/patchTodo.yml +3 -0
  18. data/examples/restful/webspicy/todo/postFile.yml +1 -1
  19. data/examples/restful/webspicy/todo/putTodo.yml +65 -0
  20. data/examples/single_spec/spec.yml +59 -0
  21. data/examples/website/config.rb +2 -0
  22. data/examples/website/schema.fio +1 -0
  23. data/examples/website/specification/get-http.yml +34 -0
  24. data/examples/website/specification/get-https.yml +34 -0
  25. data/lib/finitio/webspicy/scalars.fio +25 -0
  26. data/lib/webspicy.rb +48 -17
  27. data/lib/webspicy/checker.rb +2 -2
  28. data/lib/webspicy/configuration.rb +70 -14
  29. data/lib/webspicy/configuration/scope.rb +162 -0
  30. data/lib/webspicy/configuration/single_url.rb +58 -0
  31. data/lib/webspicy/configuration/single_yml_file.rb +30 -0
  32. data/lib/webspicy/formaldoc.fio +24 -9
  33. data/lib/webspicy/mocker.rb +8 -8
  34. data/lib/webspicy/openapi.rb +1 -0
  35. data/lib/webspicy/openapi/generator.rb +127 -0
  36. data/lib/webspicy/{resource.rb → specification.rb} +26 -5
  37. data/lib/webspicy/specification/file_upload.rb +37 -0
  38. data/lib/webspicy/specification/postcondition.rb +16 -0
  39. data/lib/webspicy/specification/precondition.rb +19 -0
  40. data/lib/webspicy/specification/precondition/global_request_headers.rb +35 -0
  41. data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +68 -0
  42. data/lib/webspicy/{resource → specification}/service.rb +11 -6
  43. data/lib/webspicy/specification/test_case.rb +139 -0
  44. data/lib/webspicy/support.rb +1 -0
  45. data/lib/webspicy/support/colorize.rb +28 -0
  46. data/lib/webspicy/support/status_range.rb +6 -1
  47. data/lib/webspicy/tester.rb +16 -11
  48. data/lib/webspicy/tester/asserter.rb +3 -2
  49. data/lib/webspicy/tester/assertions.rb +5 -1
  50. data/lib/webspicy/tester/client.rb +63 -0
  51. data/lib/webspicy/tester/client/http_client.rb +154 -0
  52. data/lib/webspicy/tester/client/rack_test_client.rb +188 -0
  53. data/lib/webspicy/tester/client/support.rb +65 -0
  54. data/lib/webspicy/tester/invocation.rb +218 -0
  55. data/lib/webspicy/tester/rspec_asserter.rb +108 -0
  56. data/lib/webspicy/tester/rspec_matchers.rb +104 -0
  57. data/lib/webspicy/version.rb +2 -2
  58. data/spec/{unit/spec_helper.rb → spec_helper.rb} +0 -0
  59. data/spec/unit/configuration/scope/test_each_service.rb +49 -0
  60. data/spec/unit/configuration/scope/test_each_specification.rb +68 -0
  61. data/spec/unit/configuration/scope/test_expand_example.rb +65 -0
  62. data/spec/unit/configuration/scope/test_to_real_url.rb +82 -0
  63. data/spec/unit/openapi/test_generator.rb +28 -0
  64. data/spec/unit/specification/precondition/test_global_request_headers.rb +42 -0
  65. data/spec/unit/{resource → specification}/service/test_dress_params.rb +2 -2
  66. data/spec/unit/specification/test_case/test_mutate.rb +24 -0
  67. data/spec/unit/{resource → specification}/test_instantiate_url.rb +5 -5
  68. data/spec/unit/{resource → specification}/test_url_placeholders.rb +4 -4
  69. data/spec/unit/test_configuration.rb +24 -7
  70. data/spec/unit/tester/client/test_around.rb +61 -0
  71. data/spec/unit/tester/test_asserter.rb +51 -0
  72. data/spec/unit/tester/test_assertions.rb +4 -4
  73. data/tasks/test.rake +3 -1
  74. metadata +88 -45
  75. data/LICENSE.md +0 -22
  76. data/lib/webspicy/client.rb +0 -61
  77. data/lib/webspicy/client/http_client.rb +0 -133
  78. data/lib/webspicy/client/rack_test_client.rb +0 -168
  79. data/lib/webspicy/client/support.rb +0 -48
  80. data/lib/webspicy/file_upload.rb +0 -35
  81. data/lib/webspicy/postcondition.rb +0 -14
  82. data/lib/webspicy/precondition.rb +0 -15
  83. data/lib/webspicy/resource/service/invocation.rb +0 -212
  84. data/lib/webspicy/resource/service/test_case.rb +0 -132
  85. data/lib/webspicy/scope.rb +0 -160
  86. data/spec/unit/client/test_around.rb +0 -59
  87. data/spec/unit/scope/test_each_resource.rb +0 -66
  88. data/spec/unit/scope/test_each_service.rb +0 -47
  89. data/spec/unit/scope/test_expand_example.rb +0 -63
  90. data/spec/unit/scope/test_to_real_url.rb +0 -80
@@ -0,0 +1,108 @@
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 response
12
+ invocation.response
13
+ end
14
+
15
+ def test_case
16
+ invocation.test_case
17
+ end
18
+
19
+ def service
20
+ test_case.service
21
+ end
22
+
23
+ def assert!
24
+ assert_status_met
25
+ assert_content_type_met
26
+ assert_expected_headers
27
+ assert_output_schema_met
28
+ assert_assertions_met
29
+ assert_postconditions_met
30
+
31
+ assert_no_other_errors
32
+ end
33
+
34
+ def assert_status_met
35
+ got = response.status
36
+ expected = test_case.expected_status
37
+ rspec.expect(got).to rspec.match_response_status(expected)
38
+ end
39
+
40
+ def assert_content_type_met
41
+ return if test_case.is_expected_status?(204)
42
+ return unless ect = test_case.expected_content_type
43
+ got = response.content_type
44
+ got = got.mime_type if got.respond_to?(:mime_type)
45
+ if ect.nil?
46
+ rspec.expect(ect).to rspec.have_no_response_type
47
+ else
48
+ rspec.expect(got).to rspec.match_content_type(ect)
49
+ end
50
+ end
51
+
52
+ def assert_expected_headers
53
+ return unless test_case.has_expected_headers?
54
+ test_case.expected_headers.each_pair do |k,v|
55
+ got = response.headers[k]
56
+ if got.nil?
57
+ rspec.expect(got).to rspec.be_in_response_headers(k)
58
+ else
59
+ rspec.expect(got).to rspec.match_response_header(k, v)
60
+ end
61
+ end
62
+ end
63
+
64
+ def assert_output_schema_met
65
+ return if test_case.is_expected_status?(204)
66
+ return if invocation.is_redirect?
67
+ if invocation.is_empty_response?
68
+ body = response.body.to_s.strip
69
+ rspec.expect(body).to rspec.be_an_empty_response_body
70
+ else
71
+ b = invocation.dressed_body
72
+ if invocation.is_expected_success?
73
+ rspec.expect(b).to rspec.meet_output_schema
74
+ else
75
+ rspec.expect(b).to rspec.meet_error_schema
76
+ end
77
+ end
78
+ end
79
+
80
+ def assert_assertions_met
81
+ return unless test_case.has_assertions?
82
+ asserter = Tester::Asserter.new(invocation.dressed_body)
83
+ test_case.assert.each do |assert|
84
+ begin
85
+ asserter.instance_eval(assert)
86
+ rescue => ex
87
+ rspec.expect(ex).to rspec.meet_assertion(assert)
88
+ end
89
+ end
90
+ end
91
+
92
+ def assert_postconditions_met
93
+ return unless service.has_postconditions?
94
+ return if test_case.counterexample?
95
+ service.postconditions.each do |post|
96
+ msg = post.check(invocation)
97
+ rspec.expect(msg).to rspec.meet_postcondition(post)
98
+ end
99
+ end
100
+
101
+ def assert_no_other_errors
102
+ errors = invocation.errors
103
+ rspec.expect(errors).to rspec.be_an_empty_errors_array
104
+ end
105
+
106
+ end # class RSpecAsserter
107
+ end # class Tester
108
+ end # module Webspicy
@@ -0,0 +1,104 @@
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 :be_an_empty_errors_array do
98
+ match do |actual|
99
+ actual.empty?
100
+ end
101
+ failure_message_for_should do |actual|
102
+ "expected no webspicy error, got the following ones:\n" + actual.map{|a| " #{a}" }.join("\n")
103
+ end
104
+ end
@@ -1,8 +1,8 @@
1
1
  module Webspicy
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 15
5
- TINY = 5
4
+ MINOR = 16
5
+ TINY = 1
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 = /getTodo.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 = /getTodo.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 == "getTodo.yml"
31
+ }
32
+ }
33
+ }
34
+
35
+ it 'returns only that file' do
36
+ expect(subject.size).to eql(1)
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 = /getTodo.yml/
45
+ }
46
+ }
47
+
48
+ it 'returns only that file' do
49
+ expect(subject.size).to eql(1)
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 '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
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ module Webspicy
3
+ class Configuration
4
+ describe Scope, "expand_example" do
5
+
6
+ subject{ Scope.new({}).send(:expand_example, service, example) }
7
+
8
+ context 'when the service has no default example' do
9
+ let(:service) {
10
+ Webspicy.service({
11
+ method: "GET",
12
+ description: "Test service",
13
+ preconditions: "Foo",
14
+ input_schema: "{ id: Integer }",
15
+ output_schema: "{}",
16
+ error_schema: "{}"
17
+ })
18
+ }
19
+
20
+ let(:example) {
21
+ Webspicy.test_case({
22
+ description: "Hello world"
23
+ })
24
+ }
25
+
26
+ it 'returns the example itself' do
27
+ expect(subject).to be(example)
28
+ end
29
+ end
30
+
31
+ context 'when the service has a default example' do
32
+ let(:service) {
33
+ Webspicy.service({
34
+ method: "GET",
35
+ description: "Test service",
36
+ preconditions: "Foo",
37
+ input_schema: "{ id: Integer }",
38
+ output_schema: "{}",
39
+ error_schema: "{}",
40
+ default_example: {
41
+ expected: { status: 200 }
42
+ }
43
+ })
44
+ }
45
+
46
+ let(:example) {
47
+ Webspicy.test_case({
48
+ description: "Hello world",
49
+ expected: { content_type: "application/json" }
50
+ })
51
+ }
52
+
53
+ it 'deep merges the default example and the example as expected' do
54
+ expect(subject).to be_a(Specification::TestCase)
55
+ expect(subject.description).to eql("Hello world")
56
+ expect(subject.expected).to eql({
57
+ status: Support::StatusRange.int(200),
58
+ content_type: "application/json"
59
+ })
60
+ end
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+ module Webspicy
3
+ class Configuration
4
+ describe Scope, 'to_real_url' do
5
+
6
+ let(:scope) {
7
+ Scope.new(configuration)
8
+ }
9
+
10
+ context 'when no host at all' do
11
+
12
+ let(:configuration) {
13
+ Configuration.new
14
+ }
15
+
16
+ it 'does nothing on absolute URLs already' do
17
+ url = 'http://127.0.0.1:4567/todo'
18
+ got = scope.to_real_url(url)
19
+ expect(got).to eql(url)
20
+ end
21
+
22
+ it 'yields the block relative URLs' do
23
+ got = scope.to_real_url("/todo"){ "hello" }
24
+ expect(got).to eql("hello")
25
+ end
26
+
27
+ it 'fails on relative URLs and no block is given' do
28
+ expect(->(){
29
+ scope.to_real_url("/todo")
30
+ }).to raise_error(/Unable to resolve `\/todo`/)
31
+ end
32
+ end
33
+
34
+ context 'with a static host' do
35
+
36
+ let(:configuration) {
37
+ Configuration.new do |c|
38
+ c.host = "http://127.0.0.1:4568"
39
+ end
40
+ }
41
+
42
+ it 'does nothing on absolute URLs already' do
43
+ url = 'http://127.0.0.1:4567/todo'
44
+ got = scope.to_real_url(url)
45
+ expect(got).to eql(url)
46
+ end
47
+
48
+ it 'resolves relative URLs as expected' do
49
+ url = '/todo'
50
+ got = scope.to_real_url(url)
51
+ expect(got).to eql("http://127.0.0.1:4568/todo")
52
+ end
53
+
54
+ end
55
+
56
+ context 'with a dynamic host resolver' do
57
+
58
+ let(:configuration) {
59
+ Configuration.new do |c|
60
+ c.host = ->(url, tc) {
61
+ "http://127.0.0.1:4568#{url}"
62
+ }
63
+ end
64
+ }
65
+
66
+ it 'resolves absolute URLs' do
67
+ url = 'http://127.0.0.1:4567/todo'
68
+ got = scope.to_real_url(url)
69
+ expect(got).to eql("http://127.0.0.1:4568#{url}")
70
+ end
71
+
72
+ it 'resolves relative URLs as expected' do
73
+ url = '/todo'
74
+ got = scope.to_real_url(url)
75
+ expect(got).to eql("http://127.0.0.1:4568/todo")
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+ end
82
+ end