Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwaggerChecker falls back to a local load #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/apivore/swagger_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,29 @@ def load_swagger_doc!

def fetch_swagger!
session = ActionDispatch::Integration::Session.new(Rails.application)
body = nil
http_error = true

begin
session.get(swagger_path)
rescue
fail "Unable to perform GET request for swagger json: #{swagger_path} - #{$!}."
response_code = session.get(swagger_path)

if response_code == 200
http_error = false
body = session.response.body
end
rescue URI::InvalidURIError
# http_error is true
end
JSON.parse(session.response.body)

if http_error
begin
open(swagger_path) {|file| body = file.read }
rescue Errno::ENOENT
fail "Unable to perform GET request for swagger json: #{swagger_path} - #{$!}."
end
end

JSON.parse(body)
end

def validate_swagger!
Expand Down
25 changes: 25 additions & 0 deletions spec/swagger_checker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe Apivore::SwaggerChecker do
describe '#instance_of' do
let(:sample_file_path) { File.join(File.dirname(__FILE__), 'data', '01_sample2.0.json') }

it 'should be able to load the file via ActionDispatch::Integration' do
json = nil
open(sample_file_path) { |file| json = file.read }
response_double = double('response', body: json)
session_double = double('integration', response: response_double, get: 200)
allow(ActionDispatch::Integration::Session).to receive(:new).and_return(session_double)

expect { Apivore::SwaggerChecker.instance_for("/random/url.json") }.to_not raise_error
end

it 'should be able to load the swagger file locally' do
expect { Apivore::SwaggerChecker.instance_for(sample_file_path) }.to_not raise_error
end

it 'should throw an exception if it is unable to load the file' do
expect { Apivore::SwaggerChecker.instance_for('not_found.json') }.to raise_error(RuntimeError)
end
end
end