lib/committee/rails/test/methods.rb in committee-rails-0.1.1 vs lib/committee/rails/test/methods.rb in committee-rails-0.2.0

- old
+ new

@@ -1,27 +1,54 @@ module Committee::Rails module Test module Methods include Committee::Test::Methods - def schema_path - Rails.root.join('docs', 'schema', 'schema.json') + def committee_schema + @committee_schema ||= begin + driver = Committee::Drivers::HyperSchema.new + schema_hash = JSON.parse(File.read(Rails.root.join('docs', 'schema', 'schema.json'))) + driver.parse(schema_hash) + end end def assert_schema_conform - if (data = schema_contents).is_a?(String) - warn_string_deprecated - data = JSON.parse(data) - end + @committee_schema ||= begin + # The preferred option. The user has already parsed a schema elsewhere + # and we therefore don't have to worry about any performance + # implications of having to do it for every single test suite. + if committee_schema + committee_schema + else + schema = schema_contents - @schema ||= begin - schema = JsonSchema.parse!(data) - schema.expand_references! - schema + if schema.is_a?(String) + warn_string_deprecated + elsif schema.is_a?(Hash) + warn_hash_deprecated + end + + if schema.is_a?(String) + schema = JSON.parse(schema) + end + + if schema.is_a?(Hash) || schema.is_a?(JsonSchema::Schema) + driver = Committee::Drivers::HyperSchema.new + + # The driver itself has its own special cases to be able to parse + # either a hash or JsonSchema::Schema object. + schema = driver.parse(schema) + end + + schema + end end - @router ||= Committee::Router.new(@schema, prefix: schema_url_prefix) - unless link = @router.find_request_link(request) + @committee_router ||= Committee::Router.new(@committee_schema, + prefix: schema_url_prefix) + + link, _ = @committee_router.find_request_link(request) + unless link invalid_response = "`#{request.request_method} #{request.path_info}` undefined in schema." raise Committee::InvalidResponse.new(invalid_response) end if validate_response?(response.response_code)