diff --git a/spec/replay/http/request_spec.cr b/spec/replay/http/request_spec.cr index c36941e..e318387 100644 --- a/spec/replay/http/request_spec.cr +++ b/spec/replay/http/request_spec.cr @@ -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") diff --git a/src/cli.cr b/src/cli.cr index c0f7495..e5da855 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -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 || ( diff --git a/src/replay/datasource/filesystem.cr b/src/replay/datasource/filesystem.cr index a506398..32cfc85 100644 --- a/src/replay/datasource/filesystem.cr +++ b/src/replay/datasource/filesystem.cr @@ -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 @@ -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) @@ -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) diff --git a/src/replay/http/record.cr b/src/replay/http/record.cr index 7ad36ee..8934dd9 100644 --- a/src/replay/http/record.cr +++ b/src/replay/http/record.cr @@ -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 diff --git a/src/replay/http/request.cr b/src/replay/http/request.cr index af205fe..cf71a62 100644 --- a/src/replay/http/request.cr +++ b/src/replay/http/request.cr @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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