From 96089573143c21e7616c30339776ed288dfd68e5 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Sat, 26 Nov 2022 00:24:49 +0900 Subject: [PATCH 1/5] Search index file into sub directory --- src/replay/datasource/filesystem.cr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/replay/datasource/filesystem.cr b/src/replay/datasource/filesystem.cr index 053e7b3..f99299d 100644 --- a/src/replay/datasource/filesystem.cr +++ b/src/replay/datasource/filesystem.cr @@ -27,7 +27,7 @@ class FileSystemDatasource def get(request : Request) : Record | NoIndexFound | CorruptedReplayResource | NoResourceFound meta_index = request.base_index - index_files = Dir["#{@index_file_dir}/#{meta_index}_*"] + index_files = Dir["#{@index_file_dir}/**/#{meta_index}_*"] if index_files.empty? Replay::Log.debug { "No index_file avairable." } NoIndexFound.new(meta_index) @@ -37,6 +37,7 @@ class FileSystemDatasource candidate == request end found_index_file.try do |found| + Replay::Log.debug { "Found index_file path: #{found}" } 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? From c4d10526fe9e86b601079d4a82cf56328ff1291d Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Sun, 27 Nov 2022 15:15:09 +0900 Subject: [PATCH 2/5] Add git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9b44e61..dd88a6e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.dwarf shard.lock /dev/ +.tool-versions From 25d51c73104bc6dd0c084ef0548940eac9f1de8b Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Sun, 27 Nov 2022 15:15:25 +0900 Subject: [PATCH 3/5] Remove unused --- shard.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/shard.yml b/shard.yml index 724b8a6..c702180 100644 --- a/shard.yml +++ b/shard.yml @@ -15,9 +15,6 @@ development_dependencies: ameba: github: crystal-ameba/ameba version: ~> 1.3 - webmock: - github: manastech/webmock.cr - version: 0.14.0 crystal: ~> 1.6 From af2facb5ca0ee72d039d795b2ccd8c80c8838893 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Sun, 27 Nov 2022 15:15:43 +0900 Subject: [PATCH 4/5] Add spec --- spec/replay/datasource/filesystem_spec.cr | 14 +++++++++ .../indexes/another_case/db0da_1920c | 29 +++++++++++++++++++ .../filesystem_spec/replies/db0da_1920c | 1 + .../replies/db0da_1920c_headers | 1 + 4 files changed, 45 insertions(+) create mode 100644 spec/replay/datasource/filesystem_spec/indexes/another_case/db0da_1920c create mode 100644 spec/replay/datasource/filesystem_spec/replies/db0da_1920c create mode 100644 spec/replay/datasource/filesystem_spec/replies/db0da_1920c_headers diff --git a/spec/replay/datasource/filesystem_spec.cr b/spec/replay/datasource/filesystem_spec.cr index cf70c8c..57fb81e 100644 --- a/spec/replay/datasource/filesystem_spec.cr +++ b/spec/replay/datasource/filesystem_spec.cr @@ -31,6 +31,20 @@ describe FileSystemDatasource do datasource = FileSystemDatasource.new(test_file_dir, records, requests) actual = datasource.get(request) actual.should eq(record) + actual.as(Record).entity.should eq("baz=qux") + end + + it "can retrive resources on sub dir" do + test_file_dir = "#{FileUtils.pwd}/spec/replay/datasource/filesystem_spec" + request = MockRequest.new("db0da", "db0da_1920c", {"baz" => "qux"}) + requests = MockRequests.new(request) + record = MockRecord.new({"foo" => "bar"}, "quux=corge") + records = MockRecords.new(record) + + datasource = FileSystemDatasource.new(test_file_dir, records, requests) + actual = datasource.get(request) + actual.should eq(record) + actual.as(Record).entity.should eq("quux=corge") end it "can not retrive resources not avairable" do diff --git a/spec/replay/datasource/filesystem_spec/indexes/another_case/db0da_1920c b/spec/replay/datasource/filesystem_spec/indexes/another_case/db0da_1920c new file mode 100644 index 0000000..764f5a4 --- /dev/null +++ b/spec/replay/datasource/filesystem_spec/indexes/another_case/db0da_1920c @@ -0,0 +1,29 @@ +{ + "id": "1770a0838932637cb823570c908552e4", + "host": "hello.replay.org", + "method": "GET", + "path": "/another", + "indexed": { + "headers": {}, + "params": {}, + "body": "" + }, + "not_indexed": { + "headers": { + "Host": [ + "hello.replay.org" + ], + "User-Agent": [ + "baz/1" + ], + "Accept": [ + "*/*" + ] + }, + "params": { + "q": "a", + "b": "c" + }, + "body": "" + } +} diff --git a/spec/replay/datasource/filesystem_spec/replies/db0da_1920c b/spec/replay/datasource/filesystem_spec/replies/db0da_1920c new file mode 100644 index 0000000..940a58c --- /dev/null +++ b/spec/replay/datasource/filesystem_spec/replies/db0da_1920c @@ -0,0 +1 @@ +Hello! diff --git a/spec/replay/datasource/filesystem_spec/replies/db0da_1920c_headers b/spec/replay/datasource/filesystem_spec/replies/db0da_1920c_headers new file mode 100644 index 0000000..b35137b --- /dev/null +++ b/spec/replay/datasource/filesystem_spec/replies/db0da_1920c_headers @@ -0,0 +1 @@ +{"headers":{"Content-Type":"text/plain","Date":"Fri, 09 Sep 2022 15:16:10 GMT","Server":"test_server","Cache-Control":"private","Set-Cookie":"baz=qux"},"status":200} From b638dca2fe168b308b662c9c8a7e336568f04990 Mon Sep 17 00:00:00 2001 From: y2k2mt Date: Sun, 27 Nov 2022 15:15:56 +0900 Subject: [PATCH 5/5] ameba --- spec/support/mocks.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/support/mocks.cr b/spec/support/mocks.cr index 18c828f..46806ca 100644 --- a/spec/support/mocks.cr +++ b/spec/support/mocks.cr @@ -19,8 +19,8 @@ struct MockRequests @expected_request || RequestError.new end - def from(request_json : JSON::Any) - @expected_request.not_nil! + def from(request_json : JSON::Any) : Request + @expected_request || raise "Expecred request is not set." end end