Skip to content

Commit fc98782

Browse files
Update README.md (#334)
- Organize Caching configuration params on its own section using bullet points - Put all Caching configuration params together - Add Ruby syntax highlighting to some blocks that were missing
1 parent f137217 commit fc98782

File tree

1 file changed

+82
-80
lines changed

1 file changed

+82
-80
lines changed

README.md

Lines changed: 82 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ you can turn on cache persistence. This way you don't have to manually mock out
289289
everything as requests are automatically recorded and played back. With cache
290290
persistence you can take tests completely offline.
291291

292+
The cache works with all types of requests and will distinguish between
293+
different POST requests to the same URL.
294+
295+
### Params
296+
292297
```ruby
293298
Billy.configure do |c|
294299
c.cache = true
@@ -319,89 +324,123 @@ Billy.configure do |c|
319324
end
320325
```
321326

322-
The cache works with all types of requests and will distinguish between
323-
different POST requests to the same URL.
324-
325-
`c.cache_request_headers` is used to store the outgoing request headers in the cache.
327+
- `c.cache_request_headers` is used to store the outgoing request headers in the cache.
326328
It is also saved to yml if `persist_cache` is enabled. This additional information
327329
is useful for debugging (for example: viewing the referer of the request).
328330

329-
`c.ignore_params` is used to ignore parameters of certain requests when
331+
- `c.ignore_params` is used to ignore parameters of certain requests when
330332
caching. You should mostly use this for analytics and various social buttons as
331333
they use cache avoidance techniques, but return practically the same response
332334
that most often does not affect your test results.
333335

334-
`c.allow_params` is used to allow parameters of certain requests when caching. This is best used when a site
335-
has a large number of analytics and social buttons. `c.allow_params` is the opposite of `c.ignore_params`,
336-
a whitelist to a blacklist. In order to toggle between using one or the other, use `c.use_ignore_params`.
337-
338-
`c.strip_query_params` is used to strip query parameters when you stub some requests
339-
with query parameters. Default value is true. For example, `proxy.stub('http://myapi.com/user/?country=FOO')`
340-
is considered the same as: `proxy.stub('http://myapi.com/user/?anything=FOO')` and
341-
generally the same as: `proxy.stub('http://myapi.com/user/')`. When you need to distinguish between all these requests,
342-
you may set this config value to false.
343-
344-
`c.dynamic_jsonp` is used to rewrite the body of JSONP responses based on the
345-
callback parameter. For example, if a request to `http://example.com/foo?callback=bar`
346-
returns `bar({"some": "json"});` and is recorded, then a later request to
347-
`http://example.com/foo?callback=baz` will be a cache hit and respond with
348-
`baz({"some": "json"});` This is useful because most JSONP implementations
349-
base the callback name off of a timestamp or something else dynamic.
350-
351-
`c.dynamic_jsonp_keys` is used to configure which parameters to ignore when
352-
using `c.dynamic_jsonp`. This is helpful when JSONP APIs use cache-busting
353-
parameters. For example, if you want `http://example.com/foo?callback=bar&id=1&cache_bust=12345` and `http://example.com/foo?callback=baz&id=1&cache_bust=98765` to be cache hits for each other, you would set `c.dynamic_jsonp_keys = ['callback', 'cache_bust']` to ignore both params. Note
354-
that in this example the `id` param would still be considered important.
355-
356-
`c.dynamic_jsonp_callback_name` is used to configure the name of the JSONP callback
357-
parameter. The default is `callback`.
358-
359-
`c.path_blacklist = []` is used to always cache specific paths on any hostnames,
336+
- `c.path_blacklist = []` is used to always cache specific paths on any hostnames,
360337
including whitelisted ones. This is useful if your AUT has routes that get data
361338
from external services, such as `/api` where the ajax request is a local URL but
362339
the actual data is coming from a different application that you want to cache.
363340

364-
`c.merge_cached_responses_whitelist = []` is used to group together the cached
341+
- `c.merge_cached_responses_whitelist = []` is used to group together the cached
365342
responses for specific uri regexes that match any part of the url. This is useful
366343
for ensuring that any kind of analytics and various social buttons that have
367344
slightly different urls each time can be recorded once and reused nicely. Note
368345
that the request body is ignored for requests that contain a body.
369346

370-
`c.ignore_cache_port` is used to strip the port from the URL if it exists. This
347+
- `c.ignore_cache_port` is used to strip the port from the URL if it exists. This
371348
is useful when caching local paths (via `path_blacklist`) or other local rack apps
372349
that are running on random ports.
373350

374-
`c.non_successful_cache_disabled` is used to not cache responses without 200-series
351+
- `c.non_successful_cache_disabled` is used to not cache responses without 200-series
375352
or 304 status codes. This prevents unauthorized or internal server errors from
376353
being cached and used for future test runs.
377354

378-
`c.non_successful_error_level` is used to log when non-successful responses are
355+
- `c.non_successful_error_level` is used to log when non-successful responses are
379356
received. By default, it just writes to the log file, but when set to `:error`
380357
it throws an error with the URL and status code received for easier debugging.
381358

382-
`c.non_whitelisted_requests_disabled` is used to disable hitting new URLs when
359+
- `c.non_whitelisted_requests_disabled` is used to disable hitting new URLs when
383360
no cache file exists. Only whitelisted URLs (on non-blacklisted paths) are
384361
allowed, all others will throw an error with the URL attempted to be accessed.
385362
This is useful for debugging issues in isolated environments (ie.
386363
continuous integration).
387364

388-
`c.cache_path` can be used to locate the cache directory to a different place
365+
- `c.cache_path` can be used to locate the cache directory to a different place
389366
other than `system temp directory/puffing-billy`.
390367

391-
`c.certs_path` can be used to locate the directory for dynamically generated
368+
- `c.certs_path` can be used to locate the directory for dynamically generated
392369
SSL certificates to a different place other than `system temp
393370
directory/puffing-billy/certs`.
394371

395-
`c.proxy_host` and `c.proxy_port` are used for the Billy proxy itself which runs locally.
372+
- `c.proxy_host` and `c.proxy_port` are used for the Billy proxy itself which runs locally.
396373

397-
`c.proxied_request_host` and `c.proxied_request_port` are used if an internal proxy
374+
- `c.proxied_request_host` and `c.proxied_request_port` are used if an internal proxy
398375
server is required to access the internet. Most common in larger companies.
399376

400-
`c.record_requests` can be used to record all requests that puffing billy proxied.
377+
- `c.allow_params` is used to allow parameters of certain requests when caching. This is best used when a site
378+
has a large number of analytics and social buttons. `c.allow_params` is the opposite of `c.ignore_params`,
379+
a whitelist to a blacklist. In order to toggle between using one or the other, use `c.use_ignore_params`.
380+
381+
- `c.strip_query_params` is used to strip query parameters when you stub some requests
382+
with query parameters. Default value is true. For example, `proxy.stub('http://myapi.com/user/?country=FOO')`
383+
is considered the same as: `proxy.stub('http://myapi.com/user/?anything=FOO')` and
384+
generally the same as: `proxy.stub('http://myapi.com/user/')`. When you need to distinguish between all these requests,
385+
you may set this config value to false.
386+
387+
- `c.dynamic_jsonp` is used to rewrite the body of JSONP responses based on the
388+
callback parameter. For example, if a request to `http://example.com/foo?callback=bar`
389+
returns `bar({"some": "json"});` and is recorded, then a later request to
390+
`http://example.com/foo?callback=baz` will be a cache hit and respond with
391+
`baz({"some": "json"});` This is useful because most JSONP implementations
392+
base the callback name off of a timestamp or something else dynamic.
393+
394+
- `c.dynamic_jsonp_keys` is used to configure which parameters to ignore when
395+
using `c.dynamic_jsonp`. This is helpful when JSONP APIs use cache-busting
396+
parameters. For example, if you want `http://example.com/foo?callback=bar&id=1&cache_bust=12345` and `http://example.com/foo?callback=baz&id=1&cache_bust=98765` to be cache hits for each other, you would set `c.dynamic_jsonp_keys = ['callback', 'cache_bust']` to ignore both params. Note
397+
that in this example the `id` param would still be considered important.
398+
399+
- `c.dynamic_jsonp_callback_name` is used to configure the name of the JSONP callback
400+
parameter. The default is `callback`.
401+
402+
- `c.record_requests` can be used to record all requests that puffing billy proxied.
401403
This can be useful for debugging purposes, for instance if you are unsure why
402404
your stubbed requests are not being successfully proxied.
403405

404-
Example usage:
406+
- `c.cache_request_body_methods` is used to specify HTTP methods of requests that you would like to cache separately based on the contents of the request body. The default is ['post'].
407+
408+
- `c.use_ignore_params` is used to choose whether to use the ignore_params blacklist or the allow_params whitelist. Set to `true` to use `c.ignore_params`,
409+
`false` to use `c.allow_params`
410+
411+
- `c.before_handle_request` is used to modify `method`, `url`, `headers`, `body` before handle request by `stubs`, `cache` or `proxy`. Method accept 4 argumens and must return array of this arguments:
412+
413+
```ruby
414+
c.before_handle_request = proc { |method, url, headers, body|
415+
filtered_body = JSON.dump(filter_secret_data(JSON.load(body)))
416+
[method, url, headers, filtered_body]
417+
}
418+
```
419+
420+
- `c.cache_simulates_network_delays` is used to add some delay before cache returns response. When set to `true`, cached requests will wait from configured delay time before responding. This allows to catch various race conditions in asynchronous front-end requests. The default is `false`.
421+
422+
- `c.cache_simulates_network_delay_time` is used to configure time (in seconds) to wait until responding from cache. The default is `0.1`.
423+
424+
- `c.after_cache_handles_request` is used to configure a callback that can operate on the response after it has been retrieved from the cache but before it is returned. The callback receives the request and response as arguments, with a request object like: `{ method: method, url: url, headers: headers, body: body }`. An example usage would be manipulating the Access-Control-Allow-Origin header so that your test server doesn't always have to run on the same port in order to accept cached responses to CORS requests:
425+
426+
```ruby
427+
Billy.configure do |c|
428+
...
429+
fix_cors_header = proc do |_request, response|
430+
allowed_origins = response[:headers]['Access-Control-Allow-Origin']
431+
if allowed_origins.present?
432+
localhost_port_pattern = %r{(?<=http://127\.0\.0\.1:)(\d+)}
433+
allowed_origins.sub!(
434+
localhost_port_pattern, Capybara.current_session.server.port.to_s
435+
)
436+
end
437+
end
438+
c.after_cache_handles_request = fix_cors_header
439+
...
440+
end
441+
```
442+
443+
### Example usage:
405444

406445
```ruby
407446
require 'table_print' # Add this dependency to your gemfile
@@ -455,43 +494,6 @@ The handler column indicates how Puffing Billy handled your request:
455494
If your `status` is set to `inflight` this request has not yet been handled fully. Either puffing billy crashed
456495
internally on this request, or your test ended before it could complete successfully.
457496

458-
`c.cache_request_body_methods` is used to specify HTTP methods of requests that you would like to cache separately based on the contents of the request body. The default is ['post'].
459-
460-
`c.after_cache_handles_request` is used to configure a callback that can operate on the response after it has been retrieved from the cache but before it is returned. The callback receives the request and response as arguments, with a request object like: `{ method: method, url: url, headers: headers, body: body }`. An example usage would be manipulating the Access-Control-Allow-Origin header so that your test server doesn't always have to run on the same port in order to accept cached responses to CORS requests:
461-
462-
```
463-
Billy.configure do |c|
464-
...
465-
fix_cors_header = proc do |_request, response|
466-
allowed_origins = response[:headers]['Access-Control-Allow-Origin']
467-
if allowed_origins.present?
468-
localhost_port_pattern = %r{(?<=http://127\.0\.0\.1:)(\d+)}
469-
allowed_origins.sub!(
470-
localhost_port_pattern, Capybara.current_session.server.port.to_s
471-
)
472-
end
473-
end
474-
c.after_cache_handles_request = fix_cors_header
475-
...
476-
end
477-
```
478-
479-
`c.use_ignore_params` is used to choose whether to use the ignore_params blacklist or the allow_params whitelist. Set to `true` to use `c.ignore_params`,
480-
`false` to use `c.allow_params`
481-
482-
`c.before_handle_request` is used to modify `method`, `url`, `headers`, `body` before handle request by `stubs`, `cache` or `proxy`. Method accept 4 argumens and must return array of this arguments:
483-
484-
```
485-
c.before_handle_request = proc { |method, url, headers, body|
486-
filtered_body = JSON.dump(filter_secret_data(JSON.load(body)))
487-
[method, url, headers, filtered_body]
488-
}
489-
```
490-
491-
`c.cache_simulates_network_delays` is used to add some delay before cache returns response. When set to `true`, cached requests will wait from configured delay time before responding. This allows to catch various race conditions in asynchronous front-end requests. The default is `false`.
492-
493-
`c.cache_simulates_network_delay_time` is used to configure time (in seconds) to wait until responding from cache. The default is `0.1`.
494-
495497
### Cache Scopes
496498

497499
If you need to cache different responses to the same HTTP request, you can use
@@ -618,7 +620,7 @@ end
618620

619621
## Proxy timeouts
620622

621-
By default, the Puffing Billy proxy will use the EventMachine:HttpRequest timeouts of 5 seconds
623+
By default, the Puffing Billy proxy will use the `EventMachine::HttpRequest` timeouts of 5 seconds
622624
for connect and 10 seconds for inactivity when talking to downstream servers.
623625

624626
These can be configured as follows:

0 commit comments

Comments
 (0)