<commit_before>WebMock
=======

Library for stubbing HTTP requests and setting expectations on HTTP requests in Ruby.

Features
--------

* Stubbing HTTP requests at low Net::HTTP level (no need to change tests when you change HTTP lib interface)
* Setting and verifying expectations on HTTP requests
* Matching requests based on method, URI, headers and body
* Smart matching of the same URIs in different representations (also encoded and non encoded forms)
* Smart matching of the same headers in different representations.
* Support for Test::Unit and RSpec (and can be easily extended to other frameworks)
* Support for Net::HTTP and other http libraries based on Net::HTTP (i.e RightHttpConnection, rest-client, HTTParty)
* Easy to extend to other HTTP libraries except Net::HTTP

Installation
------------

    gem install webmock --source http://gemcutter.org

In your `test/test_helper.rb` add these two lines:

    require 'webmock/test_unit'
	
	include WebMock

or if you use RSpec add these lines to `spec/spec_helper`:

	require 'webmock/rspec'

	include WebMock

You can also use WebMock without RSpec or Test::Unit support:

	require 'webmock'

	include WebMock

## Examples



## Stubbing


### Stubbed request based on uri only and with the default response

	 stub_request(:any, "www.google.com")

	 Net::HTTP.get("www.google.com", "/")    # ===> Success

### Stubbing requests based on method, uri, body and headers

	stub_request(:post, "www.google.com").with(:body => "abc", :headers => { 'Content-Length' => 3 })

	uri = URI.parse("http://www.google.com/")
    req = Net::HTTP::Post.new(uri.path)
	req['Content-Length'] = 3
    res = Net::HTTP.start(uri.host, uri.port) {|http|
      http.request(req, "abc")
    }    # ===> Success

### Matching custom request headers

    stub_request(:any, "www.google.com").
	  with( :headers=>{ 'Header-Name' => 'Header-Value' } ).to_return(:body => "abc", :status => 200)

	uri = URI.parse('http://www.google.com/')
    req = Net::HTTP::Post.new(uri.path)
	req['Header-Name'] = 'Header-Value'
    res = Net::HTTP.start(uri.host, uri.port) {|http|
      http.request(req, 'abc')
    }    # ===> Success

### Stubbing with custom response

	stub_request(:any, "www.google.com").to_return(:body => "abc", :status => 200,  :headers => { 'Content-Length' => 3 } )
	
	Net::HTTP.get("www.google.com", '/')    # ===> "abc"

### Custom response with body specified as a path to file

	File.open('/tmp/response_body.txt', 'w') { |f| f.puts 'abc' }

	stub_request(:any, "www.google.com").to_return(:body => "/tmp/response_body.txt", :status => 200)

	Net::HTTP.get('www.google.com', '/')    # ===> "abc\n"

### Custom response with dynamically evaluated response
	
    stub_request(:any, 'www.example.net').
      to_return(:body => lambda { |request| request.body })

    RestClient.post('www.example.net', 'abc')    # ===> "abc\n"	

### Request with basic authentication

	stub_request(:any, "john:<EMAIL>@www.<EMAIL>")
	
	Net::HTTP.get(URI.parse('http://john:smith@www.google.com'))    # ===> Success

### Matching uris using regular expressions

	 stub_request(:any, /.*google.*/)

	 Net::HTTP.get('www.google.com', '/') # ===> Success

### Real requests to network can be allowed or disabled

	WebMock.allow_net_connect!

	stub_request(:any, "www.google.com").to_return(:body => "abc")

	Net::HTTP.get('www.google.com', '/')    # ===> "abc"
	
	Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
	
	WebMock.disable_net_connect!
	
	Net::HTTP.get('www.something.com', '/')    # ===> Failure<commit_msg>Improved section about basic authentication support in readme.<commit_after>WebMock
=======

Library for stubbing HTTP requests and setting expectations on HTTP requests in Ruby.

Features
--------

* Stubbing HTTP requests at low Net::HTTP level (no need to change tests when you change HTTP lib interface)
* Setting and verifying expectations on HTTP requests
* Matching requests based on method, URI, headers and body
* Smart matching of the same URIs in different representations (also encoded and non encoded forms)
* Smart matching of the same headers in different representations.
* Support for Test::Unit and RSpec (and can be easily extended to other frameworks)
* Support for Net::HTTP and other http libraries based on Net::HTTP (i.e RightHttpConnection, rest-client, HTTParty)
* Easy to extend to other HTTP libraries except Net::HTTP

Installation
------------

    gem install webmock --source http://gemcutter.org

In your `test/test_helper.rb` add these two lines:

    require 'webmock/test_unit'
	
	include WebMock

or if you use RSpec add these lines to `spec/spec_helper`:

	require 'webmock/rspec'

	include WebMock

You can also use WebMock without RSpec or Test::Unit support:

	require 'webmock'

	include WebMock

## Examples



## Stubbing


### Stubbed request based on uri only and with the default response

	 stub_request(:any, "www.google.com")

	 Net::HTTP.get("www.google.com", "/")    # ===> Success

### Stubbing requests based on method, uri, body and headers

	stub_request(:post, "www.google.com").with(:body => "abc", :headers => { 'Content-Length' => 3 })

	uri = URI.parse("http://www.google.com/")
    req = Net::HTTP::Post.new(uri.path)
	req['Content-Length'] = 3
    res = Net::HTTP.start(uri.host, uri.port) {|http|
      http.request(req, "abc")
    }    # ===> Success

### Matching custom request headers

    stub_request(:any, "www.google.com").
	  with( :headers=>{ 'Header-Name' => 'Header-Value' } ).to_return(:body => "abc", :status => 200)

	uri = URI.parse('http://www.google.com/')
    req = Net::HTTP::Post.new(uri.path)
	req['Header-Name'] = 'Header-Value'
    res = Net::HTTP.start(uri.host, uri.port) {|http|
      http.request(req, 'abc')
    }    # ===> Success

### Stubbing with custom response

	stub_request(:any, "www.google.com").to_return(:body => "abc", :status => 200,  :headers => { 'Content-Length' => 3 } )
	
	Net::HTTP.get("www.google.com", '/')    # ===> "abc"

### Custom response with body specified as a path to file

	File.open('/tmp/response_body.txt', 'w') { |f| f.puts 'abc' }

	stub_request(:any, "www.google.com").to_return(:body => "/tmp/response_body.txt", :status => 200)

	Net::HTTP.get('www.google.com', '/')    # ===> "abc\n"

### Custom response with dynamically evaluated response
	
    stub_request(:any, 'www.example.net').
      to_return(:body => lambda { |request| request.body })

    RestClient.post('www.example.net', 'abc')    # ===> "abc\n"	

### Request with basic authentication

    stub_request(:get, "user:pass@www.<EMAIL>")

    Net::HTTP.start('www.google.com') {|http|
      req = Net::HTTP::Get.new('/')
      req.basic_auth 'user', 'pass'
      http.request(req)
    }  # ===> Success

### Matching uris using regular expressions

	 stub_request(:any, /.*google.*/)

	 Net::HTTP.get('www.google.com', '/') # ===> Success

### Real requests to network can be allowed or disabled

	WebMock.allow_net_connect!

	stub_request(:any, "www.google.com").to_return(:body => "abc")

	Net::HTTP.get('www.google.com', '/')    # ===> "abc"
	
	Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
	
	WebMock.disable_net_connect!
	
	Net::HTTP.get('www.something.com', '/')    # ===> Failure
{'fraction_non_alphanumeric': 0.17052023121387283, 'fraction_numerical': 0.0033030553261767133, 'mean_word_length': 3.857620320855615, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 38, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6022610', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2273, 'n_words': 641}