test/client_test.rb
def mock_restclient_request(method, url, params_or_payload, client_route_name,
                              headers = { :accept => :json }, status = 200,
                              result = "{}", raise_exc = false)
    args = {
      :method => method,
      :url => url,
      :headers => {Olaf::Http::REQUEST_GUID_HEADER => FAKE_REQUEST_GUID}.merge(headers)
    }

    stub(UUIDTools::UUID).random_create { FAKE_REQUEST_GUID }

    if Olaf::ServiceClient::HTTP_VERBS_WITH_PAYLOAD.include?(method)
      args[:payload] = params_or_payload
    else
      args[:headers].merge!(:params => params_or_payload)
    end

    fake_response = OpenStruct.new :code => status, :headers => headers,
                                   :body => result
    mock(fake_response).return!.with_any_args
    mock(RestClient::Request).execute(args).yields(fake_response, nil, nil) do
      if raise_exc
        raise Olaf::Error.new( :http_response_code => 500, :message => "Failed!", :error_code => 1002, :request_guid => "7d6b0dd4-a83f-11e2-a237-000a27020050")
      end
      result
    end
  end
# TODO: we must be able to auto-generate client_route_name here...
we must be able to auto-generate client_route_name here
we must be able to auto - generate client_route_name here
def mock_restclient_request(method, url, params_or_payload, client_route_name,
                              headers = { :accept => :json }, status = 200,
                              result = "{}", raise_exc = false)
    args = {
      :method => method,
      :url => url,
      :headers => {Olaf::Http::REQUEST_GUID_HEADER => FAKE_REQUEST_GUID}.merge(headers)
    }
    stub(UUIDTools::UUID).random_create { FAKE_REQUEST_GUID }
    if Olaf::ServiceClient::HTTP_VERBS_WITH_PAYLOAD.include?(method)
      args[:payload] = params_or_payload
    else
      args[:headers].merge!(:params => params_or_payload)
    end
    fake_response = OpenStruct.new :code => status, :headers => headers,
                                   :body => result
    mock(fake_response).return!.with_any_args
    mock(RestClient::Request).execute(args).yields(fake_response, nil, nil) do
      if raise_exc
        raise Olaf::Error.new( :http_response_code => 500, :message => "Failed!", :error_code => 1002, :request_guid => "7d6b0dd4-a83f-11e2-a237-000a27020050")
      end
      result
    end
  end
def mock_restclient_request ( method , url , params_or_payload , client_route_name , headers = { :accept => :json } , status = 200 , result = "{}" , raise_exc = false ) args = { :method => method , :url => url , :headers => { Olaf :: Http :: REQUEST_GUID_HEADER => FAKE_REQUEST_GUID } . merge ( headers ) } stub ( UUIDTools :: UUID ) . random_create { FAKE_REQUEST_GUID } if Olaf :: ServiceClient :: HTTP_VERBS_WITH_PAYLOAD . include? ( method ) args [ :payload ] = params_or_payload else args [ :headers ] . merge! ( :params => params_or_payload ) end fake_response = OpenStruct . new :code => status , :headers => headers , :body => result mock ( fake_response ) . return! . with_any_args mock ( RestClient :: Request ) . execute ( args ) . yields ( fake_response , nil , nil ) do if raise_exc raise Olaf :: Error . new ( :http_response_code => 500 , :message => "Failed!" , :error_code => 1002 , :request_guid => "7d6b0dd4-a83f-11e2-a237-000a27020050" ) end result end end
TODO: we must be able to auto-generate client_route_name here...
TODO : we must be able to auto - generate client_route_name here ...
method
url
params_or_payload
client_route_name
headers
status
result
raise_exc