Skip to content

Commit

Permalink
axum tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Dec 3, 2023
1 parent 884c47f commit 601cd73
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
7 changes: 6 additions & 1 deletion oasgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ name = "oasgen_test_actix"
path = "tests/test-actix.rs"
required-features = ["actix"]

[[test]]
name = "oasgen_test_axum"
path = "tests/test-axum.rs"
required-features = ["axum"]

[dependencies]
inventory = "0.3.13"
actix-web = { version = "4.3.1", optional = true }
axum = { version = "0.6.18", optional = true }
axum = { version = "0.6.18", optional = true, features = ["json"] }
futures = "0.3.28"
http = "0.2.9"
indexmap = "2"
Expand Down
5 changes: 5 additions & 0 deletions oasgen/tests/test-axum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[test]
fn run_tests() {
let t = trybuild::TestCases::new();
t.pass("tests/test-axum/01-hello.rs");
}
33 changes: 33 additions & 0 deletions oasgen/tests/test-axum/01-hello.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use oasgen::{OaSchema, Server, openapi};
use axum::Json;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, OaSchema)]
pub struct SendCode {
pub mobile: String,
}

#[derive(Serialize, OaSchema)]
pub struct SendCodeResponse {
pub found_account: bool,
}

#[openapi]
async fn send_code(_body: Json<SendCode>) -> Json<SendCodeResponse> {
Json(SendCodeResponse { found_account: false })
}

fn main() {
use pretty_assertions::assert_eq;
let server = Server::axum()
.post("/hello", send_code)
.freeze()
;

let spec = server.openapi.clone();
let spec = serde_yaml::to_string(&spec).unwrap();
assert_eq!(spec.trim(), include_str!("01-hello.yaml"));
let router = axum::Router::new()
.merge(server.into_router());
router.into_make_service();
}
37 changes: 37 additions & 0 deletions oasgen/tests/test-axum/01-hello.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: 3.0.3
info:
title: ''
version: ''
paths:
/hello:
post:
operationId: send_code
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SendCode'
required: true
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/SendCodeResponse'
components:
schemas:
SendCode:
type: object
properties:
mobile:
type: string
required:
- mobile
SendCodeResponse:
type: object
properties:
found_account:
type: boolean
required:
- found_account
Empty file.

0 comments on commit 601cd73

Please sign in to comment.