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::types::*;
use reqwest::StatusCode;
use reqwest::header::HeaderValue;

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

    let data = HttpData::from(b"hello".as_ref());
    match data {
        HttpData::Text(s) => assert_eq!("hello", s),
        HttpData::Binary(_) => panic!("expected Text for ASCII input"),
    }
}

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

    let bytes: &[u8] = &[0xff, 0xfe, 0xfd];
    let data = HttpData::from(bytes);
    match data {
        HttpData::Binary(b) => assert_eq!(bytes, b.as_slice()),
        HttpData::Text(_) => panic!("expected Binary for invalid-UTF-8 input"),
    }
}

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

    let header = HeaderValue::from_static("application/json");
    let data = HttpData::from(&header);
    match data {
        HttpData::Text(s) => assert_eq!("application/json", s),
        HttpData::Binary(_) => panic!("expected Text for ASCII header value"),
    }
}

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

    // HeaderValue::to_str rejects non-visible-ASCII. Bytes 0x80..=0xff are
    // valid header values (HeaderValue accepts any byte >= 0x20 except DEL
    // if constructed from bytes), but to_str returns an Err for them.
    let header = HeaderValue::from_bytes(&[0xff, 0xfe, 0xfd]).unwrap();
    let data = HttpData::from(&header);
    match data {
        HttpData::Binary(b) => assert_eq!(&[0xff, 0xfe, 0xfd][..], b.as_slice()),
        HttpData::Text(_) => panic!("expected Binary for non-UTF-8 header value"),
    }
}

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

    let t = HttpData::Text("abc".to_owned());
    let b = HttpData::Binary(b"abc".to_vec());
    assert_eq!(t, b);
    assert_eq!(b, t);

    // Differing contents compare unequal across representations.
    assert_ne!(
        HttpData::Text("abc".to_owned()),
        HttpData::Binary(b"xyz".to_vec())
    );
}

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

    assert_eq!(
        "hello",
        HttpData::Text("hello".to_owned())
            .try_into_string()
            .unwrap()
    );
}

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

    assert_eq!(
        "hello",
        HttpData::Binary(b"hello".to_vec())
            .try_into_string()
            .unwrap()
    );
}

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

    assert!(
        HttpData::Binary(vec![0xff, 0xfe, 0xfd])
            .try_into_string()
            .is_err()
    );
}

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

    let meta = ResponseMetadata {
        status: 200,
        headers: HeaderMap::new(),
    };
    assert_eq!(StatusCode::OK, meta.get_status().unwrap());
}

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

    // StatusCode::from_u16 rejects values outside 100..=999.
    let meta = ResponseMetadata {
        status: 0,
        headers: HeaderMap::new(),
    };
    assert!(meta.get_status().is_err());

    let meta = ResponseMetadata {
        status: 9999,
        headers: HeaderMap::new(),
    };
    assert!(meta.get_status().is_err());
}

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

    let mut headers = HeaderMap::new();
    headers.insert(
        "content-type".to_owned(),
        vec![HttpData::Text("application/json".to_owned())],
    );
    let meta = ResponseMetadata {
        status: 200,
        headers,
    };

    let got = meta.get_headers();
    assert_eq!(1, got.len());
    assert_eq!(
        &vec![HttpData::Text("application/json".to_owned())],
        got.get("content-type").unwrap()
    );
}

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

    let mut headers = HeaderMap::new();
    headers.insert(
        "x-custom".to_owned(),
        vec![HttpData::Text("value".to_owned())],
    );
    let meta = ResponseMetadata {
        status: 204,
        headers,
    };

    let s = serde_json::to_string(&meta).unwrap();
    let back: ResponseMetadata = serde_json::from_str(&s).unwrap();
    assert_eq!(204, back.status);
    assert_eq!(
        &vec![HttpData::Text("value".to_owned())],
        back.headers.get("x-custom").unwrap()
    );
}