如何从此响应中获取状态?

发布于 2025-01-06 04:41:55 字数 1587 浏览 1 评论 0原文

我正在尝试对某些 gem 编写简单的测试: https://github.com/regedarek/soup-client

当我在控制台中运行时,我收到此响应:

    ruby-1.9.2-p290 :022 > c.login
    I, [2012-02-15T23:12:09.681772 #12908] INFO -- : post https://www.soup.io/login
    D, [2012-02-15T23:12:09.681854 #12908] DEBUG -- request: Content-Type: "application/x-www-form-urlencoded"
    I, [2012-02-15T23:12:10.157908 #12908] INFO -- Status: 302
    D, [2012-02-15T23:12:10.158054 #12908] DEBUG -- response: server: "nginx/0.7.67"
    date: "Wed, 15 Feb 2012 22:12:11 GMT"
    content-type: "text/html; charset=utf-8"
    connection: "close"
    ...
    content-length: "94"
    status: "302"
    ...
    location: "http://soup-client.soup.io/?sessid=995f610405f8afe029b9038da6c22132"
    content-length: "0"
    status: "302"
    ...
    set-cookie: "soup_session_id=995f610405f8afe029b9038da6c22132; path=/; expires=Sun, 15 Feb 2032 22:12:13 GMT; HttpOnly"
    status: "200"
    content-length: "16024"

我想对此进行测试,但如何从该响应中获取状态?

我已经尝试了 c.login["status"].should == 200 但它不起作用:

ruby-1.9.2-p290 :025 >   c.login["status"]

...

status: "200"
vary: "Accept-Encoding"
content-length: "15700"
NoMethodError: undefined method `[]' for nil:NilClass
    from (irb):25
    from /home/rege/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

这是 c.login.inspect 的输出 -> https://gist.github.com/1842529

Im trying to write simple test to some gem: https://github.com/regedarek/soup-client

I have this response when I run in the console:

    ruby-1.9.2-p290 :022 > c.login
    I, [2012-02-15T23:12:09.681772 #12908] INFO -- : post https://www.soup.io/login
    D, [2012-02-15T23:12:09.681854 #12908] DEBUG -- request: Content-Type: "application/x-www-form-urlencoded"
    I, [2012-02-15T23:12:10.157908 #12908] INFO -- Status: 302
    D, [2012-02-15T23:12:10.158054 #12908] DEBUG -- response: server: "nginx/0.7.67"
    date: "Wed, 15 Feb 2012 22:12:11 GMT"
    content-type: "text/html; charset=utf-8"
    connection: "close"
    ...
    content-length: "94"
    status: "302"
    ...
    location: "http://soup-client.soup.io/?sessid=995f610405f8afe029b9038da6c22132"
    content-length: "0"
    status: "302"
    ...
    set-cookie: "soup_session_id=995f610405f8afe029b9038da6c22132; path=/; expires=Sun, 15 Feb 2032 22:12:13 GMT; HttpOnly"
    status: "200"
    content-length: "16024"

I would like to test this but how do I get the status from this response?

I already tried c.login["status"].should == 200 but it's not working:

ruby-1.9.2-p290 :025 >   c.login["status"]

...

status: "200"
vary: "Accept-Encoding"
content-length: "15700"
NoMethodError: undefined method `[]' for nil:NilClass
    from (irb):25
    from /home/rege/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

This is output of c.login.inspect -> https://gist.github.com/1842529

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2025-01-13 04:41:55

当您标记 rspec 时,我将为您提供 rspec 答案:

get '/login'
last_response.status.should == 200

As you tagged rspec, I'm going to give you the rspec answer:

get '/login'
last_response.status.should == 200
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文