Skip to content

Commit

Permalink
A couple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Nov 25, 2024
1 parent 761d3a7 commit ac817d2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
23 changes: 23 additions & 0 deletions rwf/src/controller/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,26 @@ impl Authentication for SessionAuth {
}
}
}

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

#[test]
fn test_should_renew() {
let mut session = Session::default();
assert!(!session.should_renew());

assert_eq!(get_config().general.session_duration(), Duration::weeks(4));

session.expiration = (OffsetDateTime::now_utc() + Duration::weeks(2)
- Duration::seconds(5))
.unix_timestamp();
assert!(session.should_renew());

session.expiration =
(OffsetDateTime::now_utc() + Duration::weeks(2) + Duration::seconds(5))
.unix_timestamp();
assert!(!session.should_renew());
}
}
17 changes: 17 additions & 0 deletions rwf/src/controller/middleware/request_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,20 @@ impl Middleware for RequestTracker {
Ok(response)
}
}

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

#[tokio::test]
async fn test_request_tracker() {
let request = Request::default();
let response = Response::default();

let mut response = RequestTracker::new()
.handle_response(&request, response)
.await
.unwrap();
assert!(response.cookies().get(COOKIE_NAME).is_some());
}
}
21 changes: 14 additions & 7 deletions rwf/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ impl Default for Request {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Clone)]
struct Inner {
body: Vec<u8>,
cookies: Cookies,
peer: Option<SocketAddr>,
peer: SocketAddr,
}

impl Default for Inner {
fn default() -> Inner {
Inner {
body: Vec::default(),
cookies: Cookies::default(),
peer: "127.0.0.1:8000".parse().unwrap(), // Just used for testing.
}
}
}

impl Request {
Expand Down Expand Up @@ -92,7 +102,7 @@ impl Request {
session: cookies.get_session()?,
inner: Arc::new(Inner {
body,
peer: Some(peer),
peer,
cookies,
}),
received_at: OffsetDateTime::now_utc(),
Expand All @@ -105,10 +115,7 @@ impl Request {
/// This is the IP address of the TCP socket, and does
/// not have to be the actual client's IP address.
pub fn peer(&self) -> &SocketAddr {
self.inner
.peer
.as_ref()
.expect("peer is not set on the request")
&self.inner.peer
}

/// Set params on the request.
Expand Down
10 changes: 10 additions & 0 deletions rwf/src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,16 @@ impl Response {
.header("upgrade", protocol)
.code(101)
}

/// Response headers.
pub fn headers(&self) -> &Headers {
&self.headers
}

/// Mutable response headers.
pub fn headers_mut(&mut self) -> &mut Headers {
&mut self.headers
}
}

impl From<serde_json::Value> for Response {
Expand Down

0 comments on commit ac817d2

Please sign in to comment.