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 diff --git a/spec/replay/http/record_spec.cr b/spec/replay/http/record_spec.cr index c0cf748..109f2ab 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;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..bdd3a18 100644 --- a/src/replay/http/record.cr +++ b/src/replay/http/record.cr @@ -11,21 +11,32 @@ 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) 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}" } 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