Skip to content

Commit 1f1a171

Browse files
committed
tests for stream
1 parent 76f0536 commit 1f1a171

File tree

7 files changed

+313
-53
lines changed

7 files changed

+313
-53
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# http_req
22
> [!CAUTION]
3-
> V0.11.0 introduces major changes to design of `RequestBuilder` and `Request`. Please review [documentation](https://docs.rs/http_req/0.11.0/http_req/) before migrating from previous versions.
3+
> v0.11.0 introduces major changes to design of `RequestBuilder` and `Request`. Please review [documentation](https://docs.rs/http_req/0.11.0/http_req/) before migrating from previous versions.
44
55
[![Rust](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml/badge.svg)](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml)
66
[![Crates.io](https://img.shields.io/badge/crates.io-v0.11.0-orange.svg?longCache=true)](https://crates.io/crates/http_req)

src/chunked.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
//! implements the wire protocol for HTTP's "chunked" Transfer-Encoding.
1+
//! support for Transfer-Encoding: chunked
22
use crate::CR_LF;
3-
///
4-
/// It's a Rust version of the reference implementation in [Go][1].
5-
///
6-
/// [1]: https://golang.google.cn/src/net/http/internal/chunked.go
7-
///
83
use std::io::{self, BufRead, BufReader, Error, ErrorKind, Read};
94

105
const MAX_LINE_LENGTH: usize = 4096;
116

7+
/// Implements the wire protocol for HTTP's Transfer-Encoding: chunked.
8+
///
9+
/// It's a Rust version of the [reference implementation in Go](https://golang.google.cn/src/net/http/internal/chunked.go)
10+
///
1211
pub struct ChunkReader<R> {
1312
check_end: bool,
1413
eof: bool,
@@ -103,7 +102,10 @@ impl<R: Read> BufRead for ChunkReader<R> {
103102
}
104103
}
105104

106-
impl<R: Read> From<BufReader<R>> for ChunkReader<R> {
105+
impl<R> From<BufReader<R>> for ChunkReader<R>
106+
where
107+
R: Read,
108+
{
107109
fn from(value: BufReader<R>) -> Self {
108110
ChunkReader {
109111
check_end: false,
@@ -119,6 +121,7 @@ impl<R> ChunkReader<R>
119121
where
120122
R: Read,
121123
{
124+
/// Creates a new `ChunkReader` from `reader`
122125
pub fn new(reader: R) -> Self
123126
where
124127
R: Read,

src/error.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ pub enum ParseErr {
1010
UriErr,
1111
Invalid,
1212
Empty,
13-
#[cfg(feature = "rust-tls")]
14-
Rustls(rustls::Error),
1513
}
1614

1715
impl error::Error for ParseErr {
@@ -22,8 +20,6 @@ impl error::Error for ParseErr {
2220
Utf8(e) => Some(e),
2321
Int(e) => Some(e),
2422
StatusErr | HeadersErr | UriErr | Invalid | Empty => None,
25-
#[cfg(feature = "rust-tls")]
26-
Rustls(e) => Some(e),
2723
}
2824
}
2925
}
@@ -40,20 +36,11 @@ impl fmt::Display for ParseErr {
4036
StatusErr => "status line contains invalid values",
4137
HeadersErr => "headers contain invalid values",
4238
UriErr => "uri contains invalid characters",
43-
#[cfg(feature = "rust-tls")]
44-
Rustls(_) => "rustls error",
4539
};
4640
write!(f, "ParseErr: {}", err)
4741
}
4842
}
4943

50-
#[cfg(feature = "rust-tls")]
51-
impl From<rustls::Error> for ParseErr {
52-
fn from(e: rustls::Error) -> Self {
53-
ParseErr::Rustls(e)
54-
}
55-
}
56-
5744
impl From<num::ParseIntError> for ParseErr {
5845
fn from(e: num::ParseIntError) -> Self {
5946
ParseErr::Int(e)
@@ -93,9 +80,9 @@ impl fmt::Display for Error {
9380

9481
let err = match self {
9582
IO(_) => "IO error",
96-
Tls => "TLS error",
97-
Timeout(_) => "Timeout error",
9883
Parse(err) => return err.fmt(f),
84+
Timeout(_) => "Timeout error",
85+
Tls => "TLS error",
9986
};
10087
write!(f, "Error: {}", err)
10188
}
@@ -138,3 +125,10 @@ impl From<mpsc::RecvTimeoutError> for Error {
138125
Error::Timeout(e)
139126
}
140127
}
128+
129+
#[cfg(feature = "rust-tls")]
130+
impl From<rustls::Error> for Error {
131+
fn from(_e: rustls::Error) -> Self {
132+
Error::Tls
133+
}
134+
}

src/request.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
};
1717

1818
const CR_LF: &str = "\r\n";
19-
const DEFAULT_REQ_TIMEOUT: u64 = 12 * 60 * 60;
19+
const DEFAULT_REQ_TIMEOUT: u64 = 60 * 60;
2020

2121
/// HTTP request methods
2222
#[derive(Debug, PartialEq, Clone, Copy)]
@@ -601,7 +601,7 @@ impl<'a> Request<'a> {
601601
});
602602

603603
//Receive and process `head` of the response.
604-
raw_response_head.receive(&receiver, deadline);
604+
raw_response_head.receive(&receiver, deadline)?;
605605

606606
let response = Response::from_head(&raw_response_head)?;
607607
let content_len = response.content_len().unwrap_or(1);
@@ -622,7 +622,7 @@ impl<'a> Request<'a> {
622622

623623
//Receive and process `body`` of the response.
624624
if content_len > 0 {
625-
writer.receive_all(&receiver, deadline);
625+
writer.receive_all(&receiver, deadline)?;
626626
}
627627

628628
Ok(response)
@@ -874,6 +874,16 @@ mod tests {
874874
assert_eq!(request.write_timeout, Some(Duration::from_nanos(100)));
875875
}
876876

877+
#[test]
878+
fn request_timeout() {
879+
let uri = Uri::try_from(URI).unwrap();
880+
let mut request = Request::new(&uri);
881+
let timeout = Duration::from_secs(360);
882+
883+
request.timeout(timeout);
884+
assert_eq!(request.timeout, timeout);
885+
}
886+
877887
#[test]
878888
fn request_send() {
879889
let mut writer = Vec::new();
@@ -885,7 +895,7 @@ mod tests {
885895

886896
#[ignore]
887897
#[test]
888-
fn request_get() {
898+
fn fn_get() {
889899
let mut writer = Vec::new();
890900
let res = get(URI, &mut writer).unwrap();
891901

@@ -899,7 +909,7 @@ mod tests {
899909

900910
#[ignore]
901911
#[test]
902-
fn request_head() {
912+
fn fn_head() {
903913
let res = head(URI).unwrap();
904914
assert_ne!(res.status_code(), UNSUCCESS_CODE);
905915

@@ -909,7 +919,7 @@ mod tests {
909919

910920
#[ignore]
911921
#[test]
912-
fn request_post() {
922+
fn fn_post() {
913923
let mut writer = Vec::new();
914924
let res = post(URI, &BODY, &mut writer).unwrap();
915925

0 commit comments

Comments
 (0)