evil-client 0.3.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +0 -11
- data/.gitignore +1 -0
- data/.rspec +0 -1
- data/.rubocop.yml +22 -19
- data/.travis.yml +1 -0
- data/CHANGELOG.md +251 -6
- data/LICENSE.txt +3 -1
- data/README.md +47 -81
- data/docs/helpers/body.md +93 -0
- data/docs/helpers/connection.md +19 -0
- data/docs/helpers/headers.md +72 -0
- data/docs/helpers/http_method.md +39 -0
- data/docs/helpers/let.md +14 -0
- data/docs/helpers/logger.md +24 -0
- data/docs/helpers/middleware.md +56 -0
- data/docs/helpers/operation.md +103 -0
- data/docs/helpers/option.md +50 -0
- data/docs/helpers/path.md +37 -0
- data/docs/helpers/query.md +59 -0
- data/docs/helpers/response.md +40 -0
- data/docs/helpers/scope.md +121 -0
- data/docs/helpers/security.md +102 -0
- data/docs/helpers/validate.md +68 -0
- data/docs/index.md +70 -78
- data/docs/license.md +5 -1
- data/docs/rspec.md +96 -0
- data/evil-client.gemspec +10 -8
- data/lib/evil/client.rb +126 -72
- data/lib/evil/client/builder.rb +47 -0
- data/lib/evil/client/builder/operation.rb +40 -0
- data/lib/evil/client/builder/scope.rb +31 -0
- data/lib/evil/client/chaining.rb +17 -0
- data/lib/evil/client/connection.rb +60 -20
- data/lib/evil/client/container.rb +66 -0
- data/lib/evil/client/container/operation.rb +23 -0
- data/lib/evil/client/container/scope.rb +28 -0
- data/lib/evil/client/exceptions/definition_error.rb +15 -0
- data/lib/evil/client/exceptions/name_error.rb +32 -0
- data/lib/evil/client/exceptions/response_error.rb +42 -0
- data/lib/evil/client/exceptions/type_error.rb +29 -0
- data/lib/evil/client/exceptions/validation_error.rb +27 -0
- data/lib/evil/client/formatter.rb +49 -0
- data/lib/evil/client/formatter/form.rb +45 -0
- data/lib/evil/client/formatter/multipart.rb +33 -0
- data/lib/evil/client/formatter/part.rb +66 -0
- data/lib/evil/client/formatter/text.rb +21 -0
- data/lib/evil/client/resolver.rb +84 -0
- data/lib/evil/client/resolver/body.rb +22 -0
- data/lib/evil/client/resolver/format.rb +30 -0
- data/lib/evil/client/resolver/headers.rb +46 -0
- data/lib/evil/client/resolver/http_method.rb +34 -0
- data/lib/evil/client/resolver/middleware.rb +36 -0
- data/lib/evil/client/resolver/query.rb +39 -0
- data/lib/evil/client/resolver/request.rb +96 -0
- data/lib/evil/client/resolver/response.rb +26 -0
- data/lib/evil/client/resolver/security.rb +113 -0
- data/lib/evil/client/resolver/uri.rb +35 -0
- data/lib/evil/client/rspec.rb +127 -0
- data/lib/evil/client/schema.rb +105 -0
- data/lib/evil/client/schema/operation.rb +177 -0
- data/lib/evil/client/schema/scope.rb +73 -0
- data/lib/evil/client/settings.rb +172 -0
- data/lib/evil/client/settings/validator.rb +64 -0
- data/mkdocs.yml +21 -15
- data/spec/features/custom_connection_spec.rb +17 -0
- data/spec/features/operation/middleware_spec.rb +50 -0
- data/spec/features/operation/options_spec.rb +71 -0
- data/spec/features/operation/request_spec.rb +94 -0
- data/spec/features/operation/response_spec.rb +48 -0
- data/spec/features/scope/options_spec.rb +52 -0
- data/spec/fixtures/locales/en.yml +16 -0
- data/spec/fixtures/test_client.rb +76 -0
- data/spec/spec_helper.rb +18 -6
- data/spec/support/fixtures_helper.rb +7 -0
- data/spec/unit/builder/operation_spec.rb +90 -0
- data/spec/unit/builder/scope_spec.rb +84 -0
- data/spec/unit/client_spec.rb +137 -0
- data/spec/unit/connection_spec.rb +78 -0
- data/spec/unit/container/operation_spec.rb +81 -0
- data/spec/unit/container/scope_spec.rb +61 -0
- data/spec/unit/container_spec.rb +107 -0
- data/spec/unit/exceptions/definition_error_spec.rb +15 -0
- data/spec/unit/exceptions/name_error_spec.rb +77 -0
- data/spec/unit/exceptions/response_error_spec.rb +22 -0
- data/spec/unit/exceptions/type_error_spec.rb +71 -0
- data/spec/unit/exceptions/validation_error_spec.rb +13 -0
- data/spec/unit/formatter/form_spec.rb +27 -0
- data/spec/unit/formatter/multipart_spec.rb +23 -0
- data/spec/unit/formatter/part_spec.rb +49 -0
- data/spec/unit/formatter/text_spec.rb +37 -0
- data/spec/unit/formatter_spec.rb +46 -0
- data/spec/unit/resolver/body_spec.rb +65 -0
- data/spec/unit/resolver/format_spec.rb +66 -0
- data/spec/unit/resolver/headers_spec.rb +93 -0
- data/spec/unit/resolver/http_method_spec.rb +67 -0
- data/spec/unit/resolver/middleware_spec.rb +83 -0
- data/spec/unit/resolver/query_spec.rb +85 -0
- data/spec/unit/resolver/request_spec.rb +121 -0
- data/spec/unit/resolver/response_spec.rb +64 -0
- data/spec/unit/resolver/security_spec.rb +156 -0
- data/spec/unit/resolver/uri_spec.rb +117 -0
- data/spec/unit/rspec_spec.rb +342 -0
- data/spec/unit/schema/operation_spec.rb +309 -0
- data/spec/unit/schema/scope_spec.rb +110 -0
- data/spec/unit/schema_spec.rb +157 -0
- data/spec/unit/settings/validator_spec.rb +128 -0
- data/spec/unit/settings_spec.rb +248 -0
- metadata +192 -135
- data/docs/base_url.md +0 -38
- data/docs/documentation.md +0 -9
- data/docs/headers.md +0 -59
- data/docs/http_method.md +0 -31
- data/docs/model.md +0 -173
- data/docs/operation.md +0 -0
- data/docs/overview.md +0 -0
- data/docs/path.md +0 -48
- data/docs/query.md +0 -99
- data/docs/responses.md +0 -66
- data/docs/security.md +0 -102
- data/docs/settings.md +0 -32
- data/lib/evil/client/connection/net_http.rb +0 -57
- data/lib/evil/client/dsl.rb +0 -127
- data/lib/evil/client/dsl/base.rb +0 -26
- data/lib/evil/client/dsl/files.rb +0 -37
- data/lib/evil/client/dsl/headers.rb +0 -16
- data/lib/evil/client/dsl/http_method.rb +0 -24
- data/lib/evil/client/dsl/operation.rb +0 -91
- data/lib/evil/client/dsl/operations.rb +0 -41
- data/lib/evil/client/dsl/path.rb +0 -25
- data/lib/evil/client/dsl/query.rb +0 -16
- data/lib/evil/client/dsl/response.rb +0 -61
- data/lib/evil/client/dsl/responses.rb +0 -29
- data/lib/evil/client/dsl/scope.rb +0 -27
- data/lib/evil/client/dsl/security.rb +0 -57
- data/lib/evil/client/dsl/verifier.rb +0 -35
- data/lib/evil/client/middleware.rb +0 -81
- data/lib/evil/client/middleware/base.rb +0 -11
- data/lib/evil/client/middleware/merge_security.rb +0 -20
- data/lib/evil/client/middleware/normalize_headers.rb +0 -17
- data/lib/evil/client/middleware/stringify_form.rb +0 -40
- data/lib/evil/client/middleware/stringify_json.rb +0 -19
- data/lib/evil/client/middleware/stringify_multipart.rb +0 -36
- data/lib/evil/client/middleware/stringify_multipart/part.rb +0 -36
- data/lib/evil/client/middleware/stringify_query.rb +0 -35
- data/lib/evil/client/operation.rb +0 -34
- data/lib/evil/client/operation/request.rb +0 -26
- data/lib/evil/client/operation/response.rb +0 -39
- data/lib/evil/client/operation/response_error.rb +0 -13
- data/lib/evil/client/operation/unexpected_response_error.rb +0 -19
- data/spec/features/instantiation_spec.rb +0 -68
- data/spec/features/middleware_spec.rb +0 -79
- data/spec/features/operation_with_documentation_spec.rb +0 -41
- data/spec/features/operation_with_files_spec.rb +0 -40
- data/spec/features/operation_with_form_body_spec.rb +0 -158
- data/spec/features/operation_with_headers_spec.rb +0 -99
- data/spec/features/operation_with_http_method_spec.rb +0 -45
- data/spec/features/operation_with_json_body_spec.rb +0 -156
- data/spec/features/operation_with_nested_responses_spec.rb +0 -95
- data/spec/features/operation_with_path_spec.rb +0 -47
- data/spec/features/operation_with_query_spec.rb +0 -84
- data/spec/features/operation_with_security_spec.rb +0 -228
- data/spec/features/scoping_spec.rb +0 -48
- data/spec/support/test_client.rb +0 -15
- data/spec/unit/evil/client/connection/net_http_spec.rb +0 -38
- data/spec/unit/evil/client/dsl/files_spec.rb +0 -37
- data/spec/unit/evil/client/dsl/operation_spec.rb +0 -374
- data/spec/unit/evil/client/dsl/operations_spec.rb +0 -29
- data/spec/unit/evil/client/dsl/scope_spec.rb +0 -32
- data/spec/unit/evil/client/dsl/security_spec.rb +0 -135
- data/spec/unit/evil/client/middleware/merge_security_spec.rb +0 -32
- data/spec/unit/evil/client/middleware/normalize_headers_spec.rb +0 -17
- data/spec/unit/evil/client/middleware/stringify_form_spec.rb +0 -63
- data/spec/unit/evil/client/middleware/stringify_json_spec.rb +0 -61
- data/spec/unit/evil/client/middleware/stringify_multipart/part_spec.rb +0 -59
- data/spec/unit/evil/client/middleware/stringify_multipart_spec.rb +0 -62
- data/spec/unit/evil/client/middleware/stringify_query_spec.rb +0 -40
- data/spec/unit/evil/client/middleware_spec.rb +0 -46
- data/spec/unit/evil/client/operation/request_spec.rb +0 -49
- data/spec/unit/evil/client/operation/response_spec.rb +0 -63
@@ -1,48 +0,0 @@
|
|
1
|
-
RSpec.describe "scoping" do
|
2
|
-
# see Test::Client definition in `/spec/support/test_client.rb`
|
3
|
-
before do
|
4
|
-
class Test::Client < Evil::Client
|
5
|
-
operation :update_user do
|
6
|
-
http_method :put
|
7
|
-
path { |id:, **| "users/#{id}" }
|
8
|
-
|
9
|
-
query do
|
10
|
-
attribute :token
|
11
|
-
end
|
12
|
-
|
13
|
-
body format: :form do
|
14
|
-
attribute :name
|
15
|
-
end
|
16
|
-
|
17
|
-
response :success, 200
|
18
|
-
end
|
19
|
-
|
20
|
-
scope do
|
21
|
-
param :token
|
22
|
-
|
23
|
-
scope :users do
|
24
|
-
scope do
|
25
|
-
param :id
|
26
|
-
|
27
|
-
def update(name:)
|
28
|
-
operations[:update_user].call(id: id, token: token, name: name)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
stub_request(:put, //)
|
36
|
-
end
|
37
|
-
|
38
|
-
let(:path) { "https://foo.example.com/api/v3/users/7?token=qux" }
|
39
|
-
let(:client) do
|
40
|
-
Test::Client.new "foo", version: 3, user: "bar"
|
41
|
-
end
|
42
|
-
|
43
|
-
it "provides access to params over nested scopes" do
|
44
|
-
client["qux"].users[7].update name: "baz"
|
45
|
-
|
46
|
-
expect(a_request(:put, path).with(body: "name=baz")).to have_been_made
|
47
|
-
end
|
48
|
-
end
|
data/spec/support/test_client.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
module Test
|
2
|
-
class Client < Evil::Client
|
3
|
-
settings do
|
4
|
-
param :subdomain, type: Dry::Types["strict.string"]
|
5
|
-
option :version, type: Dry::Types["coercible.int"], default: proc { 1 }
|
6
|
-
option :user, type: Dry::Types["strict.string"]
|
7
|
-
option :password, type: Dry::Types["coercible.string"], optional: true
|
8
|
-
option :token, type: Dry::Types["coercible.string"], optional: true
|
9
|
-
end
|
10
|
-
|
11
|
-
base_url do |settings|
|
12
|
-
"https://#{settings.subdomain}.example.com/api/v#{settings.version}/"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require "evil/client/connection/net_http"
|
2
|
-
|
3
|
-
describe Evil::Client::Connection::NetHTTP do
|
4
|
-
let(:uri) { URI("https://example.com/foo/") }
|
5
|
-
let(:connection) { described_class.new(uri) }
|
6
|
-
let(:env) do
|
7
|
-
{
|
8
|
-
http_method: "post",
|
9
|
-
path: "bar/baz",
|
10
|
-
headers: { "Content-Type": "text/plain", "Accept": "text/plain" },
|
11
|
-
body_string: "Check!",
|
12
|
-
query_string: "status=new"
|
13
|
-
}
|
14
|
-
end
|
15
|
-
|
16
|
-
before do
|
17
|
-
stub_request(:post, "https://example.com/foo/bar/baz?status=new")
|
18
|
-
.to_return status: 201,
|
19
|
-
body: "Success!",
|
20
|
-
headers: { "Content-Type" => "text/plain; charset: utf-8" }
|
21
|
-
end
|
22
|
-
|
23
|
-
subject { connection.call env }
|
24
|
-
|
25
|
-
it "sends a request" do
|
26
|
-
subject
|
27
|
-
expect(a_request(:post, "https://example.com/foo/bar/baz?status=new"))
|
28
|
-
.to have_been_made
|
29
|
-
end
|
30
|
-
|
31
|
-
it "returns rack-compatible response" do
|
32
|
-
expect(subject).to eq [
|
33
|
-
201,
|
34
|
-
{ "content-type" => ["text/plain; charset: utf-8"] },
|
35
|
-
["Success!"]
|
36
|
-
]
|
37
|
-
end
|
38
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
RSpec.describe Evil::Client::DSL::Files do
|
2
|
-
subject do
|
3
|
-
described_class.new(&block).call file: "Hi!",
|
4
|
-
charset: "utf-16",
|
5
|
-
type: "application/json",
|
6
|
-
filename: "greetings.json",
|
7
|
-
text: "Hoorah!"
|
8
|
-
end
|
9
|
-
|
10
|
-
context "from block without definitions:" do
|
11
|
-
let(:block) { proc {} }
|
12
|
-
|
13
|
-
it "provides an empty schema" do
|
14
|
-
expect(subject).to eq([])
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context "from block with definitions:" do
|
19
|
-
let(:block) do
|
20
|
-
proc do |file:, **opts|
|
21
|
-
add file, **opts
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
it "provides a schema" do
|
26
|
-
expect(subject).to be_a Array
|
27
|
-
expect(subject.count).to eq 1
|
28
|
-
|
29
|
-
item = subject.first
|
30
|
-
expect(item[:file]).to be_a StringIO
|
31
|
-
expect(item[:file].read).to eq "Hi!"
|
32
|
-
expect(item[:type]).to eq MIME::Types["application/json"].first
|
33
|
-
expect(item[:charset]).to eq "utf-16"
|
34
|
-
expect(item[:filename]).to eq "greetings.json"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,374 +0,0 @@
|
|
1
|
-
RSpec.describe Evil::Client::DSL::Operation do
|
2
|
-
let(:operation) { described_class.new(:some_operation, block) }
|
3
|
-
let(:settings) { double version: 3, user: "foo", password: "bar" }
|
4
|
-
|
5
|
-
subject { operation.finalize(settings) }
|
6
|
-
|
7
|
-
context "without definitions" do
|
8
|
-
let(:block) { proc {} }
|
9
|
-
it "returns a hash" do
|
10
|
-
expect(subject).to eq key: :some_operation, responses: {}
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context "with #documentation" do
|
15
|
-
let(:block) do
|
16
|
-
proc { |settings| documentation "https://foo.bar/v#{settings.version}" }
|
17
|
-
end
|
18
|
-
|
19
|
-
it "defines link to :doc" do
|
20
|
-
expect(subject[:doc]).to eq "https://foo.bar/v3"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context "with #http_method" do
|
25
|
-
let(:block) do
|
26
|
-
proc { |settings| http_method settings.version > 2 ? "post" : "get" }
|
27
|
-
end
|
28
|
-
|
29
|
-
it "defines :method" do
|
30
|
-
expect(subject[:method].call).to eq "post"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context "with #path" do
|
35
|
-
let(:block) do
|
36
|
-
proc do |settings|
|
37
|
-
path { |id:, **| "/v#{settings.version}/users/#{id}/" }
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
it "defines :path without trailing slashes" do
|
42
|
-
path = subject[:path]
|
43
|
-
expect(path).to be_kind_of Proc
|
44
|
-
expect(path.call(id: 55)).to eq "v3/users/55"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "with #files" do
|
49
|
-
let(:block) do
|
50
|
-
proc do
|
51
|
-
files do |file:, **options|
|
52
|
-
add file, **options
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it "sets format to file" do
|
58
|
-
expect(subject[:format]).to eq "multipart"
|
59
|
-
end
|
60
|
-
|
61
|
-
it "defines schema for a file" do
|
62
|
-
file = Tempfile.new
|
63
|
-
|
64
|
-
schema = subject[:files].call file: file,
|
65
|
-
type: "text/html",
|
66
|
-
charset: "utf-16"
|
67
|
-
|
68
|
-
expect(schema).to contain_exactly \
|
69
|
-
file: file,
|
70
|
-
type: MIME::Types["text/html"].first,
|
71
|
-
charset: "utf-16",
|
72
|
-
filename: nil
|
73
|
-
end
|
74
|
-
|
75
|
-
it "wraps string to StringIO" do
|
76
|
-
schema = subject[:files].call file: "Hello!"
|
77
|
-
file = schema.first[:file]
|
78
|
-
|
79
|
-
expect(file).to be_kind_of StringIO
|
80
|
-
expect(file.read).to eq "Hello!"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
context "with #security" do
|
85
|
-
let(:block) do
|
86
|
-
proc do |settings|
|
87
|
-
security do
|
88
|
-
basic_auth settings.user, settings.password
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
it "defines :security schema" do
|
94
|
-
expect(subject[:security].call)
|
95
|
-
.to eq headers: { "authorization" => "Basic Zm9vOmJhcg==" }
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
context "with #body" do
|
100
|
-
context "with block without :model" do
|
101
|
-
let(:block) do
|
102
|
-
proc do |_|
|
103
|
-
body do
|
104
|
-
attribute :foo
|
105
|
-
attribute :bar
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
it "sets format to json" do
|
111
|
-
expect(subject[:format]).to eq "json"
|
112
|
-
end
|
113
|
-
|
114
|
-
it "defines :block as model filter" do
|
115
|
-
model = subject[:body][foo: :FOO, bar: :BAR, baz: :BAZ]
|
116
|
-
expect(model).to eq foo: :FOO, bar: :BAR
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context "with :model without block" do
|
121
|
-
before do
|
122
|
-
class Test::Foo < Evil::Struct
|
123
|
-
attribute :qux
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
let(:block) do
|
128
|
-
proc do |_|
|
129
|
-
body model: Test::Foo
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
it "defines :block as model filter" do
|
134
|
-
model = subject[:body][foo: :FOO, bar: :BAR, qux: :QUX, baz: :BAZ]
|
135
|
-
expect(model).to eq qux: :QUX
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
context "with :model and block" do
|
140
|
-
before do
|
141
|
-
class Test::Foo < Evil::Struct
|
142
|
-
attribute :qux
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
let(:block) do
|
147
|
-
proc do |_|
|
148
|
-
body model: Test::Foo do
|
149
|
-
attribute :foo
|
150
|
-
attribute :bar
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
it "defines :block as model filter" do
|
156
|
-
model = subject[:body][foo: :FOO, bar: :BAR, qux: :QUX, baz: :BAZ]
|
157
|
-
expect(model).to eq foo: :FOO, bar: :BAR, qux: :QUX
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
context "with #query" do
|
163
|
-
before do
|
164
|
-
class Test::Foo < Evil::Struct
|
165
|
-
attribute :qux
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
let(:block) do
|
170
|
-
proc do |settings|
|
171
|
-
query model: Test::Foo do
|
172
|
-
attribute settings.user
|
173
|
-
attribute :bar
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
it "defines :block as model filter" do
|
179
|
-
model = subject[:query][foo: :FOO, bar: :BAR, qux: :QUX, baz: :BAZ]
|
180
|
-
expect(model).to eq foo: :FOO, bar: :BAR, qux: :QUX
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
context "with #headers" do
|
185
|
-
before do
|
186
|
-
class Test::Foo < Evil::Struct
|
187
|
-
attribute :qux
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
let(:block) do
|
192
|
-
proc do |settings|
|
193
|
-
headers model: Test::Foo do
|
194
|
-
attribute settings.user
|
195
|
-
attribute :bar
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
it "defines :block as model filter" do
|
201
|
-
model = subject[:headers][foo: :FOO, bar: :BAR, qux: :QUX, baz: :BAZ]
|
202
|
-
expect(model).to eq foo: :FOO, bar: :BAR, qux: :QUX
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
context "with #response" do
|
207
|
-
let(:response_schema) { subject[:responses][:success] }
|
208
|
-
let(:response_raise) { response_schema[:raise] }
|
209
|
-
let(:response_coercer) { response_schema[:coercer] }
|
210
|
-
|
211
|
-
context "with plain format" do
|
212
|
-
let(:body) { "foo" }
|
213
|
-
let(:block) { proc { |_| response :success, 200, format: :plain } }
|
214
|
-
|
215
|
-
it "works" do
|
216
|
-
expect(response_coercer.call(body)).to eq "foo"
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
context "with plain format and handler" do
|
221
|
-
let(:body) { "foo" }
|
222
|
-
let(:block) do
|
223
|
-
proc do |_|
|
224
|
-
response :success, 200, format: :plain do |body|
|
225
|
-
body.to_sym
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
it "applies coercer" do
|
231
|
-
expect(response_coercer.call(body)).to eq :foo
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
context "with plain format and type" do
|
236
|
-
let(:body) { "0" }
|
237
|
-
let(:block) do
|
238
|
-
proc do |_|
|
239
|
-
response :success,
|
240
|
-
200,
|
241
|
-
format: :plain,
|
242
|
-
model: Dry::Types["coercible.int"]
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
it "uses type" do
|
247
|
-
expect(response_coercer.call(body)).to eq 0
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
context "with plain format, coercer and type" do
|
252
|
-
let(:body) { "0" }
|
253
|
-
let(:block) do
|
254
|
-
proc do |_|
|
255
|
-
response :success,
|
256
|
-
200,
|
257
|
-
format: :plain,
|
258
|
-
model: Dry::Types["coercible.string"] { |val| val.to_i + 1 }
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
it "applies coercer and then type" do
|
263
|
-
expect(response_coercer.call(body)).to eq "1"
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
context "with json format" do
|
268
|
-
let(:body) { '{"foo":1,"baz":"qux"}' }
|
269
|
-
let(:block) { proc { |_| response :success, 200, format: :json } }
|
270
|
-
|
271
|
-
it "returns parsed body" do
|
272
|
-
expect(response_coercer.call(body)).to eq "foo" => 1, "baz" => "qux"
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
context "with json format and handler" do
|
277
|
-
let(:body) { '{"foo":1,"baz":"qux"}' }
|
278
|
-
let(:block) do
|
279
|
-
proc do |_|
|
280
|
-
response :success, 200, format: :json do |body|
|
281
|
-
body.keys
|
282
|
-
end
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
it "returns parsed and handled body" do
|
287
|
-
expect(response_coercer.call(body)).to eq data: %w[foo baz]
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
context "with json format and type" do
|
292
|
-
before do
|
293
|
-
class Test::Foo < Evil::Struct
|
294
|
-
attribute :foo
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
let(:body) { '{"foo":1,"baz":"qux"}' }
|
299
|
-
let(:block) do
|
300
|
-
proc do |_|
|
301
|
-
response :success, 200, format: :json, model: Test::Foo
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
|
-
it "returns parsed and filtered body" do
|
306
|
-
expect(response_coercer.call(body)).to eq foo: 1
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
context "with json format, type and handler" do
|
311
|
-
before do
|
312
|
-
class Test::Foo < Evil::Struct
|
313
|
-
attribute :foo
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
|
-
let(:body) { '{"foo":1,"baz":"qux"}' }
|
318
|
-
let(:block) do
|
319
|
-
proc do |_|
|
320
|
-
response :success, 200, format: :json, model: Test::Foo do |body|
|
321
|
-
body["foo"] = body["foo"].to_s
|
322
|
-
body
|
323
|
-
end
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
it "returns parsed, handled and filtered body" do
|
328
|
-
expect(response_coercer.call(body)).to eq Test::Foo.new(foo: "1")
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
context "with json format and filter" do
|
333
|
-
before do
|
334
|
-
class Test::Foo < Evil::Struct
|
335
|
-
attribute :foo
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
let(:body) { '{"foo":1,"baz":"qux"}' }
|
340
|
-
let(:block) do
|
341
|
-
proc do |_|
|
342
|
-
response :success, 200, format: :json do
|
343
|
-
attribute :baz
|
344
|
-
end
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
it "returns parsed and filtered body" do
|
349
|
-
expect(response_coercer.call(body)).to eq baz: "qux"
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
context "with json format, type and filter" do
|
354
|
-
before do
|
355
|
-
class Test::Foo < Evil::Struct
|
356
|
-
attribute :foo
|
357
|
-
end
|
358
|
-
end
|
359
|
-
|
360
|
-
let(:body) { '{"foo":1,"baz":2}' }
|
361
|
-
let(:block) do
|
362
|
-
proc do |_|
|
363
|
-
response :success, 200, format: :json, model: Test::Foo do
|
364
|
-
attribute :baz, Dry::Types["coercible.string"]
|
365
|
-
end
|
366
|
-
end
|
367
|
-
end
|
368
|
-
|
369
|
-
it "returns parsed and filtered body" do
|
370
|
-
expect(response_coercer.call(body)).to eq foo: 1, baz: "2"
|
371
|
-
end
|
372
|
-
end
|
373
|
-
end
|
374
|
-
end
|