spec/integration/specs/lib/server_spec.rb in agile-proxy-0.1.18 vs spec/integration/specs/lib/server_spec.rb in agile-proxy-0.1.19
- old
+ new
@@ -1,10 +1,11 @@
require 'integration_spec_helper'
# require 'spec_helper'
require 'agile_proxy'
require 'resolv'
+require 'rest_client'
shared_examples_for 'a proxy server' do |options = {}|
if options.key?(:recording) && options[:recording]
after :each do
expect(JSON.parse(recordings_resource.get)['total']).to eql(1)
@@ -30,19 +31,36 @@
expect(http.delete('/echo').body).to eql 'DELETE /echo'
end
end
shared_examples_for 'a request stub' do |options = {}|
+ recording = false
if options.key?(:recording) && options[:recording]
+ recording = true
after :each do
data = recordings_resource.get
count = JSON.parse(data)['total']
expect(count).to eql(1)
end
end
+ def find_stub(client, name)
+ @stubs_with_recordings.select { |stub| stub.url == client.url_prefix.to_s && stub.name == name}.first
+ end
+ def rest_client_for_stub(stub)
+ RestClient::Resource.new("http://localhost:#{api_port}/api/v1/users/1/applications/#{@recording_application_id}/request_specs/#{stub.body[:id]}/recordings", content_type: :json )
+ end
+ def recordings_for(name)
+ stub = find_stub(http, name)
+ JSON.parse(rest_client_for_stub(stub).get).with_indifferent_access
+ end
+ def recordings_matcher_for(name, path)
+ stub = find_stub(http, name)
+ {recordings: a_collection_containing_exactly(a_hash_including request_body: '', request_url: "#{http.url_prefix}#{path.gsub(/^\//, '')}", request_method: 'GET', request_spec_id: stub.body[:id])}
+ end
it 'should stub GET requests' do
expect(http.get('/index.html').body).to eql '<html><body>Mocked Content</body></html>'
+ expect(recordings_for 'index').to include(recordings_matcher_for('index', '/index.html')) if recording
end
it 'should stub GET response statuses' do
expect(http.get('/index.html').status).to eql 200
end
@@ -50,9 +68,10 @@
it 'Should stub a different get request with json response' do
resp = http.get('/api/forums')
expect(ActiveSupport::JSON.decode(resp.body).symbolize_keys).to eql forums: [], total: 0
expect(resp.status).to eql 200
expect(resp.headers['Content-Type']).to eql 'application/json'
+ expect(recordings_for 'api_forums').to include(recordings_matcher_for('api_forums', '/api/forums')) if recording
end
it 'Should get the mocked content with parameter substitution for the /api/forums/:forum_id/posts url' do
resp = http.get '/api/forums/my_forum/posts'
expect(ActiveSupport::JSON.decode(resp.body)).to eql 'posts' => [{ 'forum_id' => 'my_forum', 'subject' => 'My first post' }, { 'forum_id' => 'my_forum', 'subject' => 'My second post' }, { 'forum_id' => 'my_forum', 'subject' => 'My third post' }], 'total' => 3