Module: CurbFu::Request::Test::ClassMethods

Defined in:
lib/curb-fu/request/test.rb

Instance Method Summary collapse

Instance Method Details

#build_request_options(url) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/curb-fu/request/test.rb', line 71

def build_request_options(url)
  options = {}
  options[:headers] = (url.is_a?(String)) ? nil : url.delete(:headers)
  options[:url] = build_url(url)
  options[:username], options[:password] = get_auth(url)
  options[:interface] = get_interface(url)
  options
end

#delete(url, params = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/curb-fu/request/test.rb', line 42

def delete(url, params = {})
  request_options = build_request_options(url)
  params = hashify_params(params) if params.is_a?(String)
  respond(request_options[:interface], :delete, request_options[:url],
    params, request_options[:headers], request_options[:username], request_options[:password])
end

#get(url, params = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/curb-fu/request/test.rb', line 13

def get(url, params = {})
  request_options = build_request_options(url)
  params = hashify_params(params) if params.is_a?(String)
  respond(request_options[:interface], :get, request_options[:url],
    params, request_options[:headers], request_options[:username], request_options[:password])
end

#get_auth(url) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/curb-fu/request/test.rb', line 131

def get_auth(url)
  username = password = nil
  if url.is_a?(Hash) && url[:username]
    username = url[:username]
    password = url[:password]
  end
  [username, password]
end

#get_interface(url) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/curb-fu/request/test.rb', line 110

def get_interface(url)
  if url.is_a?(Hash)
    host = url[:host] || parse_hostname(url[:url])
  else
    host = parse_hostname(url)
  end
  match_host(host)
end

#hashify_params(param_string) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/curb-fu/request/test.rb', line 49

def hashify_params(param_string)
  param_string.sub!(/^\?/,'')
  param_string.split('&').inject({}) do |hsh, pair|
    key, value = pair.split('=')
    
    if key.match(/(.+)\[\]$/)
      key = $1
      hsh[key] ||= []
      hsh[key] << value
    elsif key.match(/([^\[]+)\[(.+)\]$/)
      key = $1
      subkey = $2
      hsh[key] ||= {}
      hsh[key].update( subkey => value )
    else
      hsh[key] = value
    end

    hsh
  end
end

#match_host(host) ⇒ Object



124
125
126
127
128
129
# File 'lib/curb-fu/request/test.rb', line 124

def match_host(host)
  match = CurbFu.stubs.find do |(hostname, interface)|
    hostname == host
  end
  match.last unless match.nil?
end

#parse_hostname(uri) ⇒ Object



119
120
121
122
# File 'lib/curb-fu/request/test.rb', line 119

def parse_hostname(uri)
  parsed_hostname = URI.parse(uri)
  parsed_hostname.host || parsed_hostname.path
end

#post(url, params = {}) ⇒ Object



20
21
22
23
24
# File 'lib/curb-fu/request/test.rb', line 20

def post(url, params = {})
  request_options = build_request_options(url)
  respond(request_options[:interface], :post, request_options[:url],
    params, request_options[:headers], request_options[:username], request_options[:password])
end

#post_file(url, params = {}, filez = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/curb-fu/request/test.rb', line 26

def post_file(url, params = {}, filez = {})
  request_options = build_request_options(url)
  uploaded_files = filez.inject({}) do |hsh, (name, path)|
    hsh[name] = Rack::Test::UploadedFile.new(path)
    hsh
  end
  respond(request_options[:interface], :post, request_options[:url], 
    params.merge(uploaded_files), request_options[:headers], request_options[:username], request_options[:password])
end

#process_headers(headers) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/curb-fu/request/test.rb', line 101

def process_headers(headers)
  headers.inject({}) do |accum, (header_name, value)|
    key = header_name.gsub("-", "_").upcase
    key = "HTTP_" + key unless key =~ /^HTTP_/
    accum[key] = value
    accum
  end
end

#put(url, params = {}) ⇒ Object



36
37
38
39
40
# File 'lib/curb-fu/request/test.rb', line 36

def put(url, params = {})
  request_options = build_request_options(url)
  respond(request_options[:interface], :put, request_options[:url],
    params, request_options[:headers], request_options[:username], request_options[:password])
end

#respond(interface, operation, url, params, headers, username = nil, password = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/curb-fu/request/test.rb', line 80

def respond(interface, operation, url, params, headers, username = nil, password = nil)
  if interface.nil?
    raise Curl::Err::ConnectionFailedError
  else
    unless headers.nil?
      process_headers(headers).each do |name, value|
        interface.header(name, value)
      end
    end
    interface.authorize(username, password) unless username.nil?
    puts "sending #{operation} to #{url} with params #{params.inspect} using interface #{interface.inspect}" if CurbFu.debug?
    begin
      response = interface.send(operation, url, params, 'SERVER_NAME' => interface.hostname)
    rescue => e
      puts "Caught error: #{e}, #{e.backtrace.join("\n")}" if CurbFu.debug?
      raise e
    end
    CurbFu::Response::Base.from_rack_response(response)
  end
end