<commit_before>= Rack Stubs

Rack middleware for specifying behaviour of HTTP resources by POSTing stub representations.

== Usage

See the features :)<commit_msg>actual readme<commit_after>= Rack Stubs

Rack middleware for stubbing responses from HTTP web services.

== Installation

  gem install rack-stubs

== Using the server middleware

rack-stubs is implemented as rack middleware, so you would typically host it as part of a rack web app. You can write a rack configuration (config.ru) like this if you want to get started:

  require 'rubygems'
  require 'rack-stubs'

  use RackStubs::Middleware
  run lambda { |e| [404, { 'Content-Type' => 'text/plain' }, ["Not found"]] }
  
Then you can run the rackup binary:

  rackup
  
...and you'll have a server with rack-stubs running at http://127.0.0.1:9292

To create a stub response, let's say we want HTTP GET requests to /foo to return an HTTP 200 status, with the body "bar" and the content type "text/plain". Using a REST tool of some kind (you could use the rest-client ruby gem or the poster firefox plugin) you can now set this up by making the following HTTP request:

  method  : POST
  action  : http://127.0.0.1:9292/foo
  body    : { "GET" : [200, { "Content-Type": "text/plain" }, ["bar"]] }
  headers : Content-Type=application/json+rack-stub

All subsequent requests to http://127.0.0.1:9292/foo should now respond with HTTP 200 status, Content-Type=text/plain and the body "bar". The body is json, but it's essentially implemented as a hash of simple rack-style response tuples.

== Using the client

To use rack-stubs from Ruby without diving down into REST, rack-stubs includes a small client library. To set up the same behaviour as specified above, you would use the client like this:

  client = RackStubs::Client.new("http://127.0.0.1:9292")
  client.get("/foo").returns(200, { "Content-Type" => "text/plain" }, "bar")
{'fraction_non_alphanumeric': 0.09804955192409066, 'fraction_numerical': 0.028993147074327884, 'mean_word_length': 3.9427083333333335, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '5273846', 'n_tokens_mistral': 604, 'n_tokens_neox': 554, 'n_words': 252}