Skip to content

Commit

Permalink
Added option to record in order or not
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinon committed Aug 11, 2018
1 parent e1b4ce6 commit fc6cccb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ load_cassette("cassette-two", :record) do
end
```

To record difference in the same response add the `:in_order` argument. This Will
record and play back the VCR in order the requests occurred (recording new ones if missing).

```crystal
VCR.use_cassette("cassette-one", :record, :in_order) do
r1 = HTTP::Client.get("https://jsonplaceholder.typicode.com/todos")
HTTP::Client.delete("https://jsonplaceholder.typicode.com/todos/1")
r2 = HTTP::Client.get("https://jsonplaceholder.typicode.com/todos")
end
```

Customize the location of where the cassettes are stored. The default is `spec/fixtures/vcr`.

```crystal
Expand Down
5 changes: 4 additions & 1 deletion src/ext/http_client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class HTTP::Client

# Create a dir for our cassette
FileUtils.mkdir(cassette_dir) unless (Dir.exists?(cassette_dir))
cassette_path = File.join(cassette_dir, "#{VCR.sequence}.#{req_md5}.vcr")

# Make file name based on if this cassette should be tracked in order
file_name = VCR.in_order? ? "#{VCR.sequence}.#{req_md5}.vcr" : "#{req_md5}.vcr"
cassette_path = File.join(cassette_dir, file_name)

# If it exists, load and return the data
if File.exists?(cassette_path)
Expand Down
30 changes: 30 additions & 0 deletions src/vcr.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "./vcr/*"
module VCR
extend self

@@in_order = false
@@sequence = 0

Habitat.create do
Expand All @@ -22,10 +23,38 @@ module VCR
@@sequence += 1
end

# Returns true if the casset should record requests in order
def in_order?
@@in_order
end

# Defines the cassette to load for recording. Optional arguments can also be
# passed.
#
# Options:
# * `:record` - will delete any VCR files in this cassette so new ones can be generated
# * `:in_order` - will record requests in order (allows multiple records of the same request). Should be used when you expect the result to be different on a second request
#
# Example:
# ```
# VCR.use_cassette("cassette-one") do
# r1 = HTTP::Client.get("https://jsonplaceholder.typicode.com/todos")
# end
# ```
#
# Record new responses, in order, so i can verify the delete worked
# ```
# VCR.use_cassette("cassette-one", :record, :in_order) do
# r1 = HTTP::Client.get("https://jsonplaceholder.typicode.com/todos")
# HTTP::Client.delete("https://jsonplaceholder.typicode.com/todos/1")
# r2 = HTTP::Client.get("https://jsonplaceholder.typicode.com/todos")
# end
# ```
def use_cassette(cassette_name : String, *args, &block)
@@cassette_name = cassette_name
@@sequence = 0

@@in_order = args.includes?(:in_order)
reset_cassette(cassette_name) if args.includes?(:record)

block.call
Expand All @@ -36,6 +65,7 @@ module VCR
private def reset!
@@cassette_name = nil
@@sequence = 0
@@in_order = false
end

private def reset_cassette(cassette)
Expand Down

0 comments on commit fc6cccb

Please sign in to comment.