# Testing an action behind HTTP Authentication

**URL:** <https://www.ruby-forum.com/t/testing-an-action-behind-http-authentication/134449>\
**Category:** Rails\
**Created:** [April 10, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/testing-an-action-behind-http-authentication/134449 "2008-04-10T23:25:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/testing-an-action-behind-http-authentication/134449/1 "2008-04-10T23:25:39Z")

</div>

Greetings,

I’m writing an integration test and would like to make sure that, with  
the correct credentials, I get the proper response. The problem is I  
don’t know how to use the ‘get’ method to attach the username and  
password to the request.

For example, when using RSpec’s Story tests…

When(“I request the user information for that user’s id”) do  
get “/api/users/#{@user.id}”  
end

Gives me a 401 Unauthorized response because that request isn’t  
authenticating.

Any help would be appreciated, thanks!

~ Jason C

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2008, 8:30am UTC](https://www.ruby-forum.com/t/testing-an-action-behind-http-authentication/134449/2 "2008-04-11T08:30:57Z")

</div>

On Apr 10, 2:25 pm, Jason C [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Greetings,
> 
> I’m writing anintegrationtestandwouldliketomakesurethat, with  
> thecorrectcredentials, I get the proper response. The problem is I  
> don’t know how to use the ‘get’ method to attach the username and  
> password to the request.

Take a look at

[http://svn.codahale.com/simple\_http\_auth/test/simple\_http\_auth\_test.rb](http://svn.codahale.com/simple_http_auth/test/simple_http_auth_test.rb)

There is a method at line 268,

def login(action, username, password, header = ‘HTTP\_AUTHORIZATION’)  
@request.env[header] = “Basic #{Base64.encode64(username \<\< ‘:’ \<\<  
password)}”  
get action  
end

Maybe you can use the complete plugin (  
[http://svn.codahale.com/simple\_http\_auth](http://svn.codahale.com/simple_http_auth) )?

Stephan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/testing-an-action-behind-http-authentication/134449/3 "2008-04-11T16:26:15Z")

</div>

On Apr 11, 2:30 am, Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Maybe you can use the complete plugin ([http://svn.codahale.com/simple\_http\_auth](http://svn.codahale.com/simple_http_auth))?
> 
> Stephan

Thanks Stephan! That worked!
