0

我正在运行 Ruby 1.9.3 和 Rails 3.1。我可以通过向自己发送 XML 有效负载来成功地手动测试我对 Washout gem 的使用。我正在尝试在 rails 测试中重新创建以下 bash 命令:

curl -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:soap_submit_contract" -d@test/fixtures/soap/success.xml http://localhost/soap/action

如您所见,它设置了一些头数据并发送文件中的数据test/fixtures/soap/success.xml

我看到的所有其他 POST 示例如下所示:

post :parse_pdf,
  :terminal_key => @terminal_key,
  :contract_pdf => load_pdf(pdf)

但在我的情况下,文件数据不是命名参数,这似乎不是设置标题信息。

如何以与 curl 命令完全相同的方式提交帖子?

我们的测试套件使用默认的 ActionController::TestCase,如下所示:

class SoapControllerTest < ActionController::TestCase
  fixtures :terminals

  test "soap_succeeds" do 
    # request.set_header_information ???
    post :action, # file data ???

    assert_match(/success/, @response.body)
  end

end
4

2 回答 2

0

在 Rails 5 中,基于Gist 并具有基于令牌的身份验证,我必须执行以下操作:

# auth_request_helper.rb
module AuthRequestHelper
  def http_login
    @env ||= {}
    User.create!(
      email: 'email', 
      password: 'pass', 
      password_confirmation: 'pass'
      )
     email = 'email'
     password = 'pass'
     # AuthenticateUser is a custom class which logs the user and returns a token
     @env['TOKEN'] = AuthenticateUser.call(user, pw).token
   end
end

然后,在规范文件上:

# users_spec.rb

# login to http basic auth
before(:each) do
  http_login
end

describe 'POST /users' do
  let(:valid_attributes) { 
    { 
      email: 'some_email', 
      password: 'some_pass',
      password_confirmation: 'some_pass',        
      format: :json # Also useful for specifying format
     } 
   }

  context 'when the request is valid' do
    before { post '/users', params: valid_attributes, headers: {'Authorization': @env['TOKEN']} }

    it 'returns status code 201' do
      expect(response).to have_http_status(201) # Success
    end
  end
end
于 2017-09-01T18:46:48.857 回答
0

在请求规范中,您可以将headersin 哈希作为请求方法的第三个参数传递,如下所示:

post :action, {}, {'YOUR_HEADER' => 'value'}

有关更多信息,请参阅文档

更新

要发布 XML 数据,请尝试以下方式:

xml_data = File.read('test/fixtures/soap/success.xml')
post :action, xml_data, { 'CONTENT_TYPE' => 'application/xml' }
于 2015-09-29T18:19:42.813 回答