# Unit testing an HTTP client

**URL:** <https://www.ruby-forum.com/t/unit-testing-an-http-client/138703>\
**Category:** Ruby\
**Created:** [May 27, 2008, 1:00am UTC](https://www.ruby-forum.com/t/unit-testing-an-http-client/138703 "2008-05-27T01:00:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![polarapfel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/958977/32.png) [@polarapfel](https://www.ruby-forum.com/u/polarapfel)\
**Post date:** [May 27, 2008, 1:00am UTC](https://www.ruby-forum.com/t/unit-testing-an-http-client/138703/1 "2008-05-27T01:00:45Z")

</div>

Hi,  
my programm uses the HTTPClient gem to call a backend that was  
originally made for AJAX-without-the-X. Mainly it does POST without  
expecting any result.

How do you test this?

Writing a http server that logs when requests don’t conform to the API  
is easy. But how to hook it up with the test methods in the client? I’ve  
never used threads…

Or I could override the central network communication stuff to just call  
another method instead.

---

<div class="post-metadata">

**Author:** ![polarapfel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/958977/32.png) [@polarapfel](https://www.ruby-forum.com/u/polarapfel)\
**Post date:** [May 27, 2008, 1:24am UTC](https://www.ruby-forum.com/t/unit-testing-an-http-client/138703/2 "2008-05-27T01:24:46Z")

</div>

On Tue, May 27, 2008 at 7:59 AM, Tobias W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> my programm uses the HTTPClient gem to call a backend that was  
> originally made for AJAX-without-the-X. Mainly it does POST without  
> expecting any result.
> 
> How do you test this?

Hello Tobias,

I’ve been through the same routine for an http client  
([http://rufus.rubyforge.org/rufus-verbs](http://rufus.rubyforge.org/rufus-verbs)). I’ve written a test server  
based on Webrick just for the unit tests :

[http://github.com/jmettraux/rufus-verbs/tree/master/test/items.rb](http://github.com/jmettraux/rufus-verbs/tree/master/test/items.rb)

(see the class ItemServer as a starting point).

It’s then used in the startup/teadown of my unit tests :

[http://github.com/jmettraux/rufus-verbs/tree/master/test/testbase.rb](http://github.com/jmettraux/rufus-verbs/tree/master/test/testbase.rb)

Maybe that’ll inspire you.

Webrick comes with Ruby, it’s quite handy. You don’t have to worry  
about threads [for now], the server will run in its own. I’m sure  
someone else will come with an even better piece of advice.

Freundliche Gruesse,

---

<div class="post-metadata">

**Author:** ![polarapfel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/958977/32.png) [@polarapfel](https://www.ruby-forum.com/u/polarapfel)\
**Post date:** [May 27, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/unit-testing-an-http-client/138703/3 "2008-05-27T22:00:43Z")

</div>

In article  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ([http://rufus.rubyforge.org/rufus-verbs](http://rufus.rubyforge.org/rufus-verbs)). I’ve written a test server  
> based on Webrick just for the unit tests :

Your server returns defined results for certain requests. Those are  
processed by your client and used in assertions.

As I wrote, my client does not expect (nor can handle) results. I want  
to use the request that arrives at the server in my assertions, to see  
if it gets build correctly.

---

<div class="post-metadata">

**Author:** ![polarapfel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/958977/32.png) [@polarapfel](https://www.ruby-forum.com/u/polarapfel)\
**Post date:** [May 28, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/unit-testing-an-http-client/138703/4 "2008-05-28T16:23:24Z")

</div>

In article [483b82c6$0$5141$removed\_email\_address@domain.invalid](mailto:483b82c6$0$5141$removed_email_address@domain.invalid),  
Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have done it that way, and I have also used Mocha to simply make  
> the HTTP client magically return whatever result you want.

Works really nice. Thanks!
