当前位置: 首页 > news >正文

Rust: Web框架Axum和Rest Client协同测试

Axum作为Rust当红Web框架,值得了解一下。下面实例包括几个典型的常场场景。
具体如下:

一、Axum

1、toml中依赖


[dependencies]
tokio = {version="1.39.3",features =["full"]}
axum ={version ="0.7.5",features =["tokio"]}
serde = {version ="1.0.209",features =["derive"]}
serde_json ="1.0.1"

2、main.rs

涉及到简单路由和提取器等,包括主要类型的文件形态的处理。

use axum::{routing::{get,post},Router,response,Form,http::{header::HeaderMap},
};
use serde_json::{Value, json};
use serde::Deserialize;
use axum::extract::{ Query,Json,Path};
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Deserialize,Debug)]
struct User {name: String,old: i32,
}#[derive(Deserialize)]
struct LoginForm {account: String,password: String,
}
#[derive(Deserialize,Debug)]
struct Params {tradedays: Option<i32>,//Option=>字段可以缺省price:Option<f64>,name: Option<String>,
}
#[derive(Deserialize,Debug)]
struct Strategy {name: String,amount: f64,id: i32,
}
#[derive(Debug)]
struct AppState {count: i32
}
#[tokio::main]
async fn main() {let shared_state = Arc::new(AppState { count:0 });let app = Router::new().route("/", get(hello)).route("/headers",get(headers)).route("/html", get(hello_html)).route("/form", get(show_form).post(accept_form)).route("/hashmap", get(query_hashmap))//全部解释成hashmap.route("/user/:name/:old", get(query_user)) .route("/path/:name/:id", get(path)) //两个参数.route("/json", post(query_user_json)).route("/query_json",post({let shared_state = Arc::clone(&shared_state);move |body| query_json(body,shared_state)}   ,)).route("/params",get(get_params)).route("/strategies/", get(query_strategy));println!("Serving on http://localhost:8080 ...");let listener = tokio::net::TcpListener::bind("127.0.0.1:8080").await.unwrap();axum::serve(listener, app).await.unwrap();
}async fn headers(headers: HeaderMap) ->String{format!("{:?}", headers)
}
// Query: ?和=来解析
async fn get_params(Query(params): Query<Params>) -> String {let tradedays = params.tradedays.unwrap_or(0);let price = params.price.unwrap_or(0.0);let name = params.name.unwrap_or("no name".to_string());format!("tradedays {:?}, price: {:?}  name:{:?}", tradedays, price,name)}
//多个复杂的参数
async fn query_strategy(Json(strategy): Json<Strategy>) ->String{println!("strategy :{:?}",strategy);format!("receive=> strategy:{:?}",strategy)
}
//Json(user):Json<User>
async fn query_hashmap(Query(inputs): Query<HashMap<String, String>>) -> String{println!("inputs:{:?}",inputs);format!("inputs:{:?}",inputs)
}
async fn query_user(Path(user):Path<User>){println!("welcome! user:{user:#?}");
}
async fn query_user_json(Json(user): Json<User>) -> response::Json<Value>{println!("response:{:?}",user);Json(json!({ "user": user.name, "old": user.old }))
}
async fn query_json(Json(user):Json<User>,arc: Arc<AppState>)->String{println!("arc:{:?}",arc);format!("user:{:?} ",user)
}// `&'static str` becomes a `200 OK` with `content-type: text/plain; charset=utf-8`
async fn hello() -> &'static str {"Hello, World!"
}
async fn path(Path((name,id)):Path<(String,i32)>) ->String{println!("hi,{name:#?} => id {id:#?}");format!("hi,{},{}",name,id)}
// `Html` will get a `text/html` content-type
async fn hello_html() -> response::Html<&'static str> {response::Html("<h1>Hello HTML</h1><p>Hello, World!</p>")
}async fn show_form() -> response::Html<&'static str> {response::Html(r#"<html><head><title>Login Form</title></head><body><h1>User Info</h1><form method="post"><label for="field1">account:</label><input type="text" name="field1" id="field1"><br><label for="field2">password:</label><input type="text" name="field2" id="field2"><br><input type="submit" value="Submit"></form></body></html>"#)
}async fn accept_form(Form(login): Form<LoginForm>) -> response::Html<String> {let response_html = format!(r#"<html><head><title>Form Submission Result</title></head><body><h1>Form Submission Result</h1><p>account : {}</p><p>password: {}</p></body></html>"#,login.account, login.password);println!("response_html: {}", response_html);response::Html(response_html)
}

二、Rest Client工具协同测试

在vscode中下载Rest Client插件后,通过创建以.http 或 .rest为结尾的文件,进行http请求测试。具体Rest Client功能很强大,这里只介绍,与Axum配合测试的方案。

具体如下:


# 头文件和body之间要有空隔行### 值中不需要引号!
@url = http://localhost:8080 
@name = wowotuo
@token =ad;ipiqedqeqeqeqepqei1213132;dkqdqeqe
@id =6### =>"/";GET;
GET {{url}}/ HTTP/1.1### =>"/html";GET
GET {{url}}/html HTTP/1.1
### /headers
GET {{url}}/headers  HTTP/1.1### /path/:name/:id ; id为输入参数
GET {{url}}/path/{{name}}/{{id}} HTTP/1.1### =>/form;有GET和POST两部分;POST方法要求输入
POST {{url}}/form HTTP/1.1
Content-Type: application/x-www-form-urlencodedaccount={{name}}&password=dbfund### /json; POST.输入user的json格式; 注意JSON格式要"双引号"!
POST {{url}}/json HTTP/1.1
content-type: application/json; charset=utf-8{"name": "{{name}} 你好!","old": 19}### /user/:name/:old; 另外一种表达形式
GET {{url}}/user/{{name}}/32  HTTP/1.1### /params
GET {{url}}/params?tradedays=3&price=3.9&name=breaking### /hashmap; POST,要求输入hashmap
GET {{url}}/hashmap?name={{name}}&old="19"  HTTP/1.1### 对于curl命令的支持 类似 =>GET {{url}}/html
curl --request GET \--url {{url}}/html###--header 'user-agent: vscode-restclient'

三、输出
用Rest Client调试的结果是非常直观。
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 常见概念 -- 非线性效应
  • FPGA随记——8B/10B编码
  • 倍福——ADS协议解析及C语言读写库
  • 2024年6月第2套英语四级真题PDF
  • 第二章 深信服超融合测试历程第二天
  • 【计算机视觉前沿研究 热点 顶会】ECCV 2024中Mamba有关的论文
  • macOS系统介绍与特点
  • Oracle(106)如何实现透明数据加密?
  • 93. UE5 GAS RPG 应用负面效果表现
  • 批处理常用指令与脚本的例子
  • web渗透:SSRF漏洞
  • 海康二次开发学习笔记7-流程相关操作
  • 深度学习——基于MTCNN算法实现人脸侦测
  • 实现A-Z滑动检索菜单
  • 论俄国留学对计算机系大学生的帮助
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • 【Amaple教程】5. 插件
  • CSS居中完全指南——构建CSS居中决策树
  • If…else
  • Java IO学习笔记一
  • Java面向对象及其三大特征
  • Linux各目录及每个目录的详细介绍
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • PAT A1017 优先队列
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 服务器之间,相同帐号,实现免密钥登录
  • 机器学习学习笔记一
  • 聚类分析——Kmeans
  • 买一台 iPhone X,还是创建一家未来的独角兽?
  • 排序算法之--选择排序
  • 浅析微信支付:申请退款、退款回调接口、查询退款
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • # windows 运行框输入mrt提示错误:Windows 找不到文件‘mrt‘。请确定文件名是否正确后,再试一次
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • ###项目技术发展史
  • #宝哥教你#查看jquery绑定的事件函数
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • $(selector).each()和$.each()的区别
  • (+4)2.2UML建模图
  • (12)目标检测_SSD基于pytorch搭建代码
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (4) PIVOT 和 UPIVOT 的使用
  • (Charles)如何抓取手机http的报文
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (每日一问)基础知识:堆与栈的区别
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (学习日记)2024.01.09
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (游戏设计草稿) 《外卖员模拟器》 (3D 科幻 角色扮演 开放世界 AI VR)
  • (转)http-server应用