Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search index file in sub directory #7

Merged
merged 5 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.dwarf
shard.lock
/dev/
.tool-versions
3 changes: 0 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions spec/replay/datasource/filesystem_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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": ""
}
}
1 change: 1 addition & 0 deletions spec/replay/datasource/filesystem_spec/replies/db0da_1920c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello!
Original file line number Diff line number Diff line change
@@ -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}
4 changes: 2 additions & 2 deletions spec/support/mocks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/replay/datasource/filesystem.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand Down