Skip to content

Commit

Permalink
support HEAD param in http interpretation
Browse files Browse the repository at this point in the history
  • Loading branch information
emanueldima committed Mar 13, 2024
1 parent f6378e8 commit f54db3b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Print all services with inaccessible images in a Docker compose file:
# shell
hial './config.yaml^yaml/services/*[ /image^split[":"]/[0]^http[HEAD]@status/code>=400 ]'
# 🚧 todo: split interpretation (regex[( ([^:]*): )*]
# 🚧 todo: HEAD param for http
```

```rust
Expand Down Expand Up @@ -79,7 +78,6 @@ Change the user's docker configuration:
```bash
# shell
hial '~/.docker/config.json^json/auths/docker.io/username = "newuser"'
# 🚧 todo: support ~
```
```rust
// rust
Expand Down
12 changes: 7 additions & 5 deletions src/interpretations/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ impl Cell {
let value_cow = value.as_cow_str();
let url = value_cow.as_ref();

let response = Client::builder()
.user_agent("hial")
.build()?
.get(url)
.send()?;
let client = Client::builder().user_agent("hial").build()?;
let request = if params.contains_key(&Value::Str("HEAD")) {
client.head(url)
} else {
client.get(url)
};
let response = request.send()?;

let mut headers = IndexMap::<String, Vec<String>>::new();
for (k, v) in response.headers().iter() {
Expand Down
18 changes: 18 additions & 0 deletions src/tests/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::{api::*, pprint::pprint, utils::log::set_verbose};

#[test]
fn test_http_basic() -> Res<()> {
set_verbose(true);

let cell = Xell::new("http://api.github.com^http");
pprint(&cell, 0, 0);
assert_eq!(cell.to("@status/code").read().value()?, Value::from(200));
assert!(cell.read().value()?.as_cow_str().len() > 10);

let cell = Xell::new("http://api.github.com^http[HEAD]");
pprint(&cell, 0, 0);
assert_eq!(cell.to("@status/code").read().value()?, Value::from(200));
assert!(cell.read().value()?.as_cow_str().len() == 0);

Ok(())
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod fs;
mod http;
mod ideals;
mod json;
mod nested;
Expand Down

0 comments on commit f54db3b

Please sign in to comment.