From c55e854eec76b7eab6501cfc959e22ae27117e89 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Tue, 22 Nov 2022 22:42:27 +0900 Subject: [PATCH 1/5] Add spec for problem --- spec/replay/http/record_spec.cr | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/replay/http/record_spec.cr b/spec/replay/http/record_spec.cr index c0cf748..2a85e6f 100644 --- a/spec/replay/http/record_spec.cr +++ b/spec/replay/http/record_spec.cr @@ -30,4 +30,18 @@ describe HTTPRecord do actual_metadatas["headers"]["Set-Cookie"].should eq("foo=bar") actual_metadatas["status"].should eq(201) end + it "can get properties via json formatted response contains array" do + body = IO::Memory.new "HELLO" + header = IO::Memory.new "{\"headers\":{\"Content-Type\":\"text/plain\",\"Server\":\"test_server\",\"Cookie\":[\"foo=bar\",\"baz=qux\"]},\"status\":201}" + request = MockRequest.new("db0da", "db0da_1770a", {"foo" => "bar"}) + record = HTTPRecord.new(header, body, request) + # Write to response + actual_body = record.entity + actual_body.should eq("HELLO") + actual_metadatas = record.metadatas + actual_metadatas["headers"]["Content-Type"].should eq("text/plain") + actual_metadatas["headers"]["Server"].should eq("test_server") + actual_metadatas["headers"]["Cookie"].should eq(["foo=bar","bar=qux"]) + actual_metadatas["status"].should eq(201) + end end From 2de9aea01a8a289505649b6186c00b01fa577263 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Tue, 22 Nov 2022 22:52:56 +0900 Subject: [PATCH 2/5] Fix: parse collectly --- spec/replay/http/record_spec.cr | 2 +- src/replay/http/record.cr | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spec/replay/http/record_spec.cr b/spec/replay/http/record_spec.cr index 2a85e6f..109f2ab 100644 --- a/spec/replay/http/record_spec.cr +++ b/spec/replay/http/record_spec.cr @@ -41,7 +41,7 @@ describe HTTPRecord do actual_metadatas = record.metadatas actual_metadatas["headers"]["Content-Type"].should eq("text/plain") actual_metadatas["headers"]["Server"].should eq("test_server") - actual_metadatas["headers"]["Cookie"].should eq(["foo=bar","bar=qux"]) + actual_metadatas["headers"]["Cookie"].should eq("foo=bar;baz=qux") actual_metadatas["status"].should eq(201) end end diff --git a/src/replay/http/record.cr b/src/replay/http/record.cr index 97071dd..2ddb01a 100644 --- a/src/replay/http/record.cr +++ b/src/replay/http/record.cr @@ -23,9 +23,16 @@ class HTTPRecord def initialize(headers_content : IO, body_content : IO, request : Request) header = JSON.parse(headers_content.gets_to_end) response_headers = HTTP::Headers.new - header["headers"].as_h.map do |k, v| - if k != "status" + header["headers"].as_h.reject do |k,_| + k == "status" + end.map do |k, v| + case v + when .as_s? response_headers[k] = v.as_s + when .as_a? + response_headers[k] = v.as_a.join(";") + else + # Do nothing end end Replay::Log.debug { "Recorded response headers: #{response_headers}" } From 6b93a51fc7ef61a56e1ff78e6ee8c147873b6300 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Tue, 22 Nov 2022 22:56:19 +0900 Subject: [PATCH 3/5] Upgrade --- shard.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shard.yml b/shard.yml index 968df7c..724b8a6 100644 --- a/shard.yml +++ b/shard.yml @@ -14,11 +14,11 @@ executables: development_dependencies: ameba: github: crystal-ameba/ameba - version: ~> 1.0.0 + version: ~> 1.3 webmock: github: manastech/webmock.cr version: 0.14.0 -crystal: 1.5.0 +crystal: ~> 1.6 license: MIT From da1a74e02cb5bf655c11218a0705f11a5b61b82d Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Tue, 22 Nov 2022 22:58:33 +0900 Subject: [PATCH 4/5] Format --- src/replay/http/record.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/replay/http/record.cr b/src/replay/http/record.cr index 2ddb01a..e7a7b62 100644 --- a/src/replay/http/record.cr +++ b/src/replay/http/record.cr @@ -23,7 +23,7 @@ class HTTPRecord def initialize(headers_content : IO, body_content : IO, request : Request) header = JSON.parse(headers_content.gets_to_end) response_headers = HTTP::Headers.new - header["headers"].as_h.reject do |k,_| + header["headers"].as_h.reject do |k, _| k == "status" end.map do |k, v| case v From b4196ca1dda754de9ff9c948bd9078a407800176 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Wed, 23 Nov 2022 17:53:21 +0900 Subject: [PATCH 5/5] Fix:ameba --- src/replay/http/record.cr | 18 +++++++++++------- src/replay/http/request.cr | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/replay/http/record.cr b/src/replay/http/record.cr index e7a7b62..bdd3a18 100644 --- a/src/replay/http/record.cr +++ b/src/replay/http/record.cr @@ -11,13 +11,17 @@ class HTTPRecord end def initialize(client_response : HTTP::Client::Response, request : Request) - initialize( - headers: client_response.headers, - body: client_response.body, - response_status: client_response.status_code, - response: client_response.not_nil!, - request: request, - ) + if client_response + initialize( + headers: client_response.headers, + body: client_response.body, + response_status: client_response.status_code, + response: client_response, + request: request, + ) + else + raise "Client response is not avairable for record" + end end def initialize(headers_content : IO, body_content : IO, request : Request) diff --git a/src/replay/http/request.cr b/src/replay/http/request.cr index 10fa5cd..3951e8b 100644 --- a/src/replay/http/request.cr +++ b/src/replay/http/request.cr @@ -12,11 +12,14 @@ class HTTPRequest @params : Hash(String, String) def initialize(@http_request : HTTP::Request, @base_uri : URI) - base_uri.host.try do |host| - http_request.headers["Host"] = host + maybe_host = base_uri.host + if maybe_host + http_request.headers["Host"] = maybe_host + @host_name = maybe_host + else + raise "Request URI is collapsed : #{base_uri}" end @id = Random::Secure.hex - @host_name = base_uri.host.not_nil! @path = @http_request.path @method = @http_request.method @headers = @http_request.headers.to_h @@ -45,7 +48,12 @@ class HTTPRequest ) @id = id @base_uri = base_uri - @host_name = base_uri.host.not_nil! + maybe_host = base_uri.host + if maybe_host + @host_name = maybe_host + else + raise "Request URI is collapsed : #{base_uri}" + end @path = path @method = method @headers = headers