gpt4 book ai didi

ruby-on-rails - 使用基本身份验证集成测试 Rails API

转载 作者:数据小太阳 更新时间:2023-10-29 07:44:36 25 4
gpt4 key购买 nike

我正在尝试使用基本身份验证进行测试登录。我尝试了几种方法。有关失败尝试和代码的列表,请参见下面的代码。有什么明显的我做错了吗?谢谢

class ClientApiTest < ActionController::IntegrationTest
fixtures :all

test "adding an entry" do

# No access to @request
#@request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("someone@somemail.com:qwerty123")

# Not sure why this didn't work
#session["warden.user.user.key"] = [User, 1]

# This didn't work either
# url = URI.parse("http://localhost.co.uk/diary/people/1/entries.xml")
# req = Net::HTTP::Post.new(url.path)
# req.basic_auth 'someone@somemail.com', 'qwerty123'

post "/diary/people/1/entries.xml", {:diary_entry => {
:drink_product_id => 4,
:drink_amount_id => 1,
:quantity => 3
},
}
puts response.body
assert_response 200
end
end

最佳答案

看起来您可能正在运行 rails3——Rails3 切换到 Rack::test,因此语法不同。您传入一个环境哈希来设置您的请求变量,例如 header 。

尝试这样的事情:

path = "/diary/people/1/entries.xml"
params = {:diary_entry => {
:drink_product_id => 4,
:drink_amount_id => 1,
:quantity => 3}

env=Hash.new
env["CONTENT_TYPE"] = "application/json"
env["ACCEPT"] = "application/json"
env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("someone@somemail.com:qwerty123")

get(end_point, params, env)

这也可以,但它可能只是 sinatra 的东西:

get '/protected', {}, {'HTTP_AUTHORIZATION' => encode_credentials('go', 'away')}

Sinatra test credit

关于ruby-on-rails - 使用基本身份验证集成测试 Rails API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3466975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com