-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add config with papyrus_config
- Loading branch information
1 parent
81ae9f4
commit 1c03303
Showing
6 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//use cool_asserts::assert_matches; | ||
use crate::GatewayConfig; | ||
use assert_matches::assert_matches; | ||
use clap::Command; | ||
use papyrus_config::loading::load_and_process_config; | ||
use std::fs::File; | ||
use std::path::Path; | ||
use validator::Validate; | ||
|
||
const TEST_FILES_FOLDER: &str = "./src/json_files_for_testing"; | ||
const CONFIG_FILE: &str = "gateway_config.json"; | ||
|
||
fn get_config_file(file_name: &str) -> Result<GatewayConfig, papyrus_config::ConfigError> { | ||
let config_file = File::open(Path::new(TEST_FILES_FOLDER).join(file_name)).unwrap(); | ||
load_and_process_config::<GatewayConfig>(config_file, Command::new(""), vec![]) | ||
} | ||
|
||
#[test] | ||
fn test_valid_config() { | ||
// Read the valid config file and validate its content. | ||
let expected_config = GatewayConfig { | ||
bind_address: String::from("0.0.0.0:8080"), | ||
}; | ||
let loaded_config = get_config_file(CONFIG_FILE).unwrap(); | ||
|
||
assert!(loaded_config.validate().is_ok()); | ||
assert_eq!(loaded_config, expected_config); | ||
} | ||
|
||
#[test] | ||
fn test_address_validator() { | ||
// Read the valid config file and check that the validator finds no errors. | ||
let mut config = get_config_file(CONFIG_FILE).unwrap(); | ||
assert!(config.validate().is_ok()); | ||
|
||
// Invalidate the bind address and check that the validator finds an error. | ||
config.bind_address = String::from("bad_address"); | ||
assert_matches!(config.validate(), Err(e) => { | ||
assert_eq!(e.field_errors()["bind_address"][0].code, "Invalid Socket address."); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/gateway/src/json_files_for_testing/gateway_config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"bind_address": { | ||
"description": "IP:PORT of the gateway.", | ||
"value": "0.0.0.0:8080", | ||
"privacy": "Public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters