Skip to content

Commit

Permalink
add color to output
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph0x45 committed Feb 5, 2023
1 parent 17c0ea3 commit b96c96d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
34 changes: 33 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
colored = "2.0.0"
reqwest = "0.11.14"
serde_json = "1.0.91"
tokio = { version = "1.25.0", features = ["full"] }
32 changes: 32 additions & 0 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,38 @@ pub async fn interpreter( certus_file: &str ){
return
}
}
// println!("Here");
//Retrieve expected HTTP status
let mut read_status = false;
for line in content.lines(){
if line.trim()=="[EXPECT]" {
read_status = true;
continue;
}
if line.trim().starts_with("#") {
continue;
}
if line.trim()==""{
continue;
}
if line.trim().starts_with("HTTP") & read_status{
let splitted : Vec<&str> = line.split(" ").collect();
if splitted.len()==2{
match u16::from_str(splitted[1]) {
Ok(status)=>{
certus_test.expected_status = status;
break;
},
Err(_)=>{
println!("Invalid HTTP status code {}", splitted[1]);
return ;
}
}
}
println!("Failed to parse expected HTTP status code");
return;
}
}
run(certus_test).await;
},
Err(_)=>{
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate colored;
mod interpreter;
mod runner;
mod utils;
Expand All @@ -6,6 +7,7 @@ use std::path::Path;
use tokio;
use utils::write_template;
use interpreter::interpreter;
use colored::*;

#[tokio::main]
async fn main() -> () {
Expand Down
9 changes: 8 additions & 1 deletion src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::CertusTest;
use reqwest::Client;
use colored::*;

pub async fn run( test: CertusTest ){
let reqwest_client = Client::new();
Expand All @@ -11,7 +12,13 @@ pub async fn run( test: CertusTest ){
let response = certus_request.send().await;
match response {
Ok(response)=>{
let response_status = response.status();
let response_status = response.status().as_u16();
if response_status!=test.expected_status {
let error_message = std::format!("Test failed: Got HTTP {} but expected HTTP {}", response_status, test.expected_status).color("red");
println!("{}", error_message);
return
}

let response_headers = response.headers();
let response_text = &response.text().await;
match response_text {
Expand Down
15 changes: 13 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ pub struct CertusTest{
pub method: Method,
pub endpoint: String,
pub headers: HeaderMap,
pub body: Value
pub body: Value,
pub expected_status: u16,
pub expected_headers: HeaderMap,
pub expected_body: Value,

}

impl Default for CertusTest {
fn default() -> Self {
CertusTest { method: Method::from_str("GET").unwrap(), endpoint: "".to_string(), headers: HeaderMap::new(), body: Value::default() }
CertusTest {
method: Method::from_str("GET").unwrap(),
endpoint: "".to_string(),
headers: HeaderMap::new(),
body: Value::default(),
expected_status:200,
expected_headers: HeaderMap::new(),
expected_body: Value::default()
}
}
}

0 comments on commit b96c96d

Please sign in to comment.