Skip to content

Commit

Permalink
Merge pull request #63 from mythmon/cookies-optional
Browse files Browse the repository at this point in the history
feat: Make cookie handling optional via cargo feature
  • Loading branch information
alexliesenfeld authored Jan 11, 2022
2 parents 5e65b9a + 9f8bee8 commit eeb4783
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ async-trait = "0.1"
async-object-pool = "0.1"
crossbeam-utils = "0.8"
futures-util = "0.3"
basic-cookies = "0.1"
similar = "2.1.0"
levenshtein = "1.0"
form_urlencoded = "1.0"

basic-cookies = { version = "0.1", optional = true }
colored = { version = "2.0", optional = true }
clap = { version = "3.0.0-rc.7", features = ["derive", "env"], optional = true }
env_logger = { version = "0.9", optional = true }
Expand All @@ -51,8 +51,10 @@ colored = "2.0"
ureq = "2.3"

[features]
default = ["cookies"]
standalone = ["clap", "env_logger", "serde_yaml"]
color = ["colored"]
cookies = ["basic-cookies"]

[[bin]]
name = "httpmock"
Expand Down
1 change: 1 addition & 0 deletions src/api/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ mod test {
};

#[test]
#[cfg(not(feature = "color"))]
#[should_panic(expected = "1 : This is a title\n\
------------------------------------------------------------------------------------------\n\
Expected: [equals] /toast\n\
Expand Down
6 changes: 6 additions & 0 deletions src/api/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,9 @@ impl When {
/// * `name` - The cookie name.
/// * `value` - The expected cookie value.
///
/// > Note: This function is only available when the `cookies` feature is enabled.
/// > It is enabled by default.
///
/// # Example
/// ```
/// use httpmock::prelude::*;
Expand Down Expand Up @@ -707,6 +710,9 @@ impl When {
///
/// * `name` - The cookie name
///
/// > Note: This function is only available when the `cookies` feature is enabled.
/// > It is enabled by default.
///
/// # Example
/// ```
/// use httpmock::prelude::*;
Expand Down
2 changes: 2 additions & 0 deletions src/server/matchers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;
use std::fmt::Display;

#[cfg(feature = "cookies")]
use basic_cookies::Cookie;
use serde::{Deserialize, Serialize};
use similar::{ChangeTag, TextDiff};
Expand Down Expand Up @@ -45,6 +46,7 @@ pub trait Matcher {
// *************************************************************************************************
// Helper functions
// *************************************************************************************************
#[cfg(feature = "cookies")]
pub(crate) fn parse_cookies(req: &HttpMockRequest) -> Result<Vec<(String, String)>, String> {
let parsing_result = req.headers.as_ref().map_or(None, |request_headers| {
request_headers
Expand Down
8 changes: 5 additions & 3 deletions src/server/matchers/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::collections::BTreeMap;
use serde_json::Value;

use crate::common::data::HttpMockRequest;
use crate::server::matchers::parse_cookies;
use crate::server::matchers::sources::ValueRefSource;
use crate::server::matchers;

pub(crate) trait ValueTarget<T> {
fn parse_from_request(&self, req: &HttpMockRequest) -> Option<T>;
Expand Down Expand Up @@ -69,17 +68,20 @@ impl ValueTarget<Value> for JSONBodyTarget {
// *************************************************************************************
// CookieTarget
// *************************************************************************************
#[cfg(feature = "cookies")]
pub(crate) struct CookieTarget {}

#[cfg(feature = "cookies")]
impl CookieTarget {
pub fn new() -> Self {
Self {}
}
}

#[cfg(feature = "cookies")]
impl MultiValueTarget<String, String> for CookieTarget {
fn parse_from_request(&self, req: &HttpMockRequest) -> Option<Vec<(String, Option<String>)>> {
let req_cookies = match parse_cookies(req) {
let req_cookies = match matchers::parse_cookies(req) {
Ok(v) => v,
Err(err) => {
log::info!(
Expand Down
6 changes: 5 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ use crate::server::matchers::sources::{
StringPathSource, XWWWFormUrlencodedSource,
};
use crate::server::matchers::targets::{
CookieTarget, FullRequestTarget, HeaderTarget, MethodTarget, PathTarget, QueryParameterTarget,
FullRequestTarget, HeaderTarget, MethodTarget, PathTarget, QueryParameterTarget,
XWWWFormUrlEncodedBodyTarget,
};
#[cfg(feature = "cookies")]
use crate::server::matchers::targets::CookieTarget;
use crate::server::matchers::Matcher;
use crate::server::web::routes;
use futures_util::task::Spawn;
Expand Down Expand Up @@ -144,6 +146,7 @@ impl MockServerState {
weight: 1,
}),
// Cookie exact
#[cfg(feature = "cookies")]
Box::new(MultiValueMatcher {
entity_name: "cookie",
key_comparator: Box::new(StringExactMatchComparator::new(true)),
Expand All @@ -157,6 +160,7 @@ impl MockServerState {
weight: 1,
}),
// Cookie exists
#[cfg(feature = "cookies")]
Box::new(MultiValueMatcher {
entity_name: "cookie",
key_comparator: Box::new(StringExactMatchComparator::new(true)),
Expand Down
1 change: 1 addition & 0 deletions src/server/web/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::BTreeMap;
use std::str::FromStr;
use std::sync::Arc;

#[cfg(feature = "cookies")]
use basic_cookies::Cookie;
use serde_json::Value;

Expand Down
1 change: 1 addition & 0 deletions tests/examples/headers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ fn headers_test() {
// Assert
m.assert();
assert_eq!(response.status(), 201);
assert_eq!(response.headers().get("Content-Length").unwrap().to_str().unwrap(), "0");
}

0 comments on commit eeb4783

Please sign in to comment.