-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails 5 / Rspec 3.5.0 -- setting request headers? #1655
Comments
Setting the header in |
Try: |
@ledbettj Could you please provide us with a rails app that we can clone that demonstrates the issue. Specifically it'd be great if
then, provide us with a description of how to clone your application and reproduce the issue. Thanks :) |
I've pushed a basic example here.
git clone https://github.com/ledbettj/rspec-repro.git repro
cd repro
bundle
bundle exec rake spec Output:
Let me know if anything else would help. |
request.env did not work for me. Any update on this? |
@richddr looks like nobody from the RSpec core team has had a chance to look at this yet. If you really need a fix to this, you're more than welcome to use the reproduction case above as a starting point for debugging and see if you can fix the problem :) |
I worked around this like so. Maybe it helps you, too. Not the prettiest solution but effective. let(:headers) { some: "header" }
# ...
request.headers.merge! headers
post :endpoint, params: params |
I tried to fix this issue but don't see anything wrong with the way it currently is. (I don't know if this is a regression) Just to explain as to why this is happening. Regarding the stubbed method. If you take a look at ActionController::TestCase::Behaviour you will notice that all HTTP methods ( Regarding the remaining two options - passing the headers as params. This also won't work. If you take a look at the above-mentioned class you can notice that only the This leaves you with the option to edit |
Sorry, forgot to post an update here. I also ended up working around it the same as @sebastian-julius did. |
You cannot stub @request = TestRequest.new scrub_env!(@request.env), @request.session, @controller.class So, the |
I expected this to work: get :create, headers: { Key: "SomeKey" } Ended up doing this, per @sebastian-julius's workaround: headers = { :Key => @api_key.token }
request.headers.merge! headers
get :create |
For me the issue is that |
I faced this issue today and I dug a little. It seems that for request specs, rspec-rails correctly leads us to
But for controller tests, it leads us to the old and now deprecated
I think this is a bug and the core team should take a look at it. |
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
…ller specs. Instead, I’m wrapping request.referrer usage in a method that can be stubbed out. See rspec/rspec-rails#1655
@KevinColemanInc between requests no, and why should it? It resets for each test, and each test should have one request. |
The only thing that worked for me. (rails: 5.1.4 & rspec-rails 3.7.2)
|
I ended up here trying to figure out how to mock the
|
+ Fixed expiring time in Doorkeeper-JWT Unfortunetely, RSpec has an issue and can not send HTTP header, so I haven't finished tests. Link to the github issue: rspec/rspec-rails#1655 Any solutions, provided by github users in issue comments hadn't helped me.
+ Fixed expiring time in Doorkeeper-JWT + implemented allow_simple_login feature Unfortunetely, RSpec has an issue and can not send HTTP header, so I haven't finished tests. Link to the github issue: rspec/rspec-rails#1655 Any solutions, provided by github users in issue comments hadn't helped me.
This comment has been minimized.
This comment has been minimized.
@andreymakovenko yup, it works in request specs. |
why so many downvotes on @andreymakovenko reply? |
@ArielAleksandrus this issue is about controller specs not request specs. |
for example: |
Hello, so... nothing works for me :p I've tried everything, I think I'm doing something wrong. I'm using rails 5.2.0 and rspec-rails 3.8.0 and I tried doing:
NOTHING WORKS, am I dumb? Here's the spec file:
Someone help me pleassse |
Dependant on what your |
Sorry, my bad, I'm using controller type now, but the main issue was something else, unrelated do Rspec. Everything works now! Thanks! |
Before you post your solution please make sure that you understand the difference between request specs and controller specs. This issue is about controller specs. |
@fnmendez works well (in controller specs), I recommend to check in controller if simple "puts" is the values are right placed; I spend 5 hours thinking that the solution not works, but, I was not the right values in models (fixtures) |
Using this works for controller/feature specs. The only thing you have to make sure is that the headers are on the second position of the args. That's why the empty hash. As @pawandubey its the different module request and controller specs are using. Unfortunatly when you use the snippet like top one rubocop will complain because |
For HTTP_REFERER use symbol and not string !
|
This solution worked for me too post "/sessions", {:session => {:email => user.email, :password => user.password}}, {"HTTPS" => "on", 'HTTP_REFERER' => '/signin'} |
For Rails 5.2.3 and Rspec 3.8 let(:valid_attributes) {
{to_wallet: "wallet", value: 100.00}
}
let(:valid_headers) { { HTTP_USERTOKEN: "Token12345678"} }
it "creates a new Transaction" do
request.headers.merge!(valid_headers) # This line right here!!!
expect {
post :create, params: {transaction: valid_attributes}
}.to change(Transaction, :count).by(1)
end |
Hello from 2019! Just want to confirm I came here trying to set headers on a controller test and that @sebastian-julius ' approach (though I slightly modified it) worked great for me! Thank you! :) |
Same! It was |
Are there any volunteers to update the doc and add feature test similar to the cookie one? |
Did'nt work |
This might work - on rspec-rails 3.8 works
|
Hi,
I had a spec working under Rails 4 / Rspec 3.4.4 that looked like this:
After upgrading, it looks like
request.referrer
is not stubbed correctly andrequest.referrer
in the controller returnsnil
.I tried rewriting like so:
But I wasn't able to get that header, or actually any header passed through to the controller.
Any suggestions?
The text was updated successfully, but these errors were encountered: