Skip to content

Commit

Permalink
Merge pull request #5 from y2k2mt/fix/record-array-header
Browse files Browse the repository at this point in the history
Recorded array header is not replaying correctly
  • Loading branch information
y2k2mt authored Nov 23, 2022
2 parents 7277fd3 + b4196ca commit 4f412f9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/replay/http/record_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 20 additions & 9 deletions src/replay/http/record.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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}" }
Expand Down
16 changes: 12 additions & 4 deletions src/replay/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4f412f9

Please sign in to comment.