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
@@ -30,7 +30,7 @@ class MustBeAuthenticated
30
30
  def counterexample(description, role, expected, status = 401)
31
31
  YAML.load <<-YML.gsub(/^\s+[#][ ]/, "")
32
32
  # description: |-
33
- # (#{self.class.name}) #{description}
33
+ # #{description} (#{self.class.name} PRE)
34
34
  # params:
35
35
  # id: 1
36
36
  # dress_params:
@@ -41,7 +41,7 @@ class MustBeAuthenticated
41
41
  # content_type: application/json
42
42
  # status: #{status}
43
43
  # assert:
44
- # - "pathFD('', error: '#{expected}')"
44
+ # - "pathFD('', error: '#{expected}')"
45
45
  YML
46
46
  end
47
47
 
@@ -0,0 +1,18 @@
1
+ class TodoRemoved
2
+ include Webspicy::Specification::Postcondition
3
+
4
+ def self.match(service, descr)
5
+ return TodoRemoved.new if descr =~ /The todo has been removed/
6
+ end
7
+
8
+ def check(invocation)
9
+ client = invocation.client
10
+ id = invocation.test_case.params['id']
11
+ url = "/todo/#{id}"
12
+ response = client.api.get(url, {}, {
13
+ "Accept" => "application/json"
14
+ })
15
+ return nil if response.status == 404
16
+ "Todo `#{id}` was not deleted, it has been found"
17
+ end
18
+ end
@@ -15,13 +15,16 @@ services:
15
15
  preconditions:
16
16
  - Must be an admin
17
17
 
18
+ postconditions:
19
+ - The todo has been removed
20
+
18
21
  input_schema: |-
19
22
  {
20
23
  id: Integer
21
24
  }
22
25
 
23
26
  output_schema: |-
24
- .
27
+ Any
25
28
 
26
29
  error_schema: |-
27
30
  ErrorSchema
@@ -0,0 +1,46 @@
1
+ ---
2
+ url: |-
3
+ /todo/{id}
4
+
5
+ method: |-
6
+ GET
7
+
8
+ description: |-
9
+ Returns a single todo item
10
+
11
+ input_schema: |-
12
+ {
13
+ id: Integer
14
+ }
15
+
16
+ output_schema: |-
17
+ Todo
18
+
19
+ error_schema: |-
20
+ ErrorSchema
21
+
22
+ examples:
23
+
24
+ - description: |-
25
+ when requested on an existing TODO
26
+ params:
27
+ id: 1
28
+ expected:
29
+ content_type: application/json
30
+ status: 200
31
+ assert:
32
+ - "pathFD('', id: 1)"
33
+ - "match('description', /Refactor/)"
34
+ - "notMatch('description', /Foo/)"
35
+
36
+ counterexamples:
37
+
38
+ - description: |-
39
+ when requested on an unexisting TODO
40
+ params:
41
+ id: 999254654
42
+ expected:
43
+ content_type: application/json
44
+ status: 404
45
+ assert:
46
+ - "pathFD('', error: 'No such todo')"
@@ -17,7 +17,7 @@ services:
17
17
  }
18
18
 
19
19
  output_schema: |-
20
- .
20
+ Any
21
21
 
22
22
  error_schema: |-
23
23
  ErrorSchema
@@ -36,6 +36,9 @@ services:
36
36
  params:
37
37
  id: 1
38
38
  description: 'hello world'
39
+ expected:
40
+ content_type: application/json
41
+ status: 200
39
42
  assert:
40
43
  - "pathFD('', description: 'hello world')"
41
44
 
@@ -16,7 +16,7 @@ services:
16
16
  - Must be authenticated
17
17
 
18
18
  input_schema: |-
19
- .
19
+ Any
20
20
 
21
21
  output_schema: |-
22
22
  { count: Integer }
@@ -0,0 +1,65 @@
1
+ ---
2
+ name: |-
3
+ Todo
4
+
5
+ url: |-
6
+ /todo/{id}
7
+
8
+ services:
9
+ - method: |-
10
+ PUT
11
+
12
+ description: |-
13
+ Update a single Todo item
14
+
15
+ preconditions:
16
+ - Must be authenticated
17
+
18
+ input_schema: |-
19
+ TodoPut
20
+
21
+ output_schema: |-
22
+ Todo
23
+
24
+ error_schema: |-
25
+ ErrorSchema
26
+
27
+ default_example:
28
+ expected:
29
+ content_type: application/json
30
+ status: 200
31
+
32
+ examples:
33
+
34
+ - description: |-
35
+ when requested on an existing TODO
36
+ params:
37
+ id: 1
38
+ description: 'hello world'
39
+ assert:
40
+ - "pathFD('', description: 'hello world')"
41
+
42
+ counterexamples:
43
+
44
+ - description: |-
45
+ when requested on an unexisting TODO
46
+ params:
47
+ id: 999254654
48
+ description: 'hello world'
49
+ expected:
50
+ content_type: application/json
51
+ status: 404
52
+ assert:
53
+ - "pathFD('', error: 'No such todo')"
54
+
55
+ - description: |-
56
+ when violating the Put data type
57
+ params:
58
+ id: 1
59
+ description: 'hello world'
60
+ nosuchone: 'foobar'
61
+ dress_params:
62
+ false
63
+ expected:
64
+ content_type: application/json
65
+ status: 400
@@ -0,0 +1,59 @@
1
+ ---
2
+ name: |-
3
+ Todo
4
+
5
+ url: |-
6
+ http://127.0.0.1:4567/todo/{id}
7
+
8
+ services:
9
+ - method: |-
10
+ GET
11
+
12
+ description: |-
13
+ Returns a single todo item
14
+
15
+ input_schema: |-
16
+ {
17
+ id: Integer
18
+ }
19
+
20
+ output_schema: |-
21
+ {
22
+ id: Integer
23
+ description: String
24
+ }
25
+
26
+ error_schema: |-
27
+ {
28
+ error: String
29
+ }
30
+
31
+ examples:
32
+
33
+ - description: |-
34
+ when requested on an existing TODO
35
+ headers:
36
+ Accept: application/json
37
+ params:
38
+ id: 1
39
+ expected:
40
+ content_type: application/json
41
+ status: 200
42
+ assert:
43
+ - "pathFD('', id: 1)"
44
+ - "match('description', /Refactor/)"
45
+ - "notMatch('description', /Foo/)"
46
+
47
+ counterexamples:
48
+
49
+ - description: |-
50
+ when requested on an unexisting TODO
51
+ headers:
52
+ Accept: application/json
53
+ params:
54
+ id: 999254654
55
+ expected:
56
+ content_type: application/json
57
+ status: 404
58
+ assert:
59
+ - "pathFD('', error: 'No such todo')"
@@ -0,0 +1,2 @@
1
+ Webspicy::Configuration.new{|c|
2
+ }
@@ -0,0 +1 @@
1
+ @import webspicy/scalars
@@ -0,0 +1,34 @@
1
+ ---
2
+ name: |-
3
+ Your backend is broken website
4
+
5
+ url: |-
6
+ http://yourbackendisbroken.dev
7
+
8
+ services:
9
+ - method: |-
10
+ GET
11
+
12
+ description: |-
13
+ Redirects to the https version
14
+
15
+ input_schema: |-
16
+ Any
17
+
18
+ output_schema: |-
19
+ Any
20
+
21
+ error_schema: |-
22
+ Any
23
+
24
+ examples:
25
+
26
+ - description: |-
27
+ it works
28
+ params:
29
+ id: 1
30
+ expected:
31
+ content_type: text/html
32
+ status: 3xx
33
+ headers:
34
+ Location: https://yourbackendisbroken.dev/?id=1
@@ -0,0 +1,34 @@
1
+ ---
2
+ name: |-
3
+ Your backend is broken website
4
+
5
+ url: |-
6
+ https://yourbackendisbroken.dev
7
+
8
+ services:
9
+ - method: |-
10
+ GET
11
+
12
+ description: |-
13
+ Returns the web page
14
+
15
+ input_schema: |-
16
+ Any
17
+
18
+ output_schema: |-
19
+ Any
20
+
21
+ error_schema: |-
22
+ Any
23
+
24
+ examples:
25
+
26
+ - description: |-
27
+ it works
28
+ params:
29
+ id: 1
30
+ expected:
31
+ content_type: text/html
32
+ status: 200
33
+ assert:
34
+ - notEmpty
@@ -0,0 +1,25 @@
1
+ # Any
2
+ Any = .
3
+
4
+ # Booleans
5
+ Nil = .NilClass
6
+ True = .TrueClass
7
+ False = .FalseClass
8
+ Boolean = .TrueClass|.FalseClass
9
+
10
+ # Numbers
11
+ Numeric = .Numeric
12
+ Integer = .Integer
13
+ Float = .Float
14
+ Real = .Float
15
+
16
+ # String
17
+ String = .String
18
+
19
+ # Date, Time, DateTime
20
+ Date = .Date <iso8601> .String \( s | Date.iso8601(s) )
21
+ \( d | d.iso8601 )
22
+ Time = .Time <iso8601> .String \( s | Time.iso8601(s) )
23
+ \( t | t.iso8601 )
24
+ DateTime = .DateTime <iso8601> .String \( s | DateTime.iso8601(s) )
25
+ \( t | t.iso8601 )
@@ -7,25 +7,33 @@ require 'ostruct'
7
7
  require 'yaml'
8
8
  require 'rspec'
9
9
  require 'rack/test'
10
- require "mustermann"
10
+ require 'mustermann'
11
+ require 'colorized_string'
12
+ require 'securerandom'
11
13
  module Webspicy
12
14
 
13
15
  ###
14
16
  ### Load library
15
17
  ###
16
18
 
19
+ require 'webspicy/version'
17
20
  require 'webspicy/support'
21
+ require 'webspicy/specification'
18
22
  require 'webspicy/configuration'
19
- require 'webspicy/file_upload'
20
- require 'webspicy/scope'
21
- require 'webspicy/client'
22
- require 'webspicy/resource'
23
- require 'webspicy/precondition'
24
- require 'webspicy/postcondition'
25
23
  require 'webspicy/checker'
26
24
  require 'webspicy/tester'
27
- require 'webspicy/tester/assertions'
28
- require 'webspicy/tester/asserter'
25
+
26
+ ###
27
+ ### Backward compatibility
28
+ ###
29
+ Client = Tester::Client
30
+ HttpClient = Tester::HttpClient
31
+ RackTestClient = Tester::RackTestClient
32
+ Resource = Specification
33
+ Precondition = Specification::Precondition
34
+ Postcondition = Specification::Postcondition
35
+ FileUpload = Specification::FileUpload
36
+ Scope = Configuration::Scope
29
37
 
30
38
  ###
31
39
  ### About folders
@@ -36,30 +44,38 @@ module Webspicy
36
44
  EXAMPLES_FOLDER = ROOT_FOLDER/('examples')
37
45
 
38
46
  ###
39
- ### About formal doc and resources defined there
47
+ ### About formal doc and specifications defined there
40
48
  ###
41
-
49
+ Finitio.stdlib_path(Path.dir/"finitio")
50
+ DEFAULT_SYSTEM = Finitio.system(<<~FIO)
51
+ @import webspicy/scalars
52
+ FIO
42
53
  FORMALDOC = Finitio.system(Path.dir/("webspicy/formaldoc.fio"))
43
54
 
44
55
  # Returns a default scope instance.
45
56
  def default_scope
46
- Scope.new(Configuration.new)
57
+ Configuration::Scope.new(Configuration.new)
47
58
  end
48
59
  module_function :default_scope
49
60
 
50
- def resource(raw, file = nil, scope = default_scope)
61
+ def specification(raw, file = nil, scope = default_scope)
62
+ raw = YAML.load(raw) if raw.is_a?(String)
51
63
  with_scope(scope) do
52
- r = FORMALDOC["Resource"].dress(raw)
64
+ r = FORMALDOC["Specification"].dress(raw)
53
65
  r.located_at!(file) if file
54
66
  r
55
67
  end
68
+ rescue Finitio::Error => ex
69
+ handle_finitio_error(ex, scope)
56
70
  end
57
- module_function :resource
71
+ module_function :specification
58
72
 
59
73
  def service(raw, scope = default_scope)
60
74
  with_scope(scope) do
61
75
  FORMALDOC["Service"].dress(raw)
62
76
  end
77
+ rescue Finitio::Error => ex
78
+ handle_finitio_error(ex)
63
79
  end
64
80
  module_function :service
65
81
 
@@ -67,9 +83,19 @@ module Webspicy
67
83
  with_scope(scope) do
68
84
  FORMALDOC["TestCase"].dress(raw)
69
85
  end
86
+ rescue Finitio::Error => ex
87
+ handle_finitio_error(ex)
70
88
  end
71
89
  module_function :test_case
72
90
 
91
+ def handle_finitio_error(ex, scope)
92
+ # msg = "#{ex.message}:\n #{ex.root_cause.message}"
93
+ # msg = Support::Colorize.colorize_error(msg, scope.config)
94
+ # fatal(msg)
95
+ raise
96
+ end
97
+ module_function :handle_finitio_error
98
+
73
99
  #
74
100
  # Yields the block after having installed `scope` globally.
75
101
  #
@@ -120,7 +146,7 @@ module Webspicy
120
146
  if scope = Thread.current[:webspicy_scope]
121
147
  scope.parse_schema(fio)
122
148
  else
123
- Finitio.system(fio)
149
+ DEFAULT_SYSTEM.system(fio)
124
150
  end
125
151
  end
126
152
  module_function :schema
@@ -132,7 +158,7 @@ module Webspicy
132
158
  LOGGER = ::Logger.new(STDOUT)
133
159
  LOGGER.level = Logger.const_get(ENV['LOG_LEVEL'] || 'WARN')
134
160
  LOGGER.formatter = proc { |severity, datetime, progname, msg|
135
- " " + msg + "\n"
161
+ " " + msg + "\n"
136
162
  }
137
163
 
138
164
  def info(*args, &bl)
@@ -145,4 +171,9 @@ module Webspicy
145
171
  end
146
172
  module_function :debug
147
173
 
174
+ def fatal(*args, &bl)
175
+ LOGGER && LOGGER.fatal(*args, &bl)
176
+ end
177
+ module_function :fatal
178
+
148
179
  end