Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Get data from API in JSON format with "get" #230

Closed
rplaurindo opened this Issue · 1 comment

2 participants

@rplaurindo

module ConsumeApi
def api_get path
RestClient.get "#{host}/#{path}"
end

private
def host
INTRANET_CONFIG['urls']['personnel']
end
end

class Person < ActiveRecord::Base
def extract_person(params)
resp_json = JSON.parse(api_get "api/people/#{params}.json")
resp_json['person']
end
end

describe 'Group create.' do
it 'Should create a group' do

stub_http_request(:get, 'http://[url]/api/people/1.json').with(:headers => {'Accept' => ['application/json'], 'User-Agent'=>'Ruby'}).to_return(:status => 200, :body => '', :headers => {'Accept' => ['application/json']})
person = Person.extract_person 1

end
end

Output

Failures:

1) Group create. Should create a group
Failure/Error: person = Person.extract_person 1
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET http://hpersonnel.seguranca.rj.gov.br/api/people/1.json with headers {'Accept'=>'/; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}

   You can stub this request with the following snippet:

   stub_request(:get, "http://hpersonnel.seguranca.rj.gov.br/api/people/1.json").
     with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => "", :headers => {})

   registered request stubs:

   stub_request(:get, "http://hpersonnel.seguranca.rj.gov.br/api/people/1.json").
     with(:headers => {'Accept'=>'application/json'})

   ============================================================
 # ./lib/extensions/consume_api.rb:3:in `api_get'
 # ./app/models/person.rb:23:in `extract_person'
 # ./spec/requests/group/new_spec.rb:13:in `block (2 levels) in <top (required)>'

Finished in 0.0872 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/group/new_spec.rb:4 # Group create. Should create a group

An important observation to be made is the fate of the host be external

@bblimke
Owner

@rplaurindo I'm sorry I haven't replied to this one. The issue is that the Accept header is different in the stub than in the actual request.

@bblimke bblimke closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.