Skip to content

Commit

Permalink
TEST: HTTP POST schema
Browse files Browse the repository at this point in the history
  • Loading branch information
slivingston committed Oct 29, 2024
1 parent c386802 commit df350aa
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/bin/rrhttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use clap::Arg;
#[macro_use]
extern crate log;

#[macro_use]
extern crate serde_json;
extern crate serde;
use serde::Deserialize;

Expand Down Expand Up @@ -562,6 +564,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

#[cfg(test)]
mod tests {
use std::io::Write;

use tempfile::NamedTempFile;

use super::{Config, ConfigMode, HttpVerb, Request, RequestRule};

#[test]
Expand Down Expand Up @@ -613,6 +619,73 @@ mod tests {
assert!(!config.is_valid(&req));
}

#[test]
fn test_post_schema() {
let config_data = "---
default: block
rules:
- verb: POST
uri: /api/head
has_body: true
schema:
- name: Pitch
optional: false
type: float
range: [-40, 0]
- name: Roll
optional: false
type: float
range: [-15, 15]
- name: Yaw
optional: false
type: float
range: [-75, 75]
- name: Velocity
optional: false
type: int
range: [1, 75]
";
let mut config_file = NamedTempFile::new().unwrap();
write!(config_file, "{}", config_data).unwrap();
let config = Config::new_from_file(&config_file.path().to_string_lossy()).unwrap();

assert!(!config.is_valid(&Request {
verb: HttpVerb::Get,
uri: "/".into(),
body: None,
query: None,
}));

let mut req = Request {
verb: HttpVerb::Post,
uri: "/api/head".into(),
body: None,
query: None,
};
assert!(!config.is_valid(&req));

req.body = Some(json!({
"Pitch": 0,
"Roll": 0,
"Yaw": 0,
"Velocity": 75,
}));
assert!(config.is_valid(&req));

req.body = Some(json!({
"Velocity": 75,
}));
assert!(!config.is_valid(&req));

req.body = Some(json!({
"Pitch": "noise",
"Roll": 0,
"Yaw": 0,
"Velocity": 75,
}));
assert!(!config.is_valid(&req));
}

#[test]
fn test_query_parsing() {
let get_example = "GET /api/cameras/rgb?Width=800&Height=600&Base64=true HTTP/1.1\r\nHost: 127.0.0.1:50352\r\nUser-Agent: curl/8.7.1\r\nAccept: */*\r\n\r\n";
Expand Down

0 comments on commit df350aa

Please sign in to comment.