Sha256: 477fb663ed275394a432e0cf78231c973343420c416ae54af760eee5d5400831

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

module RequestHelpers

  def parsed_response_body
    s = last_response.body
    if s == ""
      nil
    else
      Crack::JSON.parse(s)
    end
  end
  
  def assert_properties(correct, parsed_document)
    correct.each do |property|
      assert_include property, parsed_document
    end
    assert_equal [], parsed_document.keys - correct
  end

  def self.included(includee)
    includee.extend(ClassMethods)
  end
  
  module ClassMethods
    def doc_properties(correct)
      test "document should only have correct attributes" do
        assert_properties(correct, parsed_response_body)
      end
    end

    def docs_properties(correct)
      test "documents should only have correct attributes" do
        parsed_response_body.each do |parsed|
          assert_properties(correct, parsed)
        end
      end
    end
    
    def invalid_param(s)
      test "should report #{s} as invalid" do
        assert_include s.to_s, parsed_response_body["errors"]["invalid_params"]
      end
    end
    
    def missing_param(s)
      test "should report missing #{s}" do
        assert_include "can't be empty", parsed_response_body["errors"][s.to_s]
      end
    end

    def location_header(path)
      test "should set Location header correctly" do
        base_uri = Config.environment_config["base_uri"]
        path = %(#{path}/#{parsed_response_body["id"]})
        expected = URI.join(base_uri, path).to_s
        assert_equal expected, last_response.headers['Location']
      end
    end
    
    def nested_location_header(parent_path, parent_ivar, child_path)
      test "should set Location header correctly" do
        base_uri = Config.environment_config["base_uri"]
        parent = instance_variable_get("@#{parent_ivar}")
        raise Error unless parent
        id = parsed_response_body['id']
        path = "#{parent_path}/#{parent.id}/#{child_path}/#{id}"
        expected = URI.join(base_uri, path).to_s
        assert_equal expected, last_response.headers['Location']
      end
    end
    

  end
  
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
sinatra_resource-0.4.21 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.20 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.19 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.18 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.17 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.16 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.15 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.14 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.13 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.12 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.11 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.10 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.9 examples/datacatalog/test/helpers/lib/request_helpers.rb