Skip to content

Commit

Permalink
Support for absolute file paths in body_from_file
Browse files Browse the repository at this point in the history
  • Loading branch information
alexliesenfeld committed Oct 25, 2020
1 parent 8b8a5a6 commit e9f3ed5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Version 0.5.2
- Updated dependencies to newest version.
- Removed dependency version fixation from v0.5.1.
- `Mock::return_body_from_file` and `Then::body_from_file` now accept absolute and relative file paths.

## Version 0.5.1
- Updated dependency to futures-util to fix compile errors.
Expand Down
24 changes: 12 additions & 12 deletions src/api/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,9 @@ impl Mock {

/// Sets the HTTP response body that will be returned by the mock server.
///
/// * `body` - The response body content.
/// * `resource_file_path` - The file path to the file with the response body content. The file
/// path can either be absolute or relative to the project root
/// directory.
///
/// ## Example:
/// ```
Expand All @@ -1227,21 +1229,19 @@ impl Mock {
/// assert_eq!(response.status(), 200);
/// assert_eq!(response.text().unwrap(), "ohi!");
/// ```
pub fn return_body_from_file<S: Into<String>>(
mut self,
relative_test_resource_path: S,
) -> Self {
let rel_path = relative_test_resource_path.into();
let abs_path = match Path::new(&rel_path).is_absolute() {
true => PathBuf::from(rel_path),
false => get_test_resource_file_path(&rel_path).expect(&format!(
pub fn return_body_from_file<S: Into<String>>(mut self, resource_file_path: S) -> Self {
let resource_file_path = resource_file_path.into();
let path = Path::new(&resource_file_path);
let absolute_path = match path.is_absolute() {
true => path.to_path_buf(),
false => get_test_resource_file_path(&resource_file_path).expect(&format!(
"Cannot create absolute path from string '{}'",
&rel_path
&resource_file_path
)),
};
let content = read_file(&abs_path).expect(&format!(
let content = read_file(&absolute_path).expect(&format!(
"Cannot read from file {}",
abs_path.to_str().expect("Invalid OS path")
absolute_path.to_str().expect("Invalid OS path")
));
self.return_body(content)
}
Expand Down

0 comments on commit e9f3ed5

Please sign in to comment.