HttpTesting - Library for testing HTTP requests in Ruby.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.ruby-gemset
.ruby-version
Gemfile
MIT-LICENSE
README.rdoc
Rakefile
http-testing.gemspec

README.rdoc

HttpTesting

Library for testing HTTP requests in Ruby.

Install

gem install http-testing

General usage pattern

#Start HttpTesting context
c = HttpTesting::Context.start(30013) do |request, response|
  # request - instance of WEBrick::HTTPRequest
  # response - instance of WEBrick::HTTPResponse

  #Assert request properties

  #Set response
end

#Your code that will send the request goes here

#Wait for request to complete
c.wait

#Check the result of request

RSpec sample

require 'spec_helper'

describe "HTTP requests sender" do
  it "should get '/say-hello'" do
    #Start HttpTesting context on port 30013
    c = HttpTesting::Context.start(30013) do |request, response|
      # request - instance of WEBrick::HTTPRequest
      # response - instance of WEBrick::HTTPResponse

      #Check method and path
      request.request_method.should eql "GET"
      request.path.should eql '/say-hello'

      #Set response
      response.body = "Hello World"
    end

    #Send get request
    result = Net::HTTP.get(URI.parse('http://localhost:30013/say-hello'))

    #Wait for request to complete
    c.wait

    #Check the result
    result.should eql "Hello World"
  end  

  it "should post '/hello-there'" do
    #Starting context
    c = HttpTesting::Context.start(30013) do |request, response|
      #Checking method, path and body of request
      request.request_method.should eql "POST"
      request.path.should eql '/hello-there'
      request.body.should eql "from=Mike"

      #Set response
      response.body = "Hello"
    end

    #Send post request
    response = Net::HTTP.post_form(URI.parse('http://localhost:30013/hello-there'), :from => "Mike")

    #Wait for request to complete
    c.wait

    #Check the result
    response.body.should eql "Hello"
  end
end