# Testing the contents of an http response

**URL:** <https://www.ruby-forum.com/t/testing-the-contents-of-an-http-response/119491>\
**Category:** Rails\
**Created:** [November 7, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/testing-the-contents-of-an-http-response/119491 "2007-11-07T12:12:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 7, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/testing-the-contents-of-an-http-response/119491/1 "2007-11-07T12:12:47Z")

</div>

I have a show method i’m testing, that returns some data in json format.  
My tests show that something is being returned, but i want to get at it  
and test the contents. I can’t work out how to do this, can anyone  
help?

def test\_show\_gets\_data\_ok  
get :show, :token =\> @given\_token  
assert\_response  
#want to say something like  
#some\_json = a\_hash.to\_json  
#assert\_equal(response, some\_json)  
end

In other words, how do i set an object equal to the response?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 7, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/testing-the-contents-of-an-http-response/119491/2 "2007-11-07T18:04:36Z")

</div>

On Nov 7, 2007 6:12 AM, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> #some_json = a_hash.to_json
> #assert_equal(response, some_json)
> 
> ```
> 
> end
> 
> In other words, how do i set an object equal to the response?

The raw response will be in @response.body

assert\_equal some\_json, @response.body

You put “expected” before “actual”

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 7, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/testing-the-contents-of-an-http-response/119491/3 "2007-11-07T18:25:33Z")

</div>

Bob S. wrote:

> On Nov 7, 2007 6:12 AM, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ```
> > #some_json = a_hash.to_json
> > #assert_equal(response, some_json)
> > 
> > ```
> > 
> > end
> > 
> > In other words, how do i set an object equal to the response?
> 
> The raw response will be in @response.body
> 
> assert\_equal some\_json, @response.body
> 
> You put “expected” before “actual”

Great, thanks bob!

Next question, which i just realised i’d not though about before - how  
do i phrase my get request to ask for the response in json format?

I know that i can get a specific url, and ask for that in json, eg

get “items/1.json”

but in my example i just want to pass through some parameters rather  
than a url, eg something like -

get :show, :token =\> @given\_token, :format =\> “json”

Do you know how to do that by any chance?
