# Unit Test: Mocking the User-Agent

**URL:** <https://www.ruby-forum.com/t/unit-test-mocking-the-user-agent/141391>\
**Category:** Rails\
**Created:** [June 25, 2008, 11:31am UTC](https://www.ruby-forum.com/t/unit-test-mocking-the-user-agent/141391 "2008-06-25T11:31:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [June 25, 2008, 11:31am UTC](https://www.ruby-forum.com/t/unit-test-mocking-the-user-agent/141391/1 "2008-06-25T11:31:42Z")

</div>

Hi,

Im trying to test the following method:

def notice  
65 userAgent = request.env[‘HTTP\_USER\_AGENT’]  
66  
67 if userAgent.index(‘MSIE 7.0;’) != nil  
68 @ie7 = true  
69 elsif userAgent.index(‘MSIE 6.0;’) != nil  
70 @ie6 = true  
71 end  
72  
73 if (@ie6)  
74 render :action=\> “notice”, :layout =\> false  
75 else  
76 redirect\_to :controller =\> “home”, :action =\> “index”  
77 end  
78 end

the problem im having is that i cant seem to find a way of setting the  
user agent before it goes into the method and checks the type of browser  
being used.

im sure this is something trivial involving a mock but its just proving  
to be a bit illusive to me ☹

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [June 25, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/unit-test-mocking-the-user-agent/141391/2 "2008-06-25T22:19:25Z")

</div>

If you’re in a functional test and have a TestRequest object named  
@request, you could do this:

```
@request.env["User-Agent"] = "whutever" # I think that header

```

name is right…

You can set arbitrary headers like that.

–Jeremy

On Wed, Jun 25, 2008 at 4:31 AM, Chris G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 69 elsif userAgent.index(‘MSIE 6.0;’) != nil
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
[http://jeremymcanally.com/](http://jeremymcanally.com/)  
[http://entp.com](http://entp.com)

Read my books:  
Ruby in Practice ([Ruby in Practice](http://manning.com/mcanally/))  
My free Ruby e-book ([http://humblelittlerubybook.com/](http://humblelittlerubybook.com/))

Or, my blogs:

> **[Site not found · DreamHost](http://mrneighborly.com)**
>
> The owner of this domain has not yet uploaded their website.

[http://rubyinpractice.com](http://rubyinpractice.com)

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [June 26, 2008, 10:20am UTC](https://www.ruby-forum.com/t/unit-test-mocking-the-user-agent/141391/3 "2008-06-26T10:20:01Z")

</div>

Hi Jeremy,

thanks for the reply.

How exactly do i build a testRequest? ive never come across such a  
thing?

Chris

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [June 26, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/unit-test-mocking-the-user-agent/141391/4 "2008-06-26T17:54:41Z")

</div>

You need to read up on Rails’ functional testing. Google has a lot of  
good resources! 🙂

[http://www.google.com/search?client=safari&rls=en-us&q=rails+functional+testing&ie=UTF-8&oe=UTF-8](http://www.google.com/search?client=safari&rls=en-us&q=rails+functional+testing&ie=UTF-8&oe=UTF-8)

–Jeremy

On Thu, Jun 26, 2008 at 3:20 AM, Chris G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
[http://jeremymcanally.com/](http://jeremymcanally.com/)  
[http://entp.com](http://entp.com)

Read my books:  
Ruby in Practice ([Ruby in Practice](http://manning.com/mcanally/))  
My free Ruby e-book ([http://humblelittlerubybook.com/](http://humblelittlerubybook.com/))

Or, my blogs:

> **[Site not found · DreamHost](http://mrneighborly.com)**
>
> The owner of this domain has not yet uploaded their website.

[http://rubyinpractice.com](http://rubyinpractice.com)
