简体   繁体   中英

testing REST API . session issue

I am facing problem in testing REST API. I tried to authenticate like following.

 response = RestClient::Request.new(
    :method => :get,
    :url => @my_url,
    :user => @my_user,
    :password => @my_pass,
    :headers => { :accept => :json,
    :content_type => :json
    }
  ).execute

Also I tried https://username:password@www.xyz.com/api/1/contacts/list

Server side code :

def self.list(usr, id = false)
        data = { list: [], breadcrums: [] }
        current = usr.documents.find(id) if id
        query = if !id;
            usr.documents.where(node_tree: []).only(:file_name, :mime_type, :updated_at, :id, :oauth_provider_id, :tags)
        else
            folder_id = Document.get_oauth_id(folder)
            usr.documents.where(parent_id: folder_id).only(:file_name, :mime_type, :updated_at, :id, :oauth_provider_id, :tags)
        end
        result = query.sort_by{|doc| doc.file_name.downcase}.to_a
        result.each { |v| data[:list] << v.attributes.merge({ provider: v.oauth_provider.oauth_key, id: v.id.to_s}) }
        data[:breadcrumbs] = Document.breadcrumbs(current, usr) if id
        data
    end

But Problem is, When I try to authenticate from code. Internal server error thrown (500) Problem might be with, they taking information to process from session. How Can I do ? If you didn't understand please let me know. I will try to elaborate. How to resolve?

Use a mocking library such as webmock , to abstract away external services when testing:

stub_request(:get, "user:pass@www.example.com")

Net::HTTP.start('www.example.com') {|http|
  req = Net::HTTP::Get.new('/')
  req.basic_auth 'user', 'pass'
  http.request(req)
}    # ===> Success

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM