Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: Test compilation and switch to stable Rust
  • Loading branch information
dbanty committed Feb 10, 2021
1 parent bccc003 commit 3f5ece7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
7 changes: 3 additions & 4 deletions examples/hello_api.rs
@@ -1,5 +1,3 @@
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;
use rocket_lamb::RocketExt;
Expand All @@ -9,9 +7,10 @@ fn hello() -> &'static str {
"Hello, world!"
}

fn main() {
#[tokio::main]
async fn main() {
rocket::ignite()
.mount("/hello", routes![hello])
.lambda()
.launch();
.launch().await;
}
1 change: 0 additions & 1 deletion rust-toolchain

This file was deleted.

8 changes: 4 additions & 4 deletions src/builder.rs
Expand Up @@ -2,7 +2,7 @@ use crate::config::*;
use crate::handler::RocketHandler;
use lamedh_http::handler;
use lamedh_runtime::run;
use rocket::local::blocking::Client;
use rocket::local::asynchronous::Client;
use rocket::Rocket;
use std::sync::Arc;

Expand Down Expand Up @@ -43,9 +43,9 @@ impl RocketHandlerBuilder {
/// let rocket_handler = rocket::ignite().lambda().into_handler();
/// run(handler(rocket_handler));
/// ```
pub fn into_handler(self) -> RocketHandler {
pub async fn into_handler(self) -> RocketHandler {
// TODO: Change this to async Client?
let client = Arc::new(Client::untracked(self.rocket).unwrap());
let client = Arc::new(Client::untracked(self.rocket).await.unwrap());
RocketHandler {
client,
config: Arc::new(self.config),
Expand All @@ -69,7 +69,7 @@ impl RocketHandlerBuilder {
/// rocket::ignite().lambda().launch();
/// ```
pub async fn launch(self) -> ! {
run(handler(self.into_handler())).await.unwrap();
run(handler(self.into_handler().await)).await.unwrap();
unreachable!("lambda! should loop forever (or panic)")
}

Expand Down
6 changes: 3 additions & 3 deletions src/handler.rs
Expand Up @@ -5,7 +5,7 @@ use aws_lambda_events::encodings::Body;
use lamedh_http::{Handler, Request, RequestExt, Response};
use lamedh_runtime::Context;
use rocket::http::{uri::Uri, Header};
use rocket::local::blocking::{Client, LocalRequest, LocalResponse};
use rocket::local::asynchronous::{Client, LocalRequest, LocalResponse};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
Expand Down Expand Up @@ -63,7 +63,7 @@ async fn process_request(
req: Request,
) -> Result<Response<Body>, RocketLambError> {
let local_req = create_rocket_request(&client, Arc::clone(&config), req)?;
let local_res = local_req.dispatch();
let local_res = local_req.dispatch().await;
create_lambda_response(config, local_res).await
}

Expand Down Expand Up @@ -104,7 +104,7 @@ async fn create_lambda_response(
.and_then(|ct| config.response_types.get(&ct.to_lowercase()))
.copied()
.unwrap_or(config.default_response_type);
let body = match local_res.into_bytes() {
let body = match local_res.into_bytes().await {
Some(b) => match response_type {
ResponseType::Auto => match String::from_utf8(b) {
Ok(s) => Body::Text(s),
Expand Down
36 changes: 18 additions & 18 deletions src/request_ext.rs
Expand Up @@ -66,21 +66,21 @@ fn is_default_api_gateway_url(req: &Request) -> bool {
.unwrap_or(false)
}

fn populate_resource_path(req: &Request, resource_path: String) -> String {
let path_parameters = req.path_parameters();
resource_path
.split('/')
.map(|segment| {
if segment.starts_with('{') {
let end = if segment.ends_with("+}") { 2 } else { 1 };
let param = &segment[1..segment.len() - end];
path_parameters
.get(param)
.unwrap_or_else(|| panic!("Could not find path parameter '{}'.", param))
} else {
segment
}
})
.collect::<Vec<&str>>()
.join("/")
}
// fn populate_resource_path(req: &Request, resource_path: String) -> String {
// let path_parameters = req.path_parameters();
// resource_path
// .split('/')
// .map(|segment| {
// if segment.starts_with('{') {
// let end = if segment.ends_with("+}") { 2 } else { 1 };
// let param = &segment[1..segment.len() - end];
// path_parameters
// .get(param)
// .unwrap_or_else(|| panic!("Could not find path parameter '{}'.", param))
// } else {
// segment
// }
// })
// .collect::<Vec<&str>>()
// .join("/")
// }
7 changes: 3 additions & 4 deletions tests/path_tests.rs
@@ -1,9 +1,8 @@
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;

use lamedh_http::{Body, Handler, Request};
use aws_lambda_events::encodings::Body;
use lamedh_http::{Handler, Request};
use lamedh_runtime::Context;
use rocket::http::uri::Origin;
use rocket_lamb::{BasePathBehaviour, RocketExt};
Expand Down Expand Up @@ -41,7 +40,7 @@ macro_rules! test_case {
let mut handler = make_rocket()
.lambda()
.base_path_behaviour(BasePathBehaviour::$path_behaviour)
.into_handler();
.into_handler().await;

let req = get_request($file)?;
let res = handler.call(req, Context::default()).await?;
Expand Down
15 changes: 7 additions & 8 deletions tests/rocket_tests.rs
@@ -1,9 +1,8 @@
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;

use lamedh_http::{Body, Handler, Request, Response};
use aws_lambda_events::encodings::Body;
use lamedh_http::{Handler, Request, Response};
use lamedh_runtime::Context;
use rocket_lamb::{ResponseType, RocketExt};
use std::error::Error;
Expand Down Expand Up @@ -40,7 +39,7 @@ fn get_request(json_file: &'static str) -> Result<Request, Box<dyn Error>> {

#[tokio::test]
async fn ok_auto_text() -> Result<(), Box<dyn Error>> {
let mut handler = make_rocket().lambda().into_handler();
let mut handler = make_rocket().lambda().into_handler().await;

let req = get_request("upper")?;
let res = handler.call(req, Context::default()).await?;
Expand All @@ -53,7 +52,7 @@ async fn ok_auto_text() -> Result<(), Box<dyn Error>> {

#[tokio::test]
async fn ok_auto_binary() -> Result<(), Box<dyn Error>> {
let mut handler = make_rocket().lambda().into_handler();
let mut handler = make_rocket().lambda().into_handler().await;

let req = get_request("binary")?;
let res = handler.call(req, Context::default()).await?;
Expand All @@ -69,7 +68,7 @@ async fn ok_default_binary() -> Result<(), Box<dyn Error>> {
let mut handler = make_rocket()
.lambda()
.default_response_type(ResponseType::Binary)
.into_handler();
.into_handler().await;

let req = get_request("upper")?;
let res = handler.call(req, Context::default()).await?;
Expand All @@ -88,7 +87,7 @@ async fn ok_type_binary() -> Result<(), Box<dyn Error>> {
let mut handler = make_rocket()
.lambda()
.response_type("TEXT/PLAIN", ResponseType::Binary)
.into_handler();
.into_handler().await;

let req = get_request("upper")?;
let res = handler.call(req, Context::default()).await?;
Expand All @@ -104,7 +103,7 @@ async fn ok_type_binary() -> Result<(), Box<dyn Error>> {

#[tokio::test]
async fn request_not_found() -> Result<(), Box<dyn Error>> {
let mut handler = make_rocket().lambda().into_handler();
let mut handler = make_rocket().lambda().into_handler().await;

let req = get_request("not_found")?;
let res = handler.call(req, Context::default()).await?;
Expand Down

0 comments on commit 3f5ece7

Please sign in to comment.