Crates.io | rocket-versioning |
lib.rs | rocket-versioning |
version | 0.1.0 |
source | src |
created_at | 2021-07-23 04:55:17.089477 |
updated_at | 2021-07-23 04:55:17.089477 |
description | api versioning for rocket web applications |
homepage | |
repository | https://github.com/songww/rocket-versioning |
max_upload_size | |
id | 426123 |
size | 8,335 |
api versioning for rocket web applications
#[macro_use] extern crate rocket;
use rocket::local::blocking::Client;
use rocket::http::{Header, Status};
use super::Versioning;
#[get("/versioning", rank = 4)]
fn versioning(_v: Versioning<1, 0>) -> String {
"v1.0".to_string()
}
#[get("/versioning", rank = 3)]
fn versioning_1_1(_v: Versioning<1, 1>) -> String {
"v1.1".to_string()
}
#[get("/versioning", rank = 2)]
fn versioning_2_1(_v: Versioning<2, 1>) -> String {
"v2.1".to_string()
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![versioning, versioning_1_1, versioning_2_1])
}
#[test]
fn test_versioning() {
let client = Client::tracked(rocket()).expect("invalid rocket instance");
// NOTICE: version should be `Major.Minor.Patch`, and patch will be ignored.
// see: https://docs.rs/semver/1.0.3/semver/struct.Version.html#errors
let response = client.get("/versioning").header(Header::new("Api-Version", "1.0.0")).dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_string().unwrap(), "v1.0");
let response = client.get("/versioning").header(Header::new("Api-Version", "1.1.0")).dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_string().unwrap(), "v1.1");
let response = client.get("/versioning").header(Header::new("Api-Version", "2.1.0")).dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_string().unwrap(), "v2.1");
let response = client.get("/versioning").header(Header::new("Api-Version", "2.0.0")).dispatch();
assert_eq!(response.status(), Status::NotFound);
}
version in header should be Major.Minor.Patch
, and patch will be ignored.
see (unsopported)[https://docs.rs/semver/1.0.3/semver/struct.Version.html#errors] version formats.