我正在使用backbone.js和rails。我的控制器接受并返回JSON。我的所有测试都正常工作,直到我添加了设计认证。我不能让黄瓜保持认证。设计,黄瓜和水豚不在JSON请求上进行验证
我已经试过各种上提到:
- devise wiki
- here - 标准登录(进入页面,填写表格,单击按钮),设置cookie,并有过滤器之前,一个特殊的应用,做但后者在机架(无法得到这个甚至可以运行)
- 做上述的Cookie /使用的before_filter this set_cookie method
一切都无济于事。登录返回罚款(200),但是当我发送我的请求时,我得到一个401响应。
这里是我的控制器代码:
class ContactsController < ApplicationController
before_filter :authenticate_user!
### backbone
def index
render :json => current_user.person.contacts
end
def show
render :json => current_user.person.contacts.find(params[:id])
end
def create
contact = current_user.person.contacts.create! params
render :json => contact
end
def update
contact = current_user.person.contacts.find(params[:id])
contact.update_attributes! params
render :json => contact
end
def destroy
contact = current_user.person.contacts.find(params[:id])
contact.destroy
render :json => contact #TODO: change to render nothing
end
这里是一个黄瓜特点:
Scenario: View contacts
Given I am logged in as "[email protected]"
And I send and accept JSON
And the following contacts:
|name|
|name 1|
|name 2|
|name 3|
|name 4|
When I send a GET request for "/contacts"
Then the response should be "200"
And the JSON response should be an array with 4 "name" elements
And I should get the following contacts:
|name|
|name 1|
|name 2|
|name 3|
|name 4|
的JSON的方法是直接用机架。我怀疑这是我的问题所在,但我不知道如何克服它。
这里是黄瓜JSON步骤:
World(Rack::Test::Methods)
Given /^I send and accept JSON$/ do
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
end
When /^I send a GET request for "([^\"]*)"$/ do |path|
get path
end
When /^I send a POST request to "([^\"]*)" with the following:$/ do |path, table|
params = table.hashes.flatten[0].to_json
post path, params
end
When /^I send a PUT request to "([^\"]*)" with the following:$/ do |path, table|
params = table.hashes.flatten[0].to_json
put path, params
end
When /^I send a DELETE request to "([^\"]*)"$/ do |path|
delete path
end
Then /^the response should be "([^\"]*)"$/ do |status|
last_response.status.should == status.to_i
end
Then /^the JSON response should have 1 "([^\"]*)" element$/ do |name|
page = JSON.parse(last_response.body)
page[name].should be
end
Then /^the JSON response should be an array with (\d+) "([^\"]*)" elements$/ do |number_of_children, name|
page = JSON.parse(last_response.body)
page.map { |d| d[name] }.length.should == number_of_children.to_i
end