Sha256: 4ae2b0894e8f115cba4280e931ec23c342a1cbb5464a227127f29462c256e409

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Kookaburra
  # Pattern:
  # - Get some data from test_data.factory
  # - Post it to the API
  # - Remember the response in test_data
  class APIDriver
    include Rack::Test::Methods
    attr_reader :app, :test_data
    protected :app, :test_data

    def initialize(opts)
      @app       = opts.fetch(:app)
      @test_data = opts.fetch(:test_data)
    end

  protected

    def raise_unless_status(expected_status, short_description)
      message = "%s failed (#{last_response.status})\n#{last_response.body}" % short_description
      raise message unless last_response.status == expected_status
    end

    ##### JSON Tools #####

    def post_as_json(short_description, path, data = {}, options = {})
      header 'Content-Type', 'application/json'
      header 'Accept', 'application/json'
      post path, data.to_json
      raise_unless_status options[:expected_status] || 201, short_description
    end

    def put_as_json(short_description, path, data = {}, options = {})
      header 'Content-Type', 'application/json'
      header 'Accept', 'application/json'
      put path, data.to_json
      raise_unless_status options[:expected_status] || 201, short_description
    end

    def hash_from_response_json
      HashWithIndifferentAccess.new( JSON.parse(last_response.body) )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kookaburra-0.0.4 lib/kookaburra/api_driver.rb
kookaburra-0.0.3 lib/kookaburra/api_driver.rb
kookaburra-0.0.2 lib/kookaburra/api_driver.rb