Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Needs tests, but would like comments. Launch visual diff tool for comparing registered stub bodies and attempted request #138

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/webmock.rb
Expand Up @@ -16,6 +16,7 @@
require 'webmock/http_lib_adapters/typhoeus_hydra_adapter'

require 'webmock/errors'
require 'webmock/registered_stub_differ'

require 'webmock/util/uri'
require 'webmock/util/headers'
Expand Down
4 changes: 2 additions & 2 deletions lib/webmock/errors.rb
@@ -1,7 +1,8 @@
module WebMock

class NetConnectNotAllowedError < StandardError
def initialize(request_signature)
WebMock::RegisteredStubDiffer.new(request_signature.body) if ENV['show_visual_diffs_for_webmock']

text = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
text << "\n\n"
text << stubbing_instructions(request_signature)
Expand Down Expand Up @@ -29,5 +30,4 @@ def stubbing_instructions(request_signature)
text
end
end

end
55 changes: 55 additions & 0 deletions lib/webmock/registered_stub_differ.rb
@@ -0,0 +1,55 @@
module WebMock
class RegisteredStubDiffer
def initialize body
if body
FileUtils.mkdir_p stub_directory

save_attempted_file body

registered = save_registered_stubs
registered.each { |r| diff r }

cleanup registered
end
end

private

def diff registered
`#{diff_tool} #{registered} #{attempted_file} &`
end

def attempted_file
File.join stub_directory, "attempted_stub.txt"
end

def stub_directory
File.join %w(tmp webmock)
end

def cleanup files
File.delete attempted_file, *files
end

def save_attempted_file body
File.open(attempted_file, "w") { |f| f << body }
end

def save_registered_stubs
WebMock::StubRegistry.instance.request_stubs.map.
each_with_index do |stub, index|
file = "tmp/webmock/registered_stub_#{index}"

File.open file, "w" do |f|
f << stub.request_pattern.body_pattern
end

file
end
end

def diff_tool
ENV['diff_tool'] || "opendiff"
end
end
end
4 changes: 4 additions & 0 deletions spec/unit/registered_stub_differ_spec.rb
@@ -0,0 +1,4 @@
require File.expand_path('spec/spec_helper')

describe WebMock::RegisteredStubDiffer do
end