From 27e3095204f6ae08aacdd6f8f78dca6d00c73ca4 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 12:39:58 +0200 Subject: [PATCH 01/15] Allow asserting on the requests made --- lib/webmock/request_registry.rb | 24 ++++++++++++++++++++++++ spec/unit/request_registry_spec.rb | 11 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/webmock/request_registry.rb b/lib/webmock/request_registry.rb index e4618d3ea..c699b0238 100644 --- a/lib/webmock/request_registry.rb +++ b/lib/webmock/request_registry.rb @@ -7,6 +7,19 @@ class RequestRegistry attr_accessor :requested_signatures + class Request + attr_accessor :method, :uri + + def self.from_webmock_request_signature(request_signature) + new(method: request_signature.method, uri: request_signature.uri) + end + + def initialize(method:, uri:) + @method = method + @uri = uri + end + end + def initialize reset! end @@ -21,6 +34,17 @@ def times_executed(request_pattern) end.inject(0) { |sum, (_, times_executed)| sum + times_executed } end + def requests_made + to_a + end + + def to_a + requested_signatures. + hash. + flat_map { |request_signature, number_of_requests| [request_signature] * number_of_requests }. + map { |request_signature| Request.from_webmock_request_signature(request_signature) } + end + def to_s if requested_signatures.hash.empty? "No requests were made." diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index 2ed577062..ed888b8df 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -92,4 +92,15 @@ end end + describe "requests_made" do + it "should return an array of request signatures" do + WebMock::RequestRegistry.instance.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com")) + WebMock::RequestRegistry.instance.requested_signatures.put(WebMock::RequestSignature.new(:put, "www.example.org")) + expect(WebMock::RequestRegistry.instance.requests_made.count).to eq(2) + expect(WebMock::RequestRegistry.instance.requests_made[0].method).to eq(:get) + expect(WebMock::RequestRegistry.instance.requests_made[0].uri).to eq(Addressable::URI.parse("http://www.example.com/")) + expect(WebMock::RequestRegistry.instance.requests_made[1].method).to eq(:put) + expect(WebMock::RequestRegistry.instance.requests_made[1].uri).to eq(Addressable::URI.parse("http://www.example.org/")) + end + end end From 324507968c280348f4c42f819257f693818e3602 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 12:40:31 +0200 Subject: [PATCH 02/15] Better name --- spec/unit/request_registry_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index ed888b8df..7ef1056c0 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -93,7 +93,7 @@ end describe "requests_made" do - it "should return an array of request signatures" do + it "returns the requests made" do WebMock::RequestRegistry.instance.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com")) WebMock::RequestRegistry.instance.requested_signatures.put(WebMock::RequestSignature.new(:put, "www.example.org")) expect(WebMock::RequestRegistry.instance.requests_made.count).to eq(2) From 7f93ba1c357d6e35db39e0ffe28d25fac4f46491 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 12:42:20 +0200 Subject: [PATCH 03/15] Simpler --- spec/unit/request_registry_spec.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index 7ef1056c0..5e41ade7a 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -94,13 +94,14 @@ describe "requests_made" do it "returns the requests made" do - WebMock::RequestRegistry.instance.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com")) - WebMock::RequestRegistry.instance.requested_signatures.put(WebMock::RequestSignature.new(:put, "www.example.org")) - expect(WebMock::RequestRegistry.instance.requests_made.count).to eq(2) - expect(WebMock::RequestRegistry.instance.requests_made[0].method).to eq(:get) - expect(WebMock::RequestRegistry.instance.requests_made[0].uri).to eq(Addressable::URI.parse("http://www.example.com/")) - expect(WebMock::RequestRegistry.instance.requests_made[1].method).to eq(:put) - expect(WebMock::RequestRegistry.instance.requests_made[1].uri).to eq(Addressable::URI.parse("http://www.example.org/")) + request_registry = WebMock::RequestRegistry.instance + request_registry.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com")) + request_registry.requested_signatures.put(WebMock::RequestSignature.new(:put, "www.example.org")) + expect(request_registry.requests_made.count).to eq(2) + expect(request_registry.requests_made[0].method).to eq(:get) + expect(request_registry.requests_made[0].uri).to eq(Addressable::URI.parse("http://www.example.com/")) + expect(request_registry.requests_made[1].method).to eq(:put) + expect(request_registry.requests_made[1].uri).to eq(Addressable::URI.parse("http://www.example.org/")) end end end From a5ab52a305d0e87b006d47d7f22058e4e75a2c1c Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 15:48:09 +0200 Subject: [PATCH 04/15] Use existing hash counter class --- lib/webmock/request_registry.rb | 3 +-- lib/webmock/util/hash_counter.rb | 4 +++- spec/unit/request_registry_spec.rb | 6 ++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/webmock/request_registry.rb b/lib/webmock/request_registry.rb index c699b0238..5f964f412 100644 --- a/lib/webmock/request_registry.rb +++ b/lib/webmock/request_registry.rb @@ -40,8 +40,7 @@ def requests_made def to_a requested_signatures. - hash. - flat_map { |request_signature, number_of_requests| [request_signature] * number_of_requests }. + array. map { |request_signature| Request.from_webmock_request_signature(request_signature) } end diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index ce7c059cf..0a878a10f 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -5,19 +5,21 @@ module WebMock module Util class HashCounter - attr_accessor :hash + attr_accessor :hash, :array def initialize self.hash = Hash.new(0) @order = {} @max = 0 @lock = ::Mutex.new + self.array = [] end def put(key, num=1) @lock.synchronize do hash[key] += num @order[key] = @max += 1 + array << key end end diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index 5e41ade7a..80ac69e00 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -103,5 +103,11 @@ expect(request_registry.requests_made[1].method).to eq(:put) expect(request_registry.requests_made[1].uri).to eq(Addressable::URI.parse("http://www.example.org/")) end + + it "parses the URL of the requests made" do + request_registry = WebMock::RequestRegistry.instance + request_registry.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com")) + expect(request_registry.requests_made.first.uri).to eq(Addressable::URI.parse("http://www.example.com/")) + end end end From 88e96648d0f0a3ca7fbbef22a7085886b7c51d87 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 15:52:35 +0200 Subject: [PATCH 05/15] Add headers into the request --- lib/webmock/request_registry.rb | 7 ++++--- spec/unit/request_registry_spec.rb | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/webmock/request_registry.rb b/lib/webmock/request_registry.rb index 5f964f412..d18c654b6 100644 --- a/lib/webmock/request_registry.rb +++ b/lib/webmock/request_registry.rb @@ -8,15 +8,16 @@ class RequestRegistry attr_accessor :requested_signatures class Request - attr_accessor :method, :uri + attr_accessor :method, :uri, :headers def self.from_webmock_request_signature(request_signature) - new(method: request_signature.method, uri: request_signature.uri) + new(method: request_signature.method, uri: request_signature.uri, headers: request_signature.headers) end - def initialize(method:, uri:) + def initialize(method:, uri:, headers:) @method = method @uri = uri + @headers = headers end end diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index 80ac69e00..8c4c80622 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -104,10 +104,10 @@ expect(request_registry.requests_made[1].uri).to eq(Addressable::URI.parse("http://www.example.org/")) end - it "parses the URL of the requests made" do + it "returns the headers of the request" do request_registry = WebMock::RequestRegistry.instance - request_registry.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com")) - expect(request_registry.requests_made.first.uri).to eq(Addressable::URI.parse("http://www.example.com/")) + request_registry.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com", headers: { 'Content-Type' => 'application/json' })) + expect(request_registry.requests_made.first.headers).to eq({ 'Content-Type' => 'application/json' }) end end end From b366874931eace57eb761b39305843205b4eb894 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 16:02:49 +0200 Subject: [PATCH 06/15] Allow parsing JSON body --- lib/webmock/request_registry.rb | 11 ++++++++--- spec/unit/request_registry_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/webmock/request_registry.rb b/lib/webmock/request_registry.rb index d18c654b6..457511a73 100644 --- a/lib/webmock/request_registry.rb +++ b/lib/webmock/request_registry.rb @@ -8,16 +8,21 @@ class RequestRegistry attr_accessor :requested_signatures class Request - attr_accessor :method, :uri, :headers + attr_accessor :method, :uri, :headers, :body def self.from_webmock_request_signature(request_signature) - new(method: request_signature.method, uri: request_signature.uri, headers: request_signature.headers) + new(method: request_signature.method, uri: request_signature.uri, headers: request_signature.headers, body: request_signature.body) end - def initialize(method:, uri:, headers:) + def initialize(method:, uri:, headers:, body:) @method = method @uri = uri @headers = headers + @body = body + end + + def parsed_body + JSON.parse(body, symbolize_names: true) end end diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index 8c4c80622..f6b62c628 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -109,5 +109,35 @@ request_registry.requested_signatures.put(WebMock::RequestSignature.new(:get, "www.example.com", headers: { 'Content-Type' => 'application/json' })) expect(request_registry.requests_made.first.headers).to eq({ 'Content-Type' => 'application/json' }) end + + context "with a JSON request" do + it "returns the parsed JSON body of the request" do + request_registry = WebMock::RequestRegistry.instance + request_registry.requested_signatures.put(json_request_with_body(a: 1)) + expect(request_registry.requests_made.first.body).to eq({ a: 1 }.to_json) + expect(request_registry.requests_made.first.parsed_body).to eq(a: 1) + end + + context "and invalid JSON in the body" do + it "crashes" do + request_registry = WebMock::RequestRegistry.instance + request_registry.requested_signatures.put(json_request_with_raw_body("{ invalid_json }")) + expect(request_registry.requests_made.first.body).to eq("{ invalid_json }") + expect do + request_registry.requests_made.first.parsed_body + end.to raise_error JSON::ParserError + end + end + end + end + + private + + def json_request_with_body(body) + WebMock::RequestSignature.new(:get, "www.example.com", body: JSON.generate(body)) + end + + def json_request_with_raw_body(body) + WebMock::RequestSignature.new(:get, "www.example.com", body: body) end end From fbf292f57d8f00b5e454aca406eb6db110ecb87c Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 17:37:28 +0200 Subject: [PATCH 07/15] Stop creating lots of new objects for garbage collection reasons --- lib/webmock/request_registry.rb | 22 +--------------------- lib/webmock/request_signature.rb | 4 ++++ 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/lib/webmock/request_registry.rb b/lib/webmock/request_registry.rb index 457511a73..4e2ef3c6e 100644 --- a/lib/webmock/request_registry.rb +++ b/lib/webmock/request_registry.rb @@ -7,25 +7,6 @@ class RequestRegistry attr_accessor :requested_signatures - class Request - attr_accessor :method, :uri, :headers, :body - - def self.from_webmock_request_signature(request_signature) - new(method: request_signature.method, uri: request_signature.uri, headers: request_signature.headers, body: request_signature.body) - end - - def initialize(method:, uri:, headers:, body:) - @method = method - @uri = uri - @headers = headers - @body = body - end - - def parsed_body - JSON.parse(body, symbolize_names: true) - end - end - def initialize reset! end @@ -46,8 +27,7 @@ def requests_made def to_a requested_signatures. - array. - map { |request_signature| Request.from_webmock_request_signature(request_signature) } + array end def to_s diff --git a/lib/webmock/request_signature.rb b/lib/webmock/request_signature.rb index 27e62e97f..7505df484 100644 --- a/lib/webmock/request_signature.rb +++ b/lib/webmock/request_signature.rb @@ -44,6 +44,10 @@ def json_headers? !!(headers&.fetch('Content-Type', nil)&.start_with?('application/json')) end + def parsed_body + JSON.parse(body, symbolize_names: true) + end + private def assign_options(options) From c8815eecd058f24b93acc28dc13fcb7a620608f2 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 12 Sep 2024 17:52:42 +0200 Subject: [PATCH 08/15] Store a reference to existing keys --- lib/webmock/util/hash_counter.rb | 7 ++++++- spec/unit/request_registry_spec.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index 0a878a10f..af93e7313 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -17,9 +17,14 @@ def initialize def put(key, num=1) @lock.synchronize do + if hash.key?(key) + existing_key = hash.keys.find { |k| k.hash == key.hash } + array << existing_key + else + array << key + end hash[key] += num @order[key] = @max += 1 - array << key end end diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index f6b62c628..af1399aed 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -110,6 +110,16 @@ expect(request_registry.requests_made.first.headers).to eq({ 'Content-Type' => 'application/json' }) end + it "only stores references to existing signatures" do + request_registry = WebMock::RequestRegistry.instance + signature = WebMock::RequestSignature.new(:get, "www.example.com") + duplicate_signature = WebMock::RequestSignature.new(:get, "www.example.com") + request_registry.requested_signatures.put(signature) + request_registry.requested_signatures.put(duplicate_signature) + expect(request_registry.requests_made[0].object_id).to eq(signature.object_id) + expect(request_registry.requests_made[1].object_id).to eq(signature.object_id) + end + context "with a JSON request" do it "returns the parsed JSON body of the request" do request_registry = WebMock::RequestRegistry.instance From fd04cbcc4113358c75d56ddbf119aef923906164 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Fri, 13 Sep 2024 17:23:51 +0200 Subject: [PATCH 09/15] Reference existing objects in the hash --- lib/webmock/util/hash_counter.rb | 17 +++++++++++------ spec/unit/request_registry_spec.rb | 10 ++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index af93e7313..d42e4a89b 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -13,21 +13,26 @@ def initialize @max = 0 @lock = ::Mutex.new self.array = [] + @request_object_ids = {} end def put(key, num=1) @lock.synchronize do - if hash.key?(key) - existing_key = hash.keys.find { |k| k.hash == key.hash } - array << existing_key - else - array << key - end + store_to_array(key:, num:) hash[key] += num @order[key] = @max += 1 end end + def store_to_array(key:, num:) + request_object_id = @request_object_ids[key.hash] + request_object_id = key.object_id if request_object_id.nil? + num.times do + array << ObjectSpace._id2ref(request_object_id) + end + @request_object_ids[key.hash] = key.object_id + end + def get(key) @lock.synchronize do hash[key] diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index af1399aed..4d03b7476 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -120,6 +120,16 @@ expect(request_registry.requests_made[1].object_id).to eq(signature.object_id) end + context "when storing the same signature with a count" do + it "adds two references to the signature" do + request_registry = WebMock::RequestRegistry.instance + signature = WebMock::RequestSignature.new(:get, "www.example.com") + request_registry.requested_signatures.put(signature, 2) + expect(request_registry.requests_made[0].object_id).to eq(signature.object_id) + expect(request_registry.requests_made[1].object_id).to eq(signature.object_id) + end + end + context "with a JSON request" do it "returns the parsed JSON body of the request" do request_registry = WebMock::RequestRegistry.instance From 6890a013150d63fd18413d005c89b161fd5d9ac9 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sat, 14 Sep 2024 15:15:38 +0200 Subject: [PATCH 10/15] Deal with recycled objects --- lib/webmock/util/hash_counter.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index d42e4a89b..69859438a 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -29,6 +29,8 @@ def store_to_array(key:, num:) request_object_id = key.object_id if request_object_id.nil? num.times do array << ObjectSpace._id2ref(request_object_id) + rescue RangeError + # Points to an invalid or recycled object so ignore end @request_object_ids[key.hash] = key.object_id end From 2b8a8959f7ce9fb435161a3dfd21e10d05371214 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sat, 14 Sep 2024 15:17:45 +0200 Subject: [PATCH 11/15] Store the key not the hash --- lib/webmock/util/hash_counter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index 69859438a..f74889a33 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -25,14 +25,14 @@ def put(key, num=1) end def store_to_array(key:, num:) - request_object_id = @request_object_ids[key.hash] + request_object_id = @request_object_ids[key] request_object_id = key.object_id if request_object_id.nil? num.times do array << ObjectSpace._id2ref(request_object_id) rescue RangeError # Points to an invalid or recycled object so ignore end - @request_object_ids[key.hash] = key.object_id + @request_object_ids[key] = key.object_id end def get(key) From 3eaead463a8e8074cb17102624d1b7e28b1c9d92 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 Sep 2024 14:48:49 +0100 Subject: [PATCH 12/15] Stop using named arguments because CI fails --- lib/webmock/util/hash_counter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index f74889a33..46bba7b75 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -18,13 +18,13 @@ def initialize def put(key, num=1) @lock.synchronize do - store_to_array(key:, num:) + store_to_array(key, num) hash[key] += num @order[key] = @max += 1 end end - def store_to_array(key:, num:) + def store_to_array(key, num) request_object_id = @request_object_ids[key] request_object_id = key.object_id if request_object_id.nil? num.times do From cb5c40c125486431189accb3039ca6d8b931c2e8 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Sun, 15 Sep 2024 14:58:17 +0100 Subject: [PATCH 13/15] Add helper and coverage --- lib/webmock/api.rb | 4 ++++ spec/unit/api_spec.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/webmock/api.rb b/lib/webmock/api.rb index 4110c095d..26069870c 100644 --- a/lib/webmock/api.rb +++ b/lib/webmock/api.rb @@ -72,6 +72,10 @@ def reset_executed_requests! WebMock::RequestRegistry.instance.reset! end + def requests_made + WebMock::RequestRegistry.instance.requests_made + end + private def convert_uri_method_and_options_to_request_and_options(method, uri, options, &block) diff --git a/spec/unit/api_spec.rb b/spec/unit/api_spec.rb index 15c6ebd69..c17d53f81 100644 --- a/spec/unit/api_spec.rb +++ b/spec/unit/api_spec.rb @@ -172,4 +172,19 @@ def hash_excluding(*args) }.from(1).to(0) end end + + describe '#requests_made' do + subject { WebMock::API.requests_made } + + let(:request_signature) { WebMock::RequestSignature.new(:get, "www.example.com") } + let(:request_pattern) { WebMock::RequestPattern.new(:get, "www.example.com") } + + before do + WebMock::RequestRegistry.instance.requested_signatures.put(request_signature) + end + + it 'returns the number of requests made' do + expect(subject.count).to eq(1) + end + end end From 6705f5313bb47b6279a16a2523904c9954124b49 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Mon, 23 Sep 2024 23:01:45 +0100 Subject: [PATCH 14/15] fix: remove separate feature --- lib/webmock/request_signature.rb | 4 ---- spec/unit/request_registry_spec.rb | 18 +----------------- 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/lib/webmock/request_signature.rb b/lib/webmock/request_signature.rb index 7505df484..27e62e97f 100644 --- a/lib/webmock/request_signature.rb +++ b/lib/webmock/request_signature.rb @@ -44,10 +44,6 @@ def json_headers? !!(headers&.fetch('Content-Type', nil)&.start_with?('application/json')) end - def parsed_body - JSON.parse(body, symbolize_names: true) - end - private def assign_options(options) diff --git a/spec/unit/request_registry_spec.rb b/spec/unit/request_registry_spec.rb index 4d03b7476..fca30b903 100644 --- a/spec/unit/request_registry_spec.rb +++ b/spec/unit/request_registry_spec.rb @@ -131,22 +131,10 @@ end context "with a JSON request" do - it "returns the parsed JSON body of the request" do + it "returns the body of the request" do request_registry = WebMock::RequestRegistry.instance request_registry.requested_signatures.put(json_request_with_body(a: 1)) expect(request_registry.requests_made.first.body).to eq({ a: 1 }.to_json) - expect(request_registry.requests_made.first.parsed_body).to eq(a: 1) - end - - context "and invalid JSON in the body" do - it "crashes" do - request_registry = WebMock::RequestRegistry.instance - request_registry.requested_signatures.put(json_request_with_raw_body("{ invalid_json }")) - expect(request_registry.requests_made.first.body).to eq("{ invalid_json }") - expect do - request_registry.requests_made.first.parsed_body - end.to raise_error JSON::ParserError - end end end end @@ -156,8 +144,4 @@ def json_request_with_body(body) WebMock::RequestSignature.new(:get, "www.example.com", body: JSON.generate(body)) end - - def json_request_with_raw_body(body) - WebMock::RequestSignature.new(:get, "www.example.com", body: body) - end end From 088aa2678f7efb34e910e8b430b54980283b9485 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Mon, 23 Sep 2024 23:02:18 +0100 Subject: [PATCH 15/15] fix: simplify --- lib/webmock/util/hash_counter.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index 46bba7b75..3f2baab40 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -13,7 +13,7 @@ def initialize @max = 0 @lock = ::Mutex.new self.array = [] - @request_object_ids = {} + @request_objects = {} end def put(key, num=1) @@ -25,14 +25,10 @@ def put(key, num=1) end def store_to_array(key, num) - request_object_id = @request_object_ids[key] - request_object_id = key.object_id if request_object_id.nil? + stored_object = @request_objects[key] ||= key num.times do - array << ObjectSpace._id2ref(request_object_id) - rescue RangeError - # Points to an invalid or recycled object so ignore + array << stored_object end - @request_object_ids[key] = key.object_id end def get(key)