Skip to content

Commit

Permalink
Add client test
Browse files Browse the repository at this point in the history
  • Loading branch information
bezrukavyi committed Jan 18, 2018
1 parent 50b0880 commit cddccb5
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ Documentation:

FrozenStringLiteralComment:
Enabled: false

Style/FileName:
Enabled: false
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ GEM
byebug (9.1.0)
coderay (1.1.2)
concurrent-ruby (1.0.5)
crack (0.4.3)
safe_yaml (~> 1.0.0)
diff-lcs (1.3)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
Expand All @@ -27,6 +29,8 @@ GEM
dry-monads (0.3.1)
dry-core
dry-equalizer
ffaker (2.8.0)
hashdiff (0.3.7)
hashie (3.5.6)
http (3.0.0)
addressable (~> 2.3)
Expand Down Expand Up @@ -72,21 +76,30 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.9.0)
safe_yaml (1.0.4)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.4)
unicode-display_width (1.3.0)
vcr (3.0.3)
webmock (3.2.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff

PLATFORMS
ruby

DEPENDENCIES
api_struct!
bundler (~> 1.14)
ffaker (~> 2.7)
pry-byebug (~> 3.5, >= 3.5.1)
rake (~> 12.3)
rspec (~> 3.7)
rubocop (~> 0.52.0)
vcr (~> 3.0.3)
webmock (~> 3.2, >= 3.2.1)

BUNDLED WITH
1.16.0
3 changes: 3 additions & 0 deletions api_struct.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 12.3'
spec.add_development_dependency 'rspec', '~> 3.7'
spec.add_development_dependency 'rubocop', '~> 0.52.0'
spec.add_development_dependency 'vcr', '~> 3.0.3'
spec.add_development_dependency 'webmock', '~> 3.2', '>= 3.2.1'
spec.add_development_dependency 'ffaker', '~> 2.7'
end
55 changes: 55 additions & 0 deletions fixtures/vcr_cassettes/synopsis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion lib/api_struct/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module ApiStruct
class Client
DEFAULT_HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}

attr_reader :client

def self.method_missing(method_name, *args, &block)
Expand Down Expand Up @@ -29,7 +34,8 @@ def self.method_missing(method_name, *args, &block)

def initialize
api_settings_exist
@client = HTTP::Client.new(headers: headers)
client_headers = headers || DEFAULT_HEADERS
@client = HTTP::Client.new(headers: client_headers)
end

private
Expand Down
50 changes: 50 additions & 0 deletions spec/api_struct/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
describe ApiStruct::Client do
ApiStruct::Settings.configure do |config|
config.endpoints = {
stub_api: {
root: 'https://jsonplaceholder.typicode.com'
}
}
end

class StubClient < ApiStruct::Client
stub_api '/posts'

def show(id)
get("/#{id}")
end

def update(id, params)
patch("/#{id}", json: params)
end
end

context 'Get' do
it 'Success', type: :webmock do
VCR.use_cassette('posts/get_success') do
response = StubClient.new.show(1)
expect(response).to be_success
expect(response.value[:id]).to eq(1)
expect(response.value[:title]).not_to be_empty
end
end

it 'Failure', type: :webmock do
VCR.use_cassette('posts/get_failure') do
response = StubClient.new.show(101)
expect(response).to be_failure
expect(response.value.status).to eq(404)
end
end
end

context 'Patch' do
it 'Success', type: :webmock do
VCR.use_cassette('posts/update_success') do
response = StubClient.new.update(1, title: FFaker::Name.name)
expect(response).to be_success
expect(response.value[:id]).to eq(1)
end
end
end
end
67 changes: 67 additions & 0 deletions spec/cassettes/posts/get_failure.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cddccb5

Please sign in to comment.