Sha256: f2375f0358691e88d8cc45a373812dde7b91fe6e00de61862a88e37404cdc9f9

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 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

3 entries across 3 versions & 1 rubygems

Version Path
sinatra_resource-0.4.24 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.23 examples/datacatalog/test/helpers/lib/request_helpers.rb
sinatra_resource-0.4.22 examples/datacatalog/test/helpers/lib/request_helpers.rb