bdrck 0.22.5

Generic common foundational utilities.
Documentation
// Copyright 2015 Axel Rasmussen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::http::client::AbstractClient;
use crate::http::recording::{RecordedRequest, RecordedResponse, Recording, RecordingEntry};
use crate::http::types::{HeaderMap, HttpData, ResponseMetadata};
use crate::testing::http::TestStubClient;
use reqwest::{Method, Request, Url};
use std::collections::{HashMap, VecDeque};

fn build_recording_with_get_entry(url: &str, body: &str) -> Recording {
    let entry = RecordingEntry {
        req: RecordedRequest {
            method: "GET".to_owned(),
            url: url.to_owned(),
            headers: HashMap::new(),
            body: None,
        },
        res: RecordedResponse {
            metadata: ResponseMetadata {
                status: 200,
                headers: HeaderMap::new(),
            },
            body: HttpData::Text(body.to_owned()),
        },
    };
    let mut entries = VecDeque::new();
    entries.push_back(entry);
    Recording(entries)
}

#[test]
fn test_stub_client_happy_path() {
    crate::init().unwrap();

    let url: Url = "http://example.com/".parse().unwrap();
    let recording = build_recording_with_get_entry(url.as_str(), "hello");
    let bytes = serde_json::to_vec(&recording).unwrap();

    let stub = TestStubClient::new();
    stub.push_recording(&bytes).unwrap();

    let req = Request::new(Method::GET, url);
    let (meta, body) = stub.execute(req).unwrap();
    assert_eq!(200, meta.status);
    assert_eq!(b"hello".to_vec(), body);
}

#[test]
fn test_stub_client_binary_body_roundtrips() {
    crate::init().unwrap();

    // Make a recording whose response body is HttpData::Binary, and verify
    // the stub returns the raw bytes unchanged.
    let url: Url = "http://example.com/bin".parse().unwrap();
    let entry = RecordingEntry {
        req: RecordedRequest {
            method: "GET".to_owned(),
            url: url.as_str().to_owned(),
            headers: HashMap::new(),
            body: None,
        },
        res: RecordedResponse {
            metadata: ResponseMetadata {
                status: 200,
                headers: HeaderMap::new(),
            },
            body: HttpData::Binary(vec![0xff, 0xfe, 0xfd]),
        },
    };
    let mut entries = VecDeque::new();
    entries.push_back(entry);
    let recording = Recording(entries);
    let bytes = serde_json::to_vec(&recording).unwrap();

    let stub = TestStubClient::new();
    stub.push_recording(&bytes).unwrap();

    let req = Request::new(Method::GET, url);
    let (_meta, body) = stub.execute(req).unwrap();
    assert_eq!(vec![0xff, 0xfe, 0xfd], body);
}

#[test]
fn test_stub_client_push_invalid_json() {
    crate::init().unwrap();

    let stub = TestStubClient::new();
    assert!(stub.push_recording(b"this is not JSON").is_err());
}

#[test]
fn test_stub_client_request_builders() {
    crate::init().unwrap();

    // These just pass through to the inner reqwest::Client; exercise each so
    // we get line coverage on the trait-method delegations.
    let stub = TestStubClient::new();
    let url: Url = "http://example.com/".parse().unwrap();
    let _ = stub.get(url.clone());
    let _ = stub.post(url.clone());
    let _ = stub.put(url.clone());
    let _ = stub.patch(url.clone());
    let _ = stub.delete(url.clone());
    let _ = stub.head(url);
}