manastech / webmock.cr

Mock HTTP::Client
MIT License
103 stars 19 forks source link

Consider adding VCR-like support #33

Open mwlang opened 5 years ago

mwlang commented 5 years ago

I'd be happy to submit a PR that implements VCR-like support as implemented here:

https://github.com/mwlang/binance/blob/master/spec/support/vcr.cr

Basic idea taken from the Ruby VCR gem

With this, it makes writing a spec like this possible:

  context "success response" do
    it "#ping" do
      with_vcr_cassette "public/ping_success" do
        client.ping.pong.should be_truthy
      end
  end

  end
  context "error response" do
    it "#ping" do
      with_vcr_cassette("public/ping_error") do
        response = client.ping
        response.body.should eq "{\"code\": -3121, \"msg\": \"Strange Error.\"}"
        response.error_code.should eq -3121
        response.success.should be_falsey
      end
    end
  end

The first time a cassette is called and it's file does not exist, then the spec is implemented with a live call to the API server, capturing the response and writing both the request and response data to a *.yml file. The subsequent calls plays back the information captured.

Is there interest in adding this to the webmock shard? If so and approved, I'll implement in a PR and submit for consideration. If not interested, I will perhaps consider extracting this to a public shard, but would not have the time to take on a full-fledged re-implementation of the original Ruby vcr gem, hence the thought to contribute into the webmock framework where it's likely to be better maintained and grow with the community support.

straight-shoota commented 5 years ago

IMO this should be a separate shard, building on top of webmock.

mwlang commented 5 years ago

I discovered while reading through other issues on this shard that "hi8" exists and probably should close this issue on account. See #20 for more info. Still open to discussion about adding this kind of functionality. For me, it's hard to say whether it's worth a separate shard or not. If all Crystal shards were easier to find out in the wild, then I'd definitely agree.