# How does one set an HTTP header in @request for a functional test

**URL:** <https://www.ruby-forum.com/t/how-does-one-set-an-http-header-in-request-for-a-functional-test/187419>\
**Category:** Rails\
**Created:** [April 22, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/how-does-one-set-an-http-header-in-request-for-a-functional-test/187419 "2010-04-22T21:23:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [April 22, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/how-does-one-set-an-http-header-in-request-for-a-functional-test/187419/1 "2010-04-22T21:23:01Z")

</div>

I have a very simple functional test for a controller, but I need to  
set a header field in the request before the invocation of the action,  
but the headers seem to be getting reset, even though I’m not doing  
multiple requests. Is this not how functional tests are to be used?

test “test the show requires system header” do  
@request.headers[“System-Name”] = “Test System”  
get :show  
assert\_response :success  
…  
end

The controller doesn’t return a success unless the header is set, so  
it fails immediately.

Is there another idiom that’s more appropriate?

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [April 22, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/how-does-one-set-an-http-header-in-request-for-a-functional-test/187419/2 "2010-04-22T21:42:20Z")

</div>

On Apr 22, 2010, at 12:18 PM, Nathan B. wrote:

> …  
> end
> 
> The controller doesn’t return a success unless the header is set, so  
> it fails immediately.
> 
> Is there another idiom that’s more appropriate?

I use something along the lines of:

request.env[‘HTTP\_ACCEPT’] = ‘application/json, text/javascript, _/_’  
request.env[‘HTTP\_X\_REQUESTED\_WITH’] = ‘XMLHttpRequest’

to set up headers for an xhr. You should be able to do something similar  
for custom headers.

Hope this helps.
