Mocking a Network Resource with Camping

Names have been changed to protect the innocent

One of our projects involves interacting with an internal credit card processing server. We wrote a small middle-layer library that will be talking to the server, and which will be shared among other applications that do credit card processing for this client. Basically, we post XML to the server (which contains the CC info), and get back XML holding the response code.

With payment processing you usually test against the live server, using pre-determined credit card numbers like 4111111111111111—supplied by Visa or Master Card or what not. Unfortunately, this server is hosted inside our client’s well-protected network, and is unaccessible from our development machines. I also don’t like the idea of depending on some other dude’s service for my tests to pass, and I’d like to have more control over what results the tests can expect.

Camping to the rescue

We decided that best solution was to run a small web server that mocks the real server’s functionality. The only issue was in figuring out how to write this server in as little time as possible (it is only for testing, after all). Camping was perfect for the job. Here’s the code:

#!/bin/env ruby

require 'rubygems'
require 'camping'
require 'rexml/document'

Camping.goes :PaymentProcessor

silence_warnings do
  Responses = {
    "4111111111111111" => { :response_code => 101 },
    "4111111111111112" => { :response_code => 102 },
    "Missing"          => { :response_code => 103 },
    "Malformed"        => { :response_code => 200 },
  Responses.default = { :response_code => 100 }

module PaymentProcessor::Controllers
  class Index < R '/'
    def get
      render :index

  class Purchase < R '/purchase'
    def get
      render :index

    def post
      @headers['Content-Type'] = 'text/xml charset=utf8'
        $ "\nPOST: \n#{@raw_post_data}"
        xml =
        cc_number = xml.elements["//customer-cc-number"]
        @payment = Responses[cc_number ? cc_number.text : "Missing"]
      rescue Exception => e
        $ e
        @payment = Responses["Malformed"]
      $ "\nResponding with: \n#{@payment.to_xml}"
      # $ "@raw_post_data: #{@raw_post_data}"
      render :purchase

module PaymentProcessor::Views
  def index
    html do
      body do
        h1  "Payment Gateway Mock"
        div "This server is here to let thoughtbot's developers" +
              "  test their credit card processing in a controlled environment."
        div "Point your payment library at /purchase, and use the following test
        pre Responses.to_yaml
        div "All other values for the credit card number will return successful."

  def purchase
    @payment.to_xml(:root => "response")

if __FILE__ == $0
  require 'mongrel'

  $logger ='loggylogger.log')

  server = Mongrel::Camping::start("", 2000, "/", PaymentProcessor)
  puts "** PaymentProcessor is running at http://localhost:3000/"

All in under 80 lines! Fantastic. It’s even self-documenting (of a sort) for anyone browsing to http://localhost:2000/.

We now run this server on our central development box, and point our library at it when in development or testing mode. Problem solved.

There’s always a gotcha

So, I had to make the smallest of changes to the camping code in order to get to the XML in the post body…

<         qs.merge!(C.qsp(
>         @raw_post_data =
>         qs.merge!(C.qsp(@raw_post_data))

I won’t say that the camping code was easy to read, but at under 800 lines, it wasn’t too hard to figure out the patch above. And the folk over at #camping were all incredibly helpful.


Sharpen your programming skills by completing coding exercises that are reviewed by other developers at Upcase today.