Skip to content

Commit

Permalink
Merge pull request #11 from y2k2mt/refactor/improve-json-editability
Browse files Browse the repository at this point in the history
Refactor/improve json editability
  • Loading branch information
y2k2mt authored Feb 6, 2023
2 parents 0fc15b0 + 6403f27 commit 36bb114
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
50 changes: 50 additions & 0 deletions spec/replay/http/request_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,56 @@ describe IncomingHTTPRequest do
(request2.score(request1)).should eq 1
end

it "can compare json proeprties has json body condition" do
headers = HTTP::Headers{
"Content-Type" => "application/json",
}
json = %q{
{
"foo":"bar",
"baz":"qux",
"hoge": {
"fuga": 1,
"moge":"bar",
"baz" : [1,3,2]
}
}
}

recorded = %q{
{
"id": "bda3eaef950904c8ca0e307e45ea88a1",
"host": "base.uri",
"method": "POST",
"path": "/hello",
"indexed": {
"headers": {},
"params": {},
"body": {"hoge":{"fuga":1}}
},
"not_indexed": {
"headers": {
"Host": [
"base.uri"
],
"User-Agent": [
"curl/7.81.0"
],
"Accept": [
"*/*"
]
},
"params": {},
"body": "{\"hoge\":{\"fuga\":1}}"
}
}
}
request = HTTP::Request.new("POST", "/hello", headers, json)
request1 = IncomingHTTPRequest.new(request, URI.parse "http://base.uri")
request2 = RecordedHTTPRequest.new(URI.parse("http://base.uri"), JSON.parse(recorded))
(request2.score(request1)).should eq 1
end

it "can get multiple properties via server request" do
headers = HTTP::Headers{"Content-Type" => "text/plain"}
request = HTTP::Request.new("POST", "/hello", headers, "HELLO")
Expand Down
2 changes: 1 addition & 1 deletion src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if !base_url
end

base_url.try do |url|
if (query_options.empty?)
if query_options.empty?
base_dir.try do |dir|
start_server(Config.new(url, server_port, mode, dir))
end || (
Expand Down
8 changes: 4 additions & 4 deletions src/replay/datasource/filesystem.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class FileSystemDatasource

def persist(request : Request, record : Record) : Record
index_hash = request.id_index
if (!File.directory?(@index_file_dir))
if !File.directory?(@index_file_dir)
Dir.mkdir_p(@index_file_dir)
end
File.open("#{@index_file_dir}/#{index_hash}", "w+")
File.write("#{@index_file_dir}/#{index_hash}", request.metadatas.to_pretty_json)

if (!File.directory?(@reply_file_dir))
if !File.directory?(@reply_file_dir)
Dir.mkdir_p(@reply_file_dir)
end

Expand Down Expand Up @@ -47,7 +47,7 @@ class FileSystemDatasource
found_index = @requests.from(JSON.parse(File.read(found)))
body_file = Dir["#{@reply_file_dir}/#{found_index.id_index}"].first?
header_file = Dir["#{@reply_file_dir}/#{found_index.id_index}_headers"].first?
if (header_file && body_file)
if header_file && body_file
Replay::Log.debug { "Found header_file path: #{header_file}" }
Replay::Log.debug { "Found body_file path: #{body_file}" }
@records.from(File.open(header_file), File.open(body_file), found_index)
Expand All @@ -68,7 +68,7 @@ class FileSystemDatasource
found_index = @requests.from(JSON.parse(File.read(found)))
body_file = Dir["#{@reply_file_dir}/#{found_index.id_index}"].first?
header_file = Dir["#{@reply_file_dir}/#{found_index.id_index}_headers"].first?
if (header_file && body_file)
if header_file && body_file
Replay::Log.debug { "Found header_file path: #{header_file}" }
Replay::Log.debug { "Found body_file path: #{body_file}" }
@records.from(File.open(header_file), File.open(body_file), found_index)
Expand Down
2 changes: 1 addition & 1 deletion src/replay/http/record.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class HTTPRecord

def response(io : IO)
@headers["Transfer-Encoding"]?.try do |encoding|
if (encoding == "chunked")
if encoding == "chunked"
# Chunked transfer encoding not supported.
@headers.delete("Transfer-Encoding")
end
Expand Down
19 changes: 12 additions & 7 deletions src/replay/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class IncomingHTTPRequest
end

def metadatas : JSON::Any
begin
body_condition = JSON.parse(@body)
rescue e
end
JSON.parse(JSON.build do |json|
json.object do
json.field "id", @id
Expand All @@ -72,7 +76,7 @@ class IncomingHTTPRequest
json.object do
json.field "headers", @headers
json.field "params", @params
json.field "body", @body
json.field "body", body_condition || @body
end
end
end
Expand All @@ -83,9 +87,9 @@ class IncomingHTTPRequest
# FIXME:implicit dependency
method_query = query[1]?.try { |q| self.method == q }
path_query = query[2]?.try { |q| self.path.includes?(q) }
if (self.host_name == (query[0]?.try { |q| URI.parse(q).hostname } || "") &&
if self.host_name == query[0]?.try { |q| URI.parse(q).hostname } || "" &&
(method_query == nil || method_query) &&
(path_query == nil || path_query))
(path_query == nil || path_query)
self
else
nil
Expand Down Expand Up @@ -121,7 +125,8 @@ class RecordedHTTPRequest
else
raise "Request URI is collapsed : #{base_uri}"
end
@body = request_json["indexed"]["body"].as_s
body_condition = request_json["indexed"]["body"]
@body = body_condition.as_h?.try(&.to_json) || body_condition.as_s
@params = request_json["indexed"]["params"].as_h.reduce({} of String => String) do |acc, (k, v)|
acc[k] = v.as_s
acc
Expand All @@ -142,7 +147,7 @@ class RecordedHTTPRequest
Replay::Log.debug { "Comparing : #{self.base_index} and #{other.base_index}." }
case other
when IncomingHTTPRequest
if (other.base_index != self.base_index || !match_headers(self, other))
if other.base_index != self.base_index || !match_headers(self, other)
-1
else
# TODO: Plaggable comparators
Expand Down Expand Up @@ -223,9 +228,9 @@ class RecordedHTTPRequest
# FIXME:implicit dependency
method_query = query[1]?.try { |q| self.method == q }
path_query = query[2]?.try { |q| self.path.includes?(q) }
if (self.host_name == (query[0]?.try { |q| URI.parse(q).hostname } || "") &&
if self.host_name == query[0]?.try { |q| URI.parse(q).hostname } || "" &&
(method_query == nil || method_query) &&
(path_query == nil || path_query))
(path_query == nil || path_query)
self
else
nil
Expand Down

0 comments on commit 36bb114

Please sign in to comment.