Top Level Namespace

Defined Under Namespace

Modules: Curl, EventMachine, Net, Test, WebMock Classes: StringIO, StubSocket, WebMockHTTPClient

Constant Summary

RSPEC_NAMESPACE =
Spec
RSPEC_CONFIGURER =
Spec::Runner

Instance Method Summary collapse

Instance Method Details

#build_request_signature(req, reuse_existing = false) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 133

def build_request_signature(req, reuse_existing = false)
  uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
  uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query) if req.header.request_query
  uri.port = req.header.request_uri.port
  uri = uri.omit(:userinfo)

  auth = www_auth.basic_auth
  auth.challenge(req.header.request_uri, nil)

  @request_filter.each do |filter|
    filter.filter_request(req)
  end

  headers = req.header.all.inject({}) do |hdrs, header|
    hdrs[header[0]] ||= []
    hdrs[header[0]] << header[1]
    hdrs
  end

  if (auth_cred = auth.get(req)) && auth.scheme == 'Basic'
    userinfo = WebMock::Util::Headers.decode_userinfo_from_header(auth_cred)
    userinfo = WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)
    headers.reject! {|k,v| k =~ /[Aa]uthorization/ && v =~ /^Basic / } #we added it to url userinfo
    uri.userinfo = userinfo
  end

  signature = WebMock::RequestSignature.new(
    req.header.request_method.downcase.to_sym,
    uri.to_s,
    :body => req.content,
    :headers => headers
  )

  # reuse a previous identical signature object if we stored one for later use
  if reuse_existing && previous_signature = previous_signature_for(signature)
    return previous_signature
  end

  signature
end

#build_webmock_response(httpclient_response) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 108

def build_webmock_response(httpclient_response)
  webmock_response = WebMock::Response.new
  webmock_response.status = [httpclient_response.status, httpclient_response.reason]

  webmock_response.headers = {}.tap do |hash|
    httpclient_response.header.all.each do |(key, value)|
      if hash.has_key?(key)
        hash[key] = Array(hash[key]) + [value]
      else
        hash[key] = value
      end
    end
  end

  if  httpclient_response.content.respond_to?(:read)
    webmock_response.body = httpclient_response.content.read
    body = HTTP::Message::Body.new
    body.init_response(StringIO.new(webmock_response.body))
    httpclient_response.body = body
  else
    webmock_response.body = httpclient_response.content
  end
  webmock_response
end

#previous_signature_for(signature) ⇒ Object



184
185
186
187
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 184

def previous_signature_for(signature)
  return nil unless index = webmock_request_signatures.index(signature)
  webmock_request_signatures.delete_at(index)
end

#webmock_request_signaturesObject



180
181
182
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 180

def webmock_request_signatures
  @webmock_request_signatures ||= []
end

#webmock_responsesObject



174
175
176
177
178
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 174

def webmock_responses
  @webmock_responses ||= Hash.new do |hash, request_signature|
    hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
  end
end