Sha256: e73b23f4ddfbd0978a1353dc26927d4539495672cd7763e503ff3effc8cfd191

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'capybara/spec/test_app'

# for capybara < 1.0
module Capybara
  class Server
    class Identify
      def call(env)
        if env["PATH_INFO"] == "/__identify__"
          #[200, {}, @app.object_id.to_s]
          [200, {}, [ @app.object_id.to_s] ]
        else
          @app.call(env)
        end
      end
    end
  end
end


class JsonTestApp < TestApp
  def invoke
    res = catch(:halt) { yield }

    res = [ MultiJson.encode(res => res) ] if Fixnum === res or String === res
    res = [ MultiJson.encode(res) ] if Hash === res
    if Array === res and Fixnum === res.first
      status(res.shift)
      body(res.pop)
      headers(*res)
    elsif res.respond_to? :each
      content_type :json
      body res
    end
  end

  %w[ get post put delete ].each do |method|
    __send__(method, '/') do
      'Hello world!'
    end

    # rack specifications
    # http://rack.rubyforge.org/doc/SPEC.html
    __send__(method, '/env') do
      envs = %w[ REQUEST_METHOD PATH_INFO QUERY_STRING CONTENT_TYPE CONTENT_LENGTH rack.url_scheme ]\
               .inject({}) do |hash, key|
        hash[key.downcase] = env[key]
        hash
      end
      headers = env.select {|key, value| key.start_with?('HTTP_') }.inject({}) do |hash, (key, value)|
        hash[key.sub('HTTP_', '')] = value
        hash
      end

      envs.merge('params' => params, 'headers' => headers, 'rack.input' => env['rack.input'].string)
    end
  end

  get '/errors/:status_code' do |status_code|
    status status_code.to_i
    { 'status_code' => status_code.to_i }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capybara-json-0.2.0 spec/support/json_test_app.rb