[][src]Module roa::cors

The cors module of roa. This module provides a middleware Cors.

Example

use roa::cors::Cors;
use roa::core::{App, StatusCode, header::{ORIGIN, ACCESS_CONTROL_ALLOW_ORIGIN}};
use async_std::task::spawn;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    pretty_env_logger::init();
    let (addr, server) = App::new(())
        .gate(Cors::builder().build())
        .end(|ctx| async move {
            Ok(())
        })
        .run_local()?;
    spawn(server);
    let client = reqwest::Client::new();
    let resp = client
        .get(&format!("http://{}", addr))
        .header(ORIGIN, "github.com")
        .send()
        .await?;
    assert_eq!(StatusCode::OK, resp.status());
    assert_eq!(
        "github.com",
        resp.headers().get(ACCESS_CONTROL_ALLOW_ORIGIN).unwrap().to_str()?
    );
    Ok(())
}

Structs

Cors

A middleware to deal with Cross-Origin Resource Sharing (CORS).