Skip to content

Commit

Permalink
✅📝 adds docs and tests for server
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Dec 26, 2023
1 parent 5a3c4fc commit 3e2e2d9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ utoipa-swagger-ui = { version = "5", features = ["axum"] }
axum-test = "14.0.0"
tempfile = "3.8.1"
tokio = { version = "1.0", features = ["full", "test-util"] }
tower = "0.4.13"
55 changes: 47 additions & 8 deletions src/api/openapi.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
use utoipa::OpenApi;

use crate::api::model::ErrorResponse;

use super::model::{
CompatGenerateRequest, FinishReason, GenerateParameters, GenerateRequest, GenerateResponse,
Info, StreamDetails, StreamResponse, Token,
};
use crate::api::model::ErrorResponse;
use utoipa::OpenApi;

/// Represents the API documentation for the text generation inference service.
///
/// This struct uses `utoipa::OpenApi` to provide a centralized documentation of the API endpoints
/// and their associated request and response models. It is used to generate OpenAPI specification
/// for the service, which can be served as a Swagger UI or other OpenAPI-compatible documentation tools.
#[derive(OpenApi)]
#[openapi(
paths(super::routes::generate_text::generate_text_handler, super::routes::generate_stream::generate_stream_handler, super::routes::health::get_health_handler, super::routes::info::get_info_handler),
// List of API endpoints to be included in the documentation.
paths(
super::routes::generate_text::generate_text_handler,
super::routes::generate_stream::generate_stream_handler,
super::routes::health::get_health_handler,
super::routes::info::get_info_handler
),
// Schema components for requests and responses used across the API.
components(
schemas(CompatGenerateRequest, GenerateRequest, GenerateResponse, GenerateParameters, ErrorResponse, StreamResponse,
StreamDetails, Token, FinishReason, Info)
schemas(
CompatGenerateRequest,
GenerateRequest,
GenerateResponse,
GenerateParameters,
ErrorResponse,
StreamResponse,
StreamDetails,
Token,
FinishReason,
Info
)
),
tags((name = "Text Generation Inference", description = "Text generation Inference API"))
// Metadata and description of the API tags.
tags(
(name = "Text Generation Inference", description = "Text generation Inference API")
)
)]
pub struct ApiDoc;

#[cfg(test)]
mod tests {
use super::*;
use utoipa::OpenApi;

#[test]
fn api_doc_contains_all_endpoints() {
let api_doc = ApiDoc::openapi();
let paths = api_doc.paths.paths;
assert!(paths.contains_key("/generate"));
assert!(paths.contains_key("/generate_stream"));
assert!(paths.contains_key("/health"));
assert!(paths.contains_key("/info"));
}
}
58 changes: 58 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use axum::{
response::Redirect,
routing::{get, post},
Router,
};
Expand All @@ -14,8 +15,22 @@ use crate::{
config::Config,
};

/// Creates and configures the Axum web server with various routes and Swagger UI.
///
/// This function sets up all the necessary routes for the API and merges them
/// with the Swagger UI for easy API documentation and testing.
///
/// # Arguments
///
/// * `config` - Configuration settings for the server.
///
/// # Returns
///
/// An instance of `axum::Router` configured with all routes and the Swagger UI.

pub fn server(config: Config) -> Router {
let router = Router::new()
.route("/", get(|| async { Redirect::permanent("/swagger-ui") }))
.route("/", post(generate_handler))
.route("/generate", post(generate_text_handler))
.route("/health", get(get_health_handler))
Expand All @@ -27,3 +42,46 @@ pub fn server(config: Config) -> Router {

router.merge(swagger_ui)
}

#[cfg(test)]
mod tests {
use super::*;
use axum::{
body::Body,
http::{Request, StatusCode},
};
use tower::ServiceExt; // for `oneshot` function

#[tokio::test]
async fn test_root_redirects_to_swagger_ui() {
let config = Config::default();
let app = server(config);

let req = Request::builder()
.method("GET")
.uri("/")
.body(Body::empty())
.unwrap();

let response = app.clone().oneshot(req).await.unwrap();

// Verify that the response is a redirect to /swagger-ui.
assert_eq!(response.status().as_u16(), 308);
assert_eq!(response.headers().get("location").unwrap(), "/swagger-ui");
}

#[tokio::test]
async fn test_swagger_ui_endpoint() {
let config = Config::default();
let app = server(config);

let req = Request::builder()
.method("GET")
.uri("/swagger-ui/index.html")
.body(Body::empty())
.unwrap();

let response = app.clone().oneshot(req).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
}

0 comments on commit 3e2e2d9

Please sign in to comment.