From 44f387d2eee291383cb2c181c732c9d27c5f3948 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Wed, 4 Jan 2023 23:23:10 +0900 Subject: [PATCH 1/3] Enable to set raw json body --- spec/replay/http/request_spec.cr | 50 ++++++++++++++++++++++++++++++++ src/replay/http/request.cr | 3 +- 2 files changed, 52 insertions(+), 1 deletion(-) 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/replay/http/request.cr b/src/replay/http/request.cr index af205fe..cf626d1 100644 --- a/src/replay/http/request.cr +++ b/src/replay/http/request.cr @@ -121,7 +121,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{ |b| b.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 From 51cff6e7fda40d5fd5f229647f9f60aeaa1b4fa2 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Sun, 15 Jan 2023 23:22:41 +0900 Subject: [PATCH 2/3] Recording JSON as object --- src/replay/http/request.cr | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/replay/http/request.cr b/src/replay/http/request.cr index cf626d1..715bdb3 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 @@ -122,7 +126,7 @@ class RecordedHTTPRequest raise "Request URI is collapsed : #{base_uri}" end body_condition = request_json["indexed"]["body"] - @body = body_condition.as_h?.try{ |b| b.to_json } || body_condition.as_s + @body = body_condition.as_h?.try { |b| b.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 From 6403f27f7bde30f9ef1d7bc15fd1211cbe885d71 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Mon, 6 Feb 2023 23:15:39 +0900 Subject: [PATCH 3/3] Fix: Redundant parentheses --- src/cli.cr | 2 +- src/replay/datasource/filesystem.cr | 8 ++++---- src/replay/http/record.cr | 2 +- src/replay/http/request.cr | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) 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 715bdb3..cf71a62 100644 --- a/src/replay/http/request.cr +++ b/src/replay/http/request.cr @@ -87,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 @@ -126,7 +126,7 @@ class RecordedHTTPRequest raise "Request URI is collapsed : #{base_uri}" end body_condition = request_json["indexed"]["body"] - @body = body_condition.as_h?.try { |b| b.to_json } || body_condition.as_s + @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 @@ -147,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 @@ -228,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