Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make cookie handling optional via cargo feature #63

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))]
alexliesenfeld marked this conversation as resolved.
Show resolved Hide resolved
#[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");
}