View Source HttpProxy.Utils.File (ExHttpProxy v1.6.0)
This module provides several paths associated with HttpProxy and file operation.
Link to this section Summary
Functions
Structure associated with file paths. Top path is set in config/config.exs
.
mapping_files
and response_files
are created in the top path.
Export json data into path/file
.
Generate json file name with :rand.uniform
Get paths defined on config/"#{Mix.env}.exs"
Get paths in particular directory as list.
Get decoded map data by JSX.decode/2
Link to this section Functions
Structure associated with file paths. Top path is set in config/config.exs
.
mapping_files
and response_files
are created in the top path.
example
Example
iex> HttpProxy.Utils.File.__struct__
%HttpProxy.Utils.File{mapping_files: "mappings", response_files: "__files"}
Export json data into path/file
.
Generate json file name with :rand.uniform
example
Example
iex> HttpProxy.Utils.File.filename(["path", "info", "path"]) |> String.match?(~r(\Apath-info-path.*\.json\z))
true
iex> HttpProxy.Utils.File.filename("path-info-path") |> String.match?(~r(\Apath-info-path.*\.json\z))
true
Get paths defined on config/"#{Mix.env}.exs"
exmaple
Exmaple
iex> HttpProxy.Utils.File.get_export_path
"test/example/mappings"
iex> HttpProxy.Utils.File.get_export_path(8080)
"test/example/8080/mappings"
iex> HttpProxy.Utils.File.get_export_path("8080")
"test/example/8080/mappings"
iex> HttpProxy.Utils.File.get_export_binary_path
"test/example/__files"
iex> HttpProxy.Utils.File.get_export_binary_path(8080)
"test/example/8080/__files"
iex> HttpProxy.Utils.File.get_export_binary_path("8080")
"test/example/8080/__files"
iex> HttpProxy.Utils.File.get_response_path
"test/data/__files"
iex> HttpProxy.Utils.File.get_mapping_path
"test/data/mappings"
@spec get_mapping_path() :: String.t()
@spec get_response_path() :: String.t()
Get paths in particular directory as list.
example
Example
iex> HttpProxy.Utils.File.json_files!("test/data/mappings") |> Enum.sort
["test/data/mappings/sample.json", "test/data/mappings/sample2.json", "test/data/mappings/sample3.json"]
iex> {:ok, paths} = HttpProxy.Utils.File.json_files("test/data/mappings")
iex> paths |> Enum.sort
["test/data/mappings/sample.json", "test/data/mappings/sample2.json", "test/data/mappings/sample3.json"]
Get decoded map data by JSX.decode/2
example
Example
iex> HttpProxy.Utils.File.read_json_file!("test/data/mappings/sample.json")
%{"request" => %{"method" => "GET", "path" => "/request/path", "port" => 8080},
"response" => %{"body" => "<html>hello world</html>", "cookies" => %{},
"headers" => %{"Content-Type" => "text/html; charset=UTF-8", "Server" => "GFE/2.0"}, "status_code" => 200}}
iex> HttpProxy.Utils.File.read_json_file("test/data/mappings/sample.json")
{:ok,
%{"request" => %{"method" => "GET", "path" => "/request/path", "port" => 8080},
"response" => %{"body" => "<html>hello world</html>", "cookies" => %{},
"headers" => %{"Content-Type" => "text/html; charset=UTF-8", "Server" => "GFE/2.0"}, "status_code" => 200}}}