diff options
Diffstat (limited to 'lambda-calcul/rust/src/web.rs')
| -rw-r--r-- | lambda-calcul/rust/src/web.rs | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/lambda-calcul/rust/src/web.rs b/lambda-calcul/rust/src/web.rs index ad6bc03..5bc8c3f 100644 --- a/lambda-calcul/rust/src/web.rs +++ b/lambda-calcul/rust/src/web.rs @@ -96,7 +96,7 @@ enum TestResult { } impl Client { - fn new(url: String, name: String, delay: Duration) -> Self { + fn new(url: String, name: String, delay: Duration, encoding: AstEncoding) -> Self { let id = Uuid::new_v4(); let runner = TestRunner::new_with_rng( Config::default(), @@ -110,7 +110,7 @@ impl Client { runner, results: Vec::new(), delay, - encoding: AstEncoding::Json, + encoding, } } @@ -213,6 +213,7 @@ impl AppState for State { registration.url.clone(), registration.name.clone(), self.base_duration, + registration.encoding.clone().unwrap_or(AstEncoding::Json), ); let id = client.id.to_string(); let client_ref = Arc::new(Mutex::new(client)); @@ -362,17 +363,7 @@ async fn send_one_test(client_m: &Arc<Mutex<Client>>, sleep: Duration) { let encoding = client_m.lock().unwrap().encoding.clone(); - let request = match encoding { - AstEncoding::Json => serde_json::to_string(&input).unwrap(), - _ => input - .iter() - .map(|v| format!("{}", v)) - .collect::<Vec<_>>() - .join("\n"), - }; - let response: Result<Vec<Value>, TestResult> = send_test(&request, &url, sleep) - .await - .and_then(|r| serde_json::from_str(&r).map_err(|e| TestResult::TestFailed(e.to_string()))); + let response: Result<Vec<Value>, TestResult> = send_test(&input, &url, encoding, sleep).await; apply_result(client_m, expected, response); } @@ -391,22 +382,50 @@ fn sleep_time(client_m: &Arc<Mutex<Client>>) -> Duration { client_m.lock().unwrap().time_to_next_test() } -async fn send_test(input: &String, url: &String, timeout: Duration) -> Result<String, TestResult> { - info!("Sending {} to {}", input, url); - let body = input.clone(); +async fn send_test( + input: &Vec<Value>, + url: &String, + encoding: AstEncoding, + timeout: Duration, +) -> Result<Vec<Value>, TestResult> { + info!("Sending {:?} to {}", input, url); + + let body = match encoding { + AstEncoding::Json => serde_json::to_string(&input).unwrap(), + AstEncoding::Sexp => input + .iter() + .map(|v| format!("{}", v)) + .collect::<Vec<_>>() + .join("\n"), + }; + let content_type = match encoding { + AstEncoding::Json => "application/json", + AstEncoding::Sexp => "text/plain", + }; + let response = reqwest::Client::new() .post(url) .timeout(timeout) - .header("content-type", "text/plain") + .header("content-type", content_type) .body(body) .send() .await; + match response { - Ok(response) => { - let body = response.text().await.unwrap(); - info!("Response from {}: {}", url, body); - Ok(body) - } + Ok(response) => match encoding { + AstEncoding::Json => { + let body = response.json().await.unwrap(); + info!("Response from {}: {:?}", url, body); + Ok(body) + } + AstEncoding::Sexp => { + let body = response.text().await.unwrap(); + info!("Response from {}: {}", url, body); + let exprs = + parse_total(&body).map_err(|e| TestResult::TestFailed(e.to_string()))?; + Ok(exprs) + } + }, Err(e) => { info!("Error sending test: {}", e); Err(TestResult::ErrorSendingTest(e.to_string())) @@ -625,6 +644,7 @@ mod app_tests { "http://1.2.3.4".to_string(), "foo".to_string(), Duration::from_secs(10), + AstEncoding::Json, ) } |
