oren
2010-Oct-21 02:56 UTC
[rspec-users] new syntax question and mocking HTTP_AUTHORIZATION
I am specing a sinatra app. it "should send non-valid user to /login" do get ''/'' last_response.headers[''Location''].should == ''/reports'' end this is working fine but I would like to know how to convert it to the new syntax or if there is a better way to do that. before(:each) { get ''/'' } subject { last_response } its(:status) {should == 302} require ''ap'' its(:headers) {should include(''/login'')} another question: it "should send valid user to /reports" do get ''/'', { ''HTTP_AUTHORIZATION'' => ''Basic c2cvbGFuOmFBITExMQ=='' } last_response.headers[''Location''].should == ''/reports'' end when i debug my method I see that request.env["HTTP_AUTHORIZATION"] is nil, so the header was not sent with it. how to mock it? Thanks
David Chelimsky
2010-Oct-21 03:19 UTC
[rspec-users] new syntax question and mocking HTTP_AUTHORIZATION
On Oct 20, 2010, at 9:56 PM, oren wrote:> I am specing a sinatra app. > > it "should send non-valid user to /login" do > get ''/'' > last_response.headers[''Location''].should == ''/reports'' > end > > this is working fine but I would like to know how to convert it to the > new syntax or if there is a better way to do that. > > before(:each) { get ''/'' } > subject { last_response } > > its(:status) {should == 302} > require ''ap'' > its(:headers) {should include(''/login'')}This is not "the new syntax." It is simply an alternate syntax that has been around for over a year and works well for a small subset of things you might want to specify like initial state: describe Array do context "when first created" do subject { Array.new } its(:length) { should eq(0) } end end It is not in any way an all-purpose replacement for more specifying _behaviour_.> another question: > > it "should send valid user to /reports" do > get ''/'', { ''HTTP_AUTHORIZATION'' => ''Basic > c2cvbGFuOmFBITExMQ=='' } > last_response.headers[''Location''].should == ''/reports'' > end > > when i debug my method I see that request.env["HTTP_AUTHORIZATION"] is > nil, > so the header was not sent with it. how to mock it?I don''t have any personal experience w/ Sinatra yet (I know, I know, but there are only so many hours in a day), so I''ll leave it to someone else to comment on how to approach the Sinatra questions. Cheers, David
Zach Dennis
2010-Oct-21 13:10 UTC
[rspec-users] new syntax question and mocking HTTP_AUTHORIZATION
On Wed, Oct 20, 2010 at 11:19 PM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Oct 20, 2010, at 9:56 PM, oren wrote: > > > I am specing a sinatra app. > > > > it "should send non-valid user to /login" do > > get ''/'' > > last_response.headers[''Location''].should == ''/reports'' > > end > > > > this is working fine but I would like to know how to convert it to the > > new syntax or if there is a better way to do that. > > > > before(:each) { get ''/'' } > > subject { last_response } > > > > its(:status) {should == 302} > > require ''ap'' > > its(:headers) {should include(''/login'')} > > This is not "the new syntax." It is simply an alternate syntax that has > been around for over a year and works well for a small subset of things you > might want to specify like initial state: > > describe Array do > context "when first created" do > subject { Array.new } > its(:length) { should eq(0) } > end > end > > It is not in any way an all-purpose replacement for more specifying > _behaviour_. > > > another question: > > > > it "should send valid user to /reports" do > > get ''/'', { ''HTTP_AUTHORIZATION'' => ''Basic > > c2cvbGFuOmFBITExMQ=='' } > > last_response.headers[''Location''].should == ''/reports'' > > end > > > > when i debug my method I see that request.env["HTTP_AUTHORIZATION"] is > > nil, > > so the header was not sent with it. how to mock it? >I think you are passing your headers in as the request params. I believe you''re using rack-test under the covers and the api for #get is: get(uri, params = {}, env = {}, &block) I think env is where you want your headers. so maybe: get ''/'', {}, { ''HTTP_AUTHORIZATION'' => ''Basic c2cvbGFuOmFBITExMQ=='' } I haven''t written a Sinatra app in a while, but I hope this helps, Zach> > I don''t have any personal experience w/ Sinatra yet (I know, I know, but > there are only so many hours in a day), so I''ll leave it to someone else to > comment on how to approach the Sinatra questions. > > Cheers, > David > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Zach Dennis http://www.continuousthinking.com (personal) http://www.mutuallyhuman.com (hire me) http://ideafoundry.info/behavior-driven-development (first rate BDD training) @zachdennis (twitter) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101021/471836ce/attachment.html>
oren
2010-Oct-21 17:41 UTC
[rspec-users] new syntax question and mocking HTTP_AUTHORIZATION
works! thanks so much for not only solving it but also mentioning rack- test. I see the get method here: http://github.com/brynary/rack-test/blob/master/lib/rack/test.rb but it''s not clear how to use that (I guess it depends on your experience), I wish there was a usage/sample section.. On Oct 21, 1:10?pm, Zach Dennis <zach.den... at gmail.com> wrote:> On Wed, Oct 20, 2010 at 11:19 PM, David Chelimsky <dchelim... at gmail.com>wrote: > > > > > > > > > > > On Oct 20, 2010, at 9:56 PM, oren wrote: > > > > I am specing a sinatra app. > > > > it "should send non-valid user to /login" do > > > ?get ''/'' > > > ?last_response.headers[''Location''].should == ''/reports'' > > > end > > > > this is working fine but I would like to know how to convert it to the > > > new syntax or if there is a better way to do that. > > > > before(:each) { get ''/'' } > > > subject { last_response } > > > > its(:status) {should == 302} > > > require ''ap'' > > > its(:headers) {should include(''/login'')} > > > This is not "the new syntax." It is simply an alternate syntax that has > > been around for over a year and works well for a small subset of things you > > might want to specify like initial state: > > > describe Array do > > ?context "when first created" do > > ? ?subject { Array.new } > > ? ?its(:length) { should eq(0) } > > ?end > > end > > > It is not in any way an all-purpose replacement for more specifying > > _behaviour_. > > > > another question: > > > > ? it "should send valid user to /reports" do > > > ? ? ? ?get ''/'', { ''HTTP_AUTHORIZATION'' => ''Basic > > > c2cvbGFuOmFBITExMQ=='' } > > > ? ? ? ?last_response.headers[''Location''].should == ''/reports'' > > > ? ? ?end > > > > when i debug my method I see that request.env["HTTP_AUTHORIZATION"] is > > > nil, > > > so the header was not sent with it. how to mock it? > > I think you are passing your headers in as the request params. I believe > you''re using rack-test under the covers and the api for #get is: > > ? ? ? get(uri, params = {}, env = {}, &block) > > I think env is where you want your headers. so maybe: > > ? ? ? get ''/'', {}, { ''HTTP_AUTHORIZATION'' => ''Basic c2cvbGFuOmFBITExMQ=='' } > > I haven''t written a Sinatra app in a while, but I hope this helps, > > Zach > > > > > I don''t have any personal experience w/ Sinatra yet (I know, I know, but > > there are only so many hours in a day), so I''ll leave it to someone else to > > comment on how to approach the Sinatra questions. > > > Cheers, > > David > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > -- > Zach Dennishttp://www.continuousthinking.com(personal)http://www.mutuallyhuman.com(hire me)http://ideafoundry.info/behavior-driven-development(first rate BDD > training) > @zachdennis (twitter) > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users