Possible test cases

I wanted to post a json file to an http server. I am using following code to achieve this functionality. I wonder, if some one can help me figure out, what may be the possible test cases in this case please.

Code to post a Json file to a http server using serde_json and reqwest

use error_chain::error_chain;
use serde_json:: {Value};
use std::env;
use std::fs;

error_chain! {
     foreign_links {
         HttpRequest(reqwest::Error);
         IoError(::std::io::Error);
     }
 }
 #[tokio::main]

async fn main() -> Result<()> {
    //Get file name from command line argument
    let args: Vec<String> = env::args().collect();    
    let file_name = &args[1];
    println!("The file name is: {}", file_name);

    //First load the file into a string
    let text = fs::read_to_string(file_name)?;
    println!("The text of the file is {}", text);

    //Parse the string into a dynamically-typed JSON structure.
    let json_file= serde_json::from_str::<Value>(&text).unwrap();
    

    //Post the file to an http server     
    let client = reqwest::Client::new();
    let res = client.post("https://paste.rs")
        .json(&json_file)
        .send()
        .await?;

    //Get and print responce values
     let response_text = res.text().await?;
     println!("Your paste is located at: {}",response_text );   
     Ok(())            
}

I typically suggest that when you are simply gluing together APIs from other libraries, then you don't need to test anything. Any test cases you would write would basically end up testing the functionality of the underlying libraries, which (1) are hopefully already tested, and (2) you can't influence even if they don't behave as advertised.

2 Likes

you are right but, your code always use some predefined library, you perform some action on it. So there is always a room for testing.

For the code you've shown, the only test of any value would be an integration test showing that this works as intended end-to-end (including handling the case of the wrong number of arguments supplied).

All this code is doing is integrating several libraries together, so all that exists to test is the integration - other tests belong to the libraries, not to this code.

1 Like

Oky. Thanks. Got your point

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.