Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

HttpTesting - Library for testing HTTP requests in Ruby.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .ruby-gemset
Octocat-spinner-32 .ruby-version
Octocat-spinner-32 Gemfile
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 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
Something went wrong with that request. Please try again.