webspicy 0.15.8 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +76 -24
- data/bin/webspicy +30 -14
- data/examples/restful/Gemfile.lock +40 -24
- data/examples/restful/Rakefile +0 -1
- data/examples/restful/app.rb +4 -1
- data/examples/restful/webspicy/config.rb +9 -0
- data/examples/restful/webspicy/{todo/deleteTodo.yml → formaldef/todo/_one/delete.yml} +7 -1
- data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +46 -0
- data/examples/restful/webspicy/{todo/getTodo.yml → formaldef/todo/_one/get.yml} +0 -0
- data/examples/restful/webspicy/{todo/patchTodo.yml → formaldef/todo/_one/patch.yml} +3 -0
- data/examples/restful/webspicy/{todo/putTodo.yml → formaldef/todo/_one/put.yml} +0 -0
- data/examples/restful/webspicy/{todo/getTodos.yml → formaldef/todo/get.yml} +0 -0
- data/examples/restful/webspicy/{todo → formaldef/todo}/options.yml +1 -1
- data/examples/restful/webspicy/{todo/postCsv.yml → formaldef/todo/post.csv.yml} +0 -0
- data/examples/restful/webspicy/{todo/postFile.yml → formaldef/todo/post.file.yml} +1 -1
- data/examples/restful/webspicy/{todo/postTodos.yml → formaldef/todo/post.yml} +0 -0
- data/examples/restful/webspicy/{todo → formaldef/todo}/todos.csv +0 -0
- data/examples/restful/webspicy/rack.rb +1 -1
- data/examples/restful/webspicy/real.rb +1 -1
- data/examples/restful/webspicy/schema.fio +2 -2
- data/examples/restful/webspicy/support/must_be_authenticated.rb +2 -2
- data/examples/restful/webspicy/support/todo_not_removed.rb +21 -0
- data/examples/restful/webspicy/support/todo_removed.rb +20 -0
- data/examples/single_spec/spec.yml +59 -0
- data/examples/website/config.rb +2 -0
- data/examples/website/schema.fio +1 -0
- data/examples/website/specification/get-http.yml +30 -0
- data/examples/website/specification/get-https.yml +30 -0
- data/lib/finitio/webspicy/scalars.fio +25 -0
- data/lib/webspicy.rb +49 -17
- data/lib/webspicy/checker.rb +5 -20
- data/lib/webspicy/configuration.rb +79 -14
- data/lib/webspicy/configuration/scope.rb +154 -0
- data/lib/webspicy/configuration/single_url.rb +58 -0
- data/lib/webspicy/configuration/single_yml_file.rb +30 -0
- data/lib/webspicy/formaldoc.fio +25 -8
- data/lib/webspicy/mocker.rb +8 -8
- data/lib/webspicy/mocker/config.ru +5 -0
- data/lib/webspicy/openapi.rb +1 -0
- data/lib/webspicy/openapi/generator.rb +127 -0
- data/lib/webspicy/rspec/checker.rb +2 -0
- data/lib/webspicy/rspec/checker/rspec_checker.rb +24 -0
- data/lib/webspicy/rspec/support/rspec_runnable.rb +27 -0
- data/lib/webspicy/rspec/tester.rb +4 -0
- data/lib/webspicy/rspec/tester/rspec_asserter.rb +121 -0
- data/lib/webspicy/rspec/tester/rspec_matchers.rb +114 -0
- data/lib/webspicy/rspec/tester/rspec_tester.rb +63 -0
- data/lib/webspicy/{resource.rb → specification.rb} +31 -10
- data/lib/webspicy/specification/errcondition.rb +16 -0
- data/lib/webspicy/specification/file_upload.rb +37 -0
- data/lib/webspicy/specification/postcondition.rb +16 -0
- data/lib/webspicy/specification/precondition.rb +19 -0
- data/lib/webspicy/specification/precondition/global_request_headers.rb +35 -0
- data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +68 -0
- data/lib/webspicy/{resource → specification}/service.rb +38 -25
- data/lib/webspicy/specification/test_case.rb +133 -0
- data/lib/webspicy/support.rb +2 -0
- data/lib/webspicy/support/colorize.rb +28 -0
- data/lib/webspicy/support/data_object.rb +25 -0
- data/lib/webspicy/support/status_range.rb +6 -1
- data/lib/webspicy/tester.rb +8 -77
- data/lib/webspicy/tester/asserter.rb +11 -5
- data/lib/webspicy/tester/assertions.rb +13 -10
- data/lib/webspicy/tester/client.rb +63 -0
- data/lib/webspicy/tester/client/http_client.rb +154 -0
- data/lib/webspicy/tester/client/rack_test_client.rb +188 -0
- data/lib/webspicy/tester/client/support.rb +65 -0
- data/lib/webspicy/tester/failure.rb +6 -0
- data/lib/webspicy/tester/invocation.rb +70 -0
- data/lib/webspicy/version.rb +2 -2
- data/spec/{unit/spec_helper.rb → spec_helper.rb} +0 -0
- data/spec/unit/configuration/scope/test_each_service.rb +49 -0
- data/spec/unit/configuration/scope/test_each_specification.rb +68 -0
- data/spec/unit/configuration/scope/test_expand_example.rb +65 -0
- data/spec/unit/configuration/scope/test_to_real_url.rb +82 -0
- data/spec/unit/openapi/test_generator.rb +28 -0
- data/spec/unit/specification/precondition/test_global_request_headers.rb +42 -0
- data/spec/unit/{resource → specification}/service/test_dress_params.rb +2 -2
- data/spec/unit/specification/test_case/test_mutate.rb +24 -0
- data/spec/unit/{resource → specification}/test_instantiate_url.rb +5 -5
- data/spec/unit/{resource → specification}/test_url_placeholders.rb +4 -4
- data/spec/unit/test_configuration.rb +24 -7
- data/spec/unit/tester/client/test_around.rb +61 -0
- data/spec/unit/tester/test_asserter.rb +246 -0
- data/spec/unit/tester/test_assertions.rb +12 -10
- data/tasks/test.rake +3 -1
- metadata +106 -48
- data/LICENSE.md +0 -22
- data/lib/webspicy/client.rb +0 -61
- data/lib/webspicy/client/http_client.rb +0 -145
- data/lib/webspicy/client/rack_test_client.rb +0 -181
- data/lib/webspicy/client/support.rb +0 -48
- data/lib/webspicy/file_upload.rb +0 -35
- data/lib/webspicy/postcondition.rb +0 -14
- data/lib/webspicy/precondition.rb +0 -15
- data/lib/webspicy/resource/service/invocation.rb +0 -212
- data/lib/webspicy/resource/service/test_case.rb +0 -132
- data/lib/webspicy/scope.rb +0 -160
- data/spec/unit/client/test_around.rb +0 -59
- data/spec/unit/scope/test_each_resource.rb +0 -66
- data/spec/unit/scope/test_each_service.rb +0 -47
- data/spec/unit/scope/test_expand_example.rb +0 -63
- data/spec/unit/scope/test_to_real_url.rb +0 -80
@@ -0,0 +1,133 @@
|
|
1
|
+
module Webspicy
|
2
|
+
class Specification
|
3
|
+
class TestCase
|
4
|
+
include Support::DataObject
|
5
|
+
|
6
|
+
def initialize(raw)
|
7
|
+
super(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 specification
|
24
|
+
service.specification
|
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(specification) : nil
|
70
|
+
end
|
71
|
+
|
72
|
+
def expected
|
73
|
+
@raw[:expected] || {}
|
74
|
+
end
|
75
|
+
|
76
|
+
def expected_content_type
|
77
|
+
expected[:content_type]
|
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 instrument(client)
|
113
|
+
service.preconditions.each do |pre|
|
114
|
+
pre.instrument(self, client) if pre.respond_to?(:instrument)
|
115
|
+
end
|
116
|
+
service.postconditions.each do |post|
|
117
|
+
post.instrument(self, client) if post.respond_to?(:instrument)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def mutate(override)
|
122
|
+
m = self.dup
|
123
|
+
m.raw = self.raw.merge(override)
|
124
|
+
m
|
125
|
+
end
|
126
|
+
|
127
|
+
def to_s
|
128
|
+
description
|
129
|
+
end
|
130
|
+
|
131
|
+
end # class TestCase
|
132
|
+
end # class Specification
|
133
|
+
end # module Webspicy
|
data/lib/webspicy/support.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Webspicy
|
2
|
+
module Support
|
3
|
+
module Colorize
|
4
|
+
|
5
|
+
def colorize(str, kind, config = nil)
|
6
|
+
color = (config || self.config).colors[kind]
|
7
|
+
ColorizedString[str].colorize(color)
|
8
|
+
end
|
9
|
+
module_function :colorize
|
10
|
+
|
11
|
+
def colorize_highlight(str, cfg = nil)
|
12
|
+
colorize(str, :highlight, cfg)
|
13
|
+
end
|
14
|
+
module_function :colorize_highlight
|
15
|
+
|
16
|
+
def colorize_success(str, cfg = nil)
|
17
|
+
colorize(str, :success, cfg)
|
18
|
+
end
|
19
|
+
module_function :colorize_success
|
20
|
+
|
21
|
+
def colorize_error(str, cfg = nil)
|
22
|
+
colorize(str, :error, cfg)
|
23
|
+
end
|
24
|
+
module_function :colorize_error
|
25
|
+
|
26
|
+
end # module Colorize
|
27
|
+
end # module Support
|
28
|
+
end # module Webspicy
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Webspicy
|
2
|
+
module Support
|
3
|
+
module DataObject
|
4
|
+
|
5
|
+
def initialize(raw)
|
6
|
+
@raw = raw
|
7
|
+
end
|
8
|
+
attr_accessor :raw
|
9
|
+
protected :raw, :raw=
|
10
|
+
|
11
|
+
def method_missing(name, *args, &bl)
|
12
|
+
if @raw.has_key?(name) && args.empty? && bl.nil?
|
13
|
+
@raw[name]
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_info
|
20
|
+
@raw
|
21
|
+
end
|
22
|
+
|
23
|
+
end # module DataObject
|
24
|
+
end # module Support
|
25
|
+
end # module Webspicy
|
data/lib/webspicy/tester.rb
CHANGED
@@ -1,84 +1,15 @@
|
|
1
1
|
module Webspicy
|
2
2
|
class Tester
|
3
3
|
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
attr_reader :config
|
9
|
-
|
10
|
-
def call(err=$stderr, out=$stdout)
|
11
|
-
$_rspec_core_load_started_at = nil
|
12
|
-
options = RSpec::Core::ConfigurationOptions.new(config.rspec_options)
|
13
|
-
conf = RSpec::Core::Configuration.new
|
14
|
-
RSpec::Core::Runner.new(options, conf).run(err, out)
|
15
|
-
end
|
16
|
-
|
17
|
-
# protected
|
18
|
-
|
19
|
-
def load
|
20
|
-
tester = self
|
21
|
-
RSpec.reset
|
22
|
-
rspec_config!
|
23
|
-
RSpec.describe "" do
|
24
|
-
before(:all) do
|
25
|
-
tester.config.listeners(:before_all).each do |l|
|
26
|
-
l.call(tester.config)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
after(:all) do
|
30
|
-
tester.config.listeners(:after_all).each do |l|
|
31
|
-
l.call(tester.config)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
tester.config.each_scope do |scope|
|
35
|
-
client = scope.get_client
|
36
|
-
scope.each_resource do |resource|
|
37
|
-
scope.each_service(resource) do |service|
|
38
|
-
tester.rspec_service!(self, service, client, scope)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
self
|
44
|
-
end
|
45
|
-
|
46
|
-
def rspec_service!(on, service, client, scope)
|
47
|
-
on.describe service do
|
48
|
-
scope.each_testcase(service) do |test_case|
|
49
|
-
describe test_case do
|
50
|
-
include_examples 'a successful test case invocation', client, test_case
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def rspec_config!
|
57
|
-
RSpec.shared_examples "a successful test case invocation" do |client, test_case|
|
58
|
-
|
59
|
-
around(:each) do |example|
|
60
|
-
client.around(test_case) do
|
61
|
-
client.before(test_case)
|
62
|
-
test_case.instrument(client)
|
63
|
-
client.instrument(test_case)
|
64
|
-
@invocation = client.call(test_case)
|
65
|
-
example.run
|
66
|
-
client.after(test_case, @invocation)
|
67
|
-
@invocation
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
let(:invocation) do
|
72
|
-
@invocation
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'works' do
|
76
|
-
raise "Test not ran" unless invocation.done?
|
77
|
-
errors = invocation.errors
|
78
|
-
raise "\n* " + errors.join("\n* ") + "\n" unless errors.empty?
|
79
|
-
end
|
80
|
-
end
|
4
|
+
def self.new(*args, &bl)
|
5
|
+
require_relative 'rspec/tester'
|
6
|
+
RSpecTester.new(*args, &bl)
|
81
7
|
end
|
82
8
|
|
83
9
|
end # class Tester
|
84
10
|
end # module Webspicy
|
11
|
+
require_relative 'tester/client'
|
12
|
+
require_relative 'tester/invocation'
|
13
|
+
require_relative 'tester/failure'
|
14
|
+
require_relative 'tester/assertions'
|
15
|
+
require_relative 'tester/asserter'
|
@@ -54,7 +54,8 @@ module Webspicy
|
|
54
54
|
def size(path, expected = NO_ARG)
|
55
55
|
path, expected = '', path if expected == NO_ARG
|
56
56
|
unless @assertions.size(@target, path, expected)
|
57
|
-
|
57
|
+
actual = @assertions.actual_size(@target, path)
|
58
|
+
_! "Expected #{_s(@target, path)} to have a size of #{expected}, actual size is: #{actual}"
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
@@ -77,14 +78,19 @@ module Webspicy
|
|
77
78
|
expected = id
|
78
79
|
id, path = path, ''
|
79
80
|
end
|
80
|
-
|
81
|
-
|
81
|
+
element = @assertions.element_with_id(@target, path, id)
|
82
|
+
unless element
|
83
|
+
_! "Expected an element with id #{id} to contain the key(s) and value(s) #{expected}, but there is no element with that id"
|
84
|
+
end
|
85
|
+
|
86
|
+
unless @assertions.idFD(element, expected)
|
87
|
+
_! "Expected #{_s(@target, path)} to contain the key(s) and value(s) #{expected}"
|
82
88
|
end
|
83
89
|
end
|
84
90
|
|
85
91
|
def pathFD(path, expected)
|
86
92
|
unless @assertions.pathFD(@target, path, expected)
|
87
|
-
_! "Expected #{_s(@target, path)} to
|
93
|
+
_! "Expected #{_s(@target, path)} to contain the key(s) and value(s) #{expected}"
|
88
94
|
end
|
89
95
|
end
|
90
96
|
|
@@ -120,7 +126,7 @@ module Webspicy
|
|
120
126
|
end
|
121
127
|
|
122
128
|
def _!(msg)
|
123
|
-
raise msg
|
129
|
+
raise Failure, msg
|
124
130
|
end
|
125
131
|
|
126
132
|
end # class Asserter
|
@@ -37,8 +37,12 @@ module Webspicy
|
|
37
37
|
|
38
38
|
def size(target, path, expected = NO_ARG)
|
39
39
|
path, expected = '', path if expected == NO_ARG
|
40
|
+
actual_size(target, path) == expected
|
41
|
+
end
|
42
|
+
|
43
|
+
def actual_size(target, path)
|
40
44
|
target = extract_path(target, path)
|
41
|
-
respond_to!(target, :size).size
|
45
|
+
respond_to!(target, :size).size
|
42
46
|
end
|
43
47
|
|
44
48
|
def idIn(target, path, expected = NO_ARG)
|
@@ -59,16 +63,15 @@ module Webspicy
|
|
59
63
|
(ids.to_set & expected.to_set).empty?
|
60
64
|
end
|
61
65
|
|
62
|
-
def
|
63
|
-
if expected == NO_ARG
|
64
|
-
expected = id
|
65
|
-
id, path = path, ''
|
66
|
-
end
|
66
|
+
def element_with_id(target, path, id)
|
67
67
|
target = extract_path(target, path)
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
an_array(target).find { |t| t[:id] == id }
|
69
|
+
end
|
70
|
+
|
71
|
+
def idFD(element, expected)
|
72
|
+
expected.keys.all? do |k|
|
73
|
+
value_equal(expected[k], element[k])
|
74
|
+
end
|
72
75
|
end
|
73
76
|
|
74
77
|
def pathFD(target, path, expected)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Webspicy
|
2
|
+
class Tester
|
3
|
+
class Client
|
4
|
+
|
5
|
+
def initialize(scope)
|
6
|
+
@scope = scope
|
7
|
+
end
|
8
|
+
attr_reader :scope
|
9
|
+
|
10
|
+
def config
|
11
|
+
scope.config
|
12
|
+
end
|
13
|
+
|
14
|
+
def around(*args, &bl)
|
15
|
+
args << self
|
16
|
+
ls = config.listeners(:around_each)
|
17
|
+
if ls.size == 0
|
18
|
+
bl.call
|
19
|
+
elsif ls.size > 1
|
20
|
+
_around(ls.first, ls[1..-1], args, &bl)
|
21
|
+
else
|
22
|
+
ls.first.call(*args, &bl)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def _around(head, tail, args, &bl)
|
27
|
+
head.call(*args) do
|
28
|
+
if tail.empty?
|
29
|
+
bl.call
|
30
|
+
else
|
31
|
+
_around(tail.first, tail[1..-1], args, &bl)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
private :_around
|
36
|
+
|
37
|
+
def instrument(*args, &bl)
|
38
|
+
args << self
|
39
|
+
config.listeners(:instrument).each do |i|
|
40
|
+
i.call(*args, &bl)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def before(*args, &bl)
|
45
|
+
args << self
|
46
|
+
config.listeners(:before_each).each do |beach|
|
47
|
+
beach.call(*args, &bl)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def after(*args, &bl)
|
52
|
+
args << self
|
53
|
+
config.listeners(:after_each).each do |aeach|
|
54
|
+
aeach.call(*args, &bl)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end # class Client
|
59
|
+
end # class Tester
|
60
|
+
end # module Webspicy
|
61
|
+
require_relative 'client/support'
|
62
|
+
require_relative 'client/http_client'
|
63
|
+
require_relative 'client/rack_test_client'
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module Webspicy
|
2
|
+
class Tester
|
3
|
+
class HttpClient < Client
|
4
|
+
|
5
|
+
class ::HTTP::Request
|
6
|
+
|
7
|
+
# We monkey patch the URI normalization on Http because
|
8
|
+
# we don't want it to interfere with URIs that are encoded
|
9
|
+
# in tests, especially security tests.
|
10
|
+
def normalize_uri(uri)
|
11
|
+
uri
|
12
|
+
end
|
13
|
+
|
14
|
+
end # class ::HTTP::Request
|
15
|
+
|
16
|
+
def initialize(scope)
|
17
|
+
super(scope)
|
18
|
+
@api = Api.new(scope)
|
19
|
+
end
|
20
|
+
attr_reader :api
|
21
|
+
|
22
|
+
def call(test_case)
|
23
|
+
service, specification = test_case.service, test_case.specification
|
24
|
+
|
25
|
+
# Instantiate the parameters
|
26
|
+
headers = test_case.headers
|
27
|
+
params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
|
28
|
+
body = test_case.body || test_case.located_file_upload
|
29
|
+
|
30
|
+
# Instantiate the url and strip parameters
|
31
|
+
url, params = specification.instantiate_url(params)
|
32
|
+
|
33
|
+
# Globalize the URL if required
|
34
|
+
url = scope.to_real_url(url, test_case)
|
35
|
+
|
36
|
+
# Invoke the service now
|
37
|
+
api.public_send(service.method.to_s.downcase.to_sym, url, params, headers, body)
|
38
|
+
|
39
|
+
# Return the response
|
40
|
+
api.last_response
|
41
|
+
end
|
42
|
+
|
43
|
+
class Api
|
44
|
+
include Client::Support
|
45
|
+
|
46
|
+
attr_reader :last_response
|
47
|
+
|
48
|
+
def initialize(scope)
|
49
|
+
@scope = scope
|
50
|
+
end
|
51
|
+
|
52
|
+
def config
|
53
|
+
@scope.config
|
54
|
+
end
|
55
|
+
|
56
|
+
def options(url, params = {}, headers = nil, body = nil)
|
57
|
+
info_request("OPTIONS", url, params, headers, body)
|
58
|
+
|
59
|
+
params = querystring_params(params)
|
60
|
+
@last_response = HTTP[headers || {}].options(url, params: params)
|
61
|
+
|
62
|
+
debug_response(@last_response)
|
63
|
+
|
64
|
+
@last_response
|
65
|
+
end
|
66
|
+
|
67
|
+
def get(url, params = {}, headers = nil, body = nil)
|
68
|
+
info_request("GET", url, params, headers, body)
|
69
|
+
|
70
|
+
params = querystring_params(params)
|
71
|
+
@last_response = HTTP[headers || {}].get(url, params: params)
|
72
|
+
|
73
|
+
debug_response(@last_response)
|
74
|
+
|
75
|
+
@last_response
|
76
|
+
end
|
77
|
+
|
78
|
+
def post(url, params = {}, headers = nil, body = nil)
|
79
|
+
info_request("POST", url, params, headers, body)
|
80
|
+
|
81
|
+
url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
|
82
|
+
|
83
|
+
headers ||= {}
|
84
|
+
|
85
|
+
case body
|
86
|
+
when NilClass
|
87
|
+
headers['Content-Type'] ||= 'application/json'
|
88
|
+
@last_response = HTTP[headers].post(url, body: params.to_json)
|
89
|
+
when FileUpload
|
90
|
+
file = HTTP::FormData::File.new(body.path.to_s, {
|
91
|
+
content_type: body.content_type,
|
92
|
+
filename: body.path.basename.to_s
|
93
|
+
})
|
94
|
+
@last_response = HTTP[headers].post(url, form: {
|
95
|
+
body.param_name.to_sym => file
|
96
|
+
})
|
97
|
+
else
|
98
|
+
headers['Content-Type'] ||= 'application/json'
|
99
|
+
@last_response = HTTP[headers].post(url, body: body)
|
100
|
+
end
|
101
|
+
|
102
|
+
debug_response(@last_response)
|
103
|
+
|
104
|
+
@last_response
|
105
|
+
end
|
106
|
+
|
107
|
+
def patch(url, params = {}, headers = nil, body = nil)
|
108
|
+
info_request("PATCH", url, params, headers, body)
|
109
|
+
|
110
|
+
headers ||= {}
|
111
|
+
headers['Content-Type'] ||= 'application/json'
|
112
|
+
@last_response = HTTP[headers].patch(url, body: params.to_json)
|
113
|
+
|
114
|
+
debug_response(@last_response)
|
115
|
+
|
116
|
+
@last_response
|
117
|
+
end
|
118
|
+
|
119
|
+
def put(url, params = {}, headers = nil, body = nil)
|
120
|
+
info_request("PUT", url, params, headers, body)
|
121
|
+
|
122
|
+
headers ||= {}
|
123
|
+
headers['Content-Type'] ||= 'application/json'
|
124
|
+
@last_response = HTTP[headers].put(url, body: params.to_json)
|
125
|
+
|
126
|
+
debug_response(@last_response)
|
127
|
+
|
128
|
+
@last_response
|
129
|
+
end
|
130
|
+
|
131
|
+
def post_form(url, params = {}, headers = nil, body = nil)
|
132
|
+
info_request("POST", url, params, headers, body)
|
133
|
+
|
134
|
+
@last_response = HTTP[headers || {}].post(url, form: params)
|
135
|
+
|
136
|
+
debug_response(@last_response)
|
137
|
+
|
138
|
+
@last_response
|
139
|
+
end
|
140
|
+
|
141
|
+
def delete(url, params = {}, headers = nil, body = nil)
|
142
|
+
info_request("DELETE", url, params, headers, body)
|
143
|
+
|
144
|
+
@last_response = HTTP[headers || {}].delete(url, body: params.to_json)
|
145
|
+
|
146
|
+
debug_response(@last_response)
|
147
|
+
|
148
|
+
@last_response
|
149
|
+
end
|
150
|
+
end # class Api
|
151
|
+
|
152
|
+
end # class HttpClient
|
153
|
+
end # class Tester
|
154
|
+
end # module Webspicy
|