Class | RFuzz::HttpClient |
In: |
lib/rfuzz/client.rb
|
Parent: | Object |
The actual HttpClient that does the work with the thinnest layer between you and the protocol. All exceptions and leaks are allowed to pass through since those are important when testing. It doesn’t pretend to be a full client, but instead is just enough client to track cookies, form proper HTTP requests, and return HttpResponse hashes with the results.
It’s designed so that you create one client, and then you work it with a minimum of parameters as you need. The initialize method lets you pass in defaults for most of the parameters you’ll need, and you can simple call the method you want and it’ll be translated to an HTTP method (client.get => GET, client.foobar = FOOBAR).
Here’s a few examples:
client = HttpClient.new(:head => {"X-DefaultHeader" => "ONE"}) resp = client.post("/test") resp = client.post("/test", :head => {"X-TestSend" => "Status"}, :body => "TEST BODY") resp = client.put("/testput", :query => {"q" => "test"}, :body => "SOME JUNK") client.reset
The HttpClient.reset call clears cookies that are maintained.
It uses method_missing to do the translation of .put to "PUT /testput HTTP/1.1" so you can get into trouble if you’re calling unknown methods on it. By default the methods are PUT, GET, POST, DELETE, HEAD. You can change the allowed methods by passing :allowed_methods => [:put, :get, ..] to the initialize for the object.
You can register a "notifier" with the client that will get called when different events happen. Right now the Notifier class just has a few functions for the common parts of an HTTP request that each take a symbol and some extra parameters. See RFuzz::Notifier for more information.
:head => {K => V} or {K => [V1,V2]} :query => {K => V} or {K => [V1,V2]} :body => "some body" (you must encode for now) :cookies => {K => V} or {K => [V1, V2]} :allowed_methods => [:put, :get, :post, :delete, :head] :notifier => Notifier.new :redirect => false (give it a number and it'll follow redirects for that count)
TRANSFER_ENCODING | = | "TRANSFER_ENCODING" |
CONTENT_LENGTH | = | "CONTENT_LENGTH" |
SET_COOKIE | = | "SET_COOKIE" |
LOCATION | = | "LOCATION" |
HOST | = | "HOST" |
HTTP_REQUEST_HEADER | = | "%s %s HTTP/1.1\r\n" |
REQ_CONTENT_LENGTH | = | "Content-Length" |
REQ_HOST | = | "Host" |
CHUNK_SIZE | = | 1024 * 16 |
CRLF | = | "\r\n" |
allowed_methods | [RW] | Access to the host, port, default options, and cookies currently in play |
cookies | [RW] | Access to the host, port, default options, and cookies currently in play |
host | [RW] | Access to the host, port, default options, and cookies currently in play |
notifier | [RW] | Access to the host, port, default options, and cookies currently in play |
options | [RW] | Access to the host, port, default options, and cookies currently in play |
port | [RW] | Access to the host, port, default options, and cookies currently in play |
Builds a full request from the method, uri, req, and @cookies using the default @options and writes it to out (should be an IO).
It returns the body that the caller should use (based on defaults resolution).
Translates unknown function calls into PUT, GET, POST, DELETE, HEAD methods. The allowed HTTP methods allowed are restricted by the @allowed_methods attribute which you can set after construction or during construction with :allowed_methods => [:put, :get, …]
Sends the notifications to the registered notifier, taking a block that it runs doing the :begins, :ends states around it.
It also catches errors transparently in order to call the notifier when an attempt fails.
Collects up a chunked body both collecting the body together and collecting the chunks into HttpResponse.raw_chunks[] for alternative analysis.
Does the read operations needed to parse a header with the @parser. A "header" in this case is either an HTTP header or a Chunked encoding header (since the @parser handles both).
Reads an HTTP response from the given socket. It uses readpartial which only appeared in Ruby 1.8.4. The result is a fully formed HttpResponse object for you to play with.
As with other methods in this class it doesn’t stop any exceptions from reaching your code. It’s for experts who want these exceptions so either write a wrapper, use net/http, or deal with it on your end.
Does the socket connect and then build_request, read_response calls finally returning the result.
Reads the SET_COOKIE string out of resp and translates it into the @cookies store for this HttpClient.