|
| 1 | +#![feature(todo_macro)] |
| 2 | +#![allow(dead_code, unused_imports, unused_variables)] |
| 3 | + |
| 4 | +use std::env; |
| 5 | + |
| 6 | +use colored::Colorize; |
| 7 | +use http::{Method, StatusCode}; |
| 8 | +use log::{debug, info}; |
| 9 | +use oas3::{ |
| 10 | + conformance::{ |
| 11 | + ConformanceTestSpec, OperationSpec, RequestSpec, ResolvedConformanceTestSpec, ResponseSpec, |
| 12 | + TestAuthorization, TestRequest, |
| 13 | + }, |
| 14 | + validation::Error as ValidationError, |
| 15 | + Error, Spec, |
| 16 | +}; |
| 17 | + |
| 18 | +fn do_request(req: &TestRequest) -> Result<reqwest::Response, reqwest::Error> { |
| 19 | + let base_url = "http://localhost:9000/api/auth/v1"; |
| 20 | + let client = reqwest::Client::new(); |
| 21 | + |
| 22 | + // TODO: add url params |
| 23 | + // TODO: add qs params |
| 24 | + |
| 25 | + let method: reqwest::Method = req.operation.method.as_str().parse().unwrap(); |
| 26 | + let url: String = [base_url, &req.operation.path].concat(); |
| 27 | + |
| 28 | + client |
| 29 | + .request(method, &url) |
| 30 | + .headers(req.headers.clone()) |
| 31 | + .body(req.body.to_vec()) |
| 32 | + .send() |
| 33 | +} |
| 34 | + |
| 35 | +fn do_test(spec: &Spec, test: ResolvedConformanceTestSpec) -> Result<(), ValidationError> { |
| 36 | + debug!("request: {:?}", &test.request); |
| 37 | + debug!("response spec: {:?}", &test.response); |
| 38 | + |
| 39 | + let mut res = do_request(&test.request).unwrap(); |
| 40 | + let body = res.json().map_err(|_| ValidationError::NotJson)?; |
| 41 | + let status = res.status(); |
| 42 | + |
| 43 | + debug!("response: {:?}", &res); |
| 44 | + |
| 45 | + let validation = test.response.validate_status(&res.status())?; |
| 46 | + info!("validation: {:?}", &validation); |
| 47 | + |
| 48 | + let validation = test.response.validate_body(&body)?; |
| 49 | + info!("validation: {:?}", &validation); |
| 50 | + |
| 51 | + Ok(()) |
| 52 | +} |
| 53 | + |
| 54 | +fn do_tests( |
| 55 | + spec: &Spec, |
| 56 | + tests: &[&ConformanceTestSpec], |
| 57 | +) -> Vec<(ConformanceTestSpec, Option<Error>)> { |
| 58 | + let mut results = vec![]; |
| 59 | + |
| 60 | + for test in tests { |
| 61 | + match test.resolve(&spec) { |
| 62 | + Ok(resolved_test) => { |
| 63 | + let validation = do_test(&spec, resolved_test); |
| 64 | + results.push(((*test).clone(), validation.map_err(Error::Validation).err())); |
| 65 | + } |
| 66 | + Err(err) => results.push(((*test).clone(), Some(err))), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + results |
| 71 | +} |
| 72 | + |
| 73 | +fn main() { |
| 74 | + let _ = dotenv::dotenv(); |
| 75 | + pretty_env_logger::init(); |
| 76 | + |
| 77 | + let spec = oas3::from_path("../app/docs/api.yml") |
| 78 | + .expect("api spec parse error"); |
| 79 | + |
| 80 | + let auth_method = TestAuthorization::bearer(env::var("TOKEN").unwrap()); |
| 81 | + |
| 82 | + let test_pass0 = ConformanceTestSpec::new( |
| 83 | + OperationSpec::post("/token"), |
| 84 | + RequestSpec::from_example("application/json", "basic"), |
| 85 | + ResponseSpec::from_schema("200", "application/json"), |
| 86 | + ); |
| 87 | + |
| 88 | + let test_pass1 = ConformanceTestSpec::new( |
| 89 | + OperationSpec::post("/verify"), |
| 90 | + RequestSpec::from_json_example("revoked"), |
| 91 | + ResponseSpec::from_example("200", "application/json", "revoked"), |
| 92 | + ); |
| 93 | + |
| 94 | + let test_fail0 = ConformanceTestSpec::new( |
| 95 | + OperationSpec::operation_id("signin"), |
| 96 | + RequestSpec::from_json_example("unregistered"), |
| 97 | + ResponseSpec::from_example("401", "application/json", "success"), |
| 98 | + ); |
| 99 | + |
| 100 | + let test_fail1 = ConformanceTestSpec::new( |
| 101 | + OperationSpec::operation_id("checkLoggedIn"), |
| 102 | + RequestSpec::empty().with_auth(&auth_method), |
| 103 | + ResponseSpec::from_status("200"), |
| 104 | + ); |
| 105 | + |
| 106 | + let results = do_tests(&spec, &[&test_pass0, &test_pass1, &test_fail0, &test_fail1]); |
| 107 | + |
| 108 | + println!(""); |
| 109 | + print_test_results(results.as_slice()); |
| 110 | + println!(""); |
| 111 | +} |
| 112 | + |
| 113 | +fn print_test_results(results: &[(ConformanceTestSpec, Option<Error>)]) { |
| 114 | + for (test, error) in results { |
| 115 | + let mut msg = vec![]; |
| 116 | + |
| 117 | + let op = &test.operation; |
| 118 | + |
| 119 | + if error.is_some() { |
| 120 | + msg.push("❌ Err".red()); |
| 121 | + } else { |
| 122 | + msg.push("✅ Ok ".green()); |
| 123 | + } |
| 124 | + |
| 125 | + msg.push(" | ".normal()); |
| 126 | + msg.push(format!("{}", &op).normal()); |
| 127 | + |
| 128 | + if let Some(ref err) = error { |
| 129 | + msg.push(" | ".normal()); |
| 130 | + msg.push(err.to_string().red()); |
| 131 | + } |
| 132 | + |
| 133 | + for part in msg { |
| 134 | + print!("{}", part); |
| 135 | + } |
| 136 | + |
| 137 | + println!(""); |
| 138 | + } |
| 139 | +} |
0 commit comments