Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maoertel committed Aug 17, 2023
1 parent 91cf810 commit 8dac535
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
36 changes: 18 additions & 18 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,23 @@ mod tests {
use crate::common::parse_digest_auth_header;

use digest_auth::HttpMethod;
use mockito::Mock;
use reqwest::blocking::Client;
use reqwest::{
header::{HeaderMap, HeaderValue},
StatusCode,
};
use mockito::{Mock, Server};
use reqwest::blocking::{Client, RequestBuilder};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::StatusCode;

const PATH: &str = "/test";

fn create_request(server: &Server) -> RequestBuilder {
Client::new().get(format!("{domain}{PATH}", domain = server.url()))
}

#[test]
fn given_non_digest_auth_endpoint_when_send_with_da_then_request_executed_normally() {
// Given I have a GET request against a non digest auth endpoint
let path = "/test";
let mut server = mockito::Server::new();
let mock = server.mock("GET", path).with_status(200).create();
let request = Client::new().get(format!("{domain}{path}", domain = server.url()));
let mock = server.mock("GET", PATH).with_status(200).create();
let request = create_request(&server);

// When I send with digest auth
let response = request.send_with_digest_auth("username", "password").unwrap();
Expand All @@ -111,10 +114,9 @@ mod tests {
#[test]
fn given_non_digest_auth_endpoint_unauthorized_when_send_with_da_then_request_fails_with_401() {
// Given I have a GET request against a non digest auth but authorized endpoint
let path = "/test";
let mut server = mockito::Server::new();
let mock = server.mock("GET", path).with_status(401).create();
let request = Client::new().get(format!("{domain}{path}", domain = server.url()));
let mock = server.mock("GET", PATH).with_status(401).create();
let request = create_request(&server);

// When I send with digest auth
let response = request.send_with_digest_auth("username", "password").unwrap();
Expand All @@ -127,26 +129,24 @@ mod tests {
#[test]
fn given_digest_auth_endpoint_authorized_when_send_with_da_then_request_succeeds() {
// Given I have a GET request against a digest auth endpoint with valid 'www-authenticate' header
let path = "/test";
let mut server = mockito::Server::new();
let url = format!("{domain}{path}", domain = server.url());
let www_authenticate = "Digest realm=\"testrealm@host.com\",qop=\"auth,auth-int\",nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\",opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"";
let mut header = HeaderMap::new();
header.insert("www-authenticate", HeaderValue::from_static(www_authenticate));
let auth_header = parse_digest_auth_header(&header, path, HttpMethod::GET, None, "username", "password").unwrap();
let auth_header = parse_digest_auth_header(&header, PATH, HttpMethod::GET, None, "username", "password").unwrap();

let first_request = server
.mock("GET", path)
.mock("GET", PATH)
.with_status(401)
.with_header("www-authenticate", www_authenticate)
.create();
let second_request = server
.mock("GET", path)
.mock("GET", PATH)
.with_header("Authorization", &auth_header.to_header_string())
.with_status(200)
.create();

let request = Client::new().get(url);
let request = create_request(&server);

// When I send with digest auth
let response = request.send_with_digest_auth("username", "password").unwrap();
Expand Down
34 changes: 17 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,22 @@ mod tests {
use crate::WithDigestAuth;

use digest_auth::HttpMethod;
use mockito::Mock;
use reqwest::{
header::{HeaderMap, HeaderValue},
Client, StatusCode,
};
use mockito::{Mock, Server};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{Client, RequestBuilder, StatusCode};

const PATH: &str = "/test";

fn create_request(server: &Server) -> RequestBuilder {
Client::new().get(format!("{domain}{PATH}", domain = server.url()))
}

#[tokio::test]
async fn given_non_digest_auth_endpoint_when_send_with_da_then_request_executed_normally() {
// Given I have a GET request against a non digest auth endpoint
let path = "/test";
let mut server = mockito::Server::new();
let mock = server.mock("GET", path).with_status(200).create();
let request = Client::new().get(format!("{domain}{path}", domain = server.url()));
let mock = server.mock("GET", PATH).with_status(200).create();
let request = create_request(&server);

// When I send with digest auth
let response = request.send_with_digest_auth("username", "password").await.unwrap();
Expand All @@ -161,10 +164,9 @@ mod tests {
#[tokio::test]
async fn given_non_digest_auth_endpoint_unauthorized_when_send_with_da_then_request_fails_with_401() {
// Given I have a GET request against a non digest auth but authorized endpoint
let path = "/test";
let mut server = mockito::Server::new();
let mock = server.mock("GET", path).with_status(401).create();
let request = Client::new().get(format!("{domain}{path}", domain = server.url()));
let mock = server.mock("GET", PATH).with_status(401).create();
let request = create_request(&server);

// When I send with digest auth
let response = request.send_with_digest_auth("username", "password").await.unwrap();
Expand All @@ -177,26 +179,24 @@ mod tests {
#[tokio::test]
async fn given_digest_auth_endpoint_authorized_when_send_with_da_then_request_succeeds() {
// Given I have a GET request against a digest auth endpoint with valid 'www-authenticate' header
let path = "/test";
let mut server = mockito::Server::new();
let url = format!("{domain}{path}", domain = server.url());
let www_authenticate = "Digest realm=\"testrealm@host.com\",qop=\"auth,auth-int\",nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\",opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"";
let mut header = HeaderMap::new();
header.insert("www-authenticate", HeaderValue::from_static(www_authenticate));
let auth_header = parse_digest_auth_header(&header, path, HttpMethod::GET, None, "username", "password").unwrap();
let auth_header = parse_digest_auth_header(&header, PATH, HttpMethod::GET, None, "username", "password").unwrap();

let first_request = server
.mock("GET", path)
.mock("GET", PATH)
.with_status(401)
.with_header("www-authenticate", www_authenticate)
.create();
let second_request = server
.mock("GET", path)
.mock("GET", PATH)
.with_header("Authorization", &auth_header.to_header_string())
.with_status(200)
.create();

let request = Client::new().get(url);
let request = create_request(&server);

// When I send with digest auth
let response = request.send_with_digest_auth("username", "password").await.unwrap();
Expand Down

0 comments on commit 8dac535

Please sign in to comment.