English 中文(简体)
我如何在申请时使用特定URL来测试申请标的?
原标题:How can I test the request object using a specific URL in application_helper_spec.rb?

我在申请中界定了一种方法,即根据目前的要求,恢复一种通俗的URL。 我怎么能够模仿或以其他方式为控制者规定完整的URL?

# spec/helpers/application_helper_spec.rb
describe "#canonical_url" do
  it "should return a path to an asset that includes the asset_host" do
    # Given: "http://www.foo.com:80/asdf.asdf?asdf=asdf"
    helper.canonical_url().should eq("http://www.foo.com/asdf.asdf")
  end
end

# app/helpers/application_helper.rb
def canonical_url
  "#{request.protocol}#{request.host}#{(request.port == 80) ? "" : request.port_string}#{request.path}"
end

www.un.org/Depts/DGACM/index_spanish.htm

归根结底,我要测试的是,风能(url)(a) 回到了不同的URL公司的楼梯,有些是港口,有些是码头/o,有些是用电梯,有些有路。 也许这种高技能只是最终目标。 我愿对最初的URL表示明确的怀疑/ock/点击,然后明确把期望放在对口单位。 我很想能够用一个电话来做到这一点,即: Controller.request.url = http://www.foo.com:80/asdf.asdf?asdf=asdf request = ActionController:Request.new :url =>http://www.foo.com:80/asdf.asdf.asdf?asdf=asdf ,但迄今为止,我还没有找到一个允许我这样做的单一“hook”。 因此,我期待的解决办法。 www.un.org/Depts/DGACM/index_french.htm

最佳回答

我已这样做:

helper.request.stub(:protocol).and_return("http://")
helper.request.stub(:host).and_return("www.foo.com")
helper.request.stub(:port).and_return(80)
helper.request.stub(:port_string).and_return(":80")
helper.request.stub(:path).and_return("/asdf.asdf")
helper.canonical_url.should eq("http://www.foo.com/asdf.asdf")
问题回答

造成这种混淆的最终原因是:

  • ActionDispatch::TestRequest
  • ActionDispatch::Http::URL

e. 例如,如果你设立港口(行动:请求)

def port=(number)
  @env[ SERVER_PORT ] = number.to_i
end

e.g. 您读到(行动司:Http:URL)

def raw_host_with_port
  if forwarded = env["HTTP_X_FORWARDED_HOST"]
    forwarded.split(/,s?/).last
  else
    env[ HTTP_HOST ] || "#{env[ SERVER_NAME ] || env[ SERVER_ADDR ]}:#{env[ SERVER_PORT ]}"
  end
end

2. 设置服务网 只有当你没有SSERVER_NAME, HTTP_X_FORWARDED_HOST或HTTP_HOST已经建立时,才会生效。

我对港口环境的基本工作是把港口添加到东道国——因为要求。 港口没有做你通常想要的东西。

e. 设置港口

request.host =  example.com:1234 

真正的答案是将《行动计划》中的《守则》改为《行动纲领》中的《守则》。

该党很晚,但发现它类似。

如何:

allow_any_instance_of(ActionController::TestRequest).to receive(:host).and_return( www.fudge.com )

我欣赏allow_any_instance_of,但这项工作似乎已经完成。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签