Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfreyer committed Jul 20, 2019
1 parent dfe5e7d commit 80320f8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 58 deletions.
4 changes: 2 additions & 2 deletions askama_actix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "askama_actix"
version = "0.1.0"
authors = ["Fabian Freyer <fabian.freyer@physik.tu-berlin.de>"]
version = "0.8.0"
authors = ["Dirkjan Ochtman <dirkjan@ochtman.nl>"]
categories = ["template-engine"]
workspace = ".."
edition = "2018"
Expand Down
60 changes: 54 additions & 6 deletions askama_actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
//! [the example](https://github.com/djc/askama/blob/master/testing/tests/actix_web.rs)
//! from the Askama test suite for more on how to integrate.

extern crate askama;
extern crate actix_web;
extern crate bytes;
extern crate mime_guess;

use std::fmt;

struct BytesWriter {
Expand Down Expand Up @@ -43,7 +38,7 @@ impl fmt::Write for BytesWriter {
// Older versions that don't have it exposed are easier this way. If ext is empty or no
// associated type was found, then this returns `application/octet-stream`, in line with how
// actix_web handles it in newer releases.
pub use self::actix_web::{
pub use actix_web::{
error::ErrorInternalServerError, Error, HttpRequest, HttpResponse, Responder,
};

Expand All @@ -63,3 +58,56 @@ impl<T: askama::Template> TemplateIntoResponse for T {
.body(buffer.freeze()))
}
}

#[cfg(test)]
mod tests {
use actix_web::http::header::CONTENT_TYPE;
use actix_web::test;
use actix_web::HttpMessage;
use askama::Template;
use bytes::Bytes;
use super::TemplateIntoResponse;

#[derive(Template)]
#[template(path = "../../testing/templates/hello.html")]
struct HelloTemplate<'a> {
name: &'a str,
}

#[test]
fn test_actix_web() {
let mut srv = test::TestServer::new(|app| app.handler(|_| HelloTemplate { name: "world" }));

let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
assert_eq!(
response.headers().get(CONTENT_TYPE).unwrap(),
"text/html; charset=utf-8"
);

let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
}

#[test]
fn test_actix_web_responder() {
let mut srv = test::TestServer::new(|app| {
app.handler(|_| {
let name = "world".to_owned();
HelloTemplate { name: &name }.into_response()
})
});

let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
assert_eq!(
response.headers().get(CONTENT_TYPE).unwrap(),
"text/html; charset=utf-8"
);

let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
}
}
50 changes: 0 additions & 50 deletions testing/tests/actix_web.rs

This file was deleted.

0 comments on commit 80320f8

Please sign in to comment.