Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
6 changes: 4 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ where
{
pub(crate) buffer: ReadBuffer<'buf>,
pub(crate) stream: &'resp mut B,
pub(crate) force_local_buffer: bool,
}

impl<'resp, 'buf, B> BufferingReader<'resp, 'buf, B>
Expand All @@ -58,6 +59,7 @@ where
Self {
buffer: ReadBuffer::new(buffer, loaded),
stream,
force_local_buffer: false,
}
}
}
Expand Down Expand Up @@ -88,10 +90,10 @@ where
C: TryBufRead,
{
async fn fill_buf(&mut self) -> Result<&[u8], ErrorKind> {
// We need to consume the loaded bytes before we read mode.
// We need to consume the loaded bytes before we read more.
if self.buffer.is_empty() {
// The matches/if let dance is to fix lifetime of the borrowed inner connection.
if self.stream.try_fill_buf().await.is_some() {
if !self.force_local_buffer && self.stream.try_fill_buf().await.is_some() {
if let Some(result) = self.stream.try_fill_buf().await {
return result.map_err(|e| e.kind());
}
Expand Down
10 changes: 6 additions & 4 deletions src/response/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,25 @@ where
loaded: self.raw_body.buffer.loaded,
},
stream: self.raw_body.stream,
force_local_buffer: true,
},
chunk_remaining: self.chunk_remaining,
};

let mut len = 0;
while !reader.raw_body.buffer.buffer.is_empty() {
// Read some
let read = reader.fill_buf().await?.len();
len += read;
let read = reader.fill_buf().await?;
let read_len = read.len();
len += read_len;

// Make sure we don't erase the newly read data
let was_loaded = reader.raw_body.buffer.loaded;
let fake_loaded = read.min(was_loaded);
let fake_loaded = read_len.min(was_loaded);
reader.raw_body.buffer.loaded = fake_loaded;

// Consume the returned buffer
reader.consume(read);
reader.consume(read_len);

if reader.is_done() {
// If we're done, we don't care about the rest of the housekeeping.
Expand Down
36 changes: 36 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,42 @@ async fn test_resource_drogue_cloud_sandbox() {
}
}

#[ignore]
#[tokio::test]
#[cfg(feature = "embedded-tls")]
async fn test_request_response_tls_chunked() {
use core::str;
use std::fs;

use reqwless::client::{TlsConfig, TlsVerify};

setup();
let mut tls_read_buf: [u8; 16384] = [0; 16384];
let mut tls_write_buf: [u8; 16384] = [0; 16384];
let mut client = HttpClient::new_with_tls(
&TCP,
&PUBLIC_DNS,
TlsConfig::new(OsRng.next_u64(), &mut tls_read_buf, &mut tls_write_buf, TlsVerify::None),
);
let mut rx_buf = [0; 8192];

// The server must support TLS1.3
// Also, if requests on embedded platforms fail with Error::Dns, then try to
// enable the "alloc" feature on embedded-tls to enable RSA ciphers.
let mut request = client
.request(Method::GET, "https://api.dictionaryapi.dev/api/v2/entries/en/orange")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test this without actually hitting this external API? We probably don't want the test to start failing if this website ever goes dark.

.await
.unwrap();

let response = request.send(&mut rx_buf).await.unwrap();

let body = str::from_utf8(response.body().read_to_end().await.unwrap()).unwrap();
fs::write("tests/orange-actual.json", body).unwrap();

let expected = std::fs::read_to_string("tests/orange.json").unwrap();
assert_eq!(expected, body);
}

#[tokio::test]
async fn test_request_response_notls_buffered() {
setup();
Expand Down
1 change: 1 addition & 0 deletions tests/orange.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"word":"orange","phonetic":"/ˈɔɹɪ̈nd͡ʒ/","phonetics":[{"text":"/ˈɔɹɪ̈nd͡ʒ/","audio":""},{"text":"/ˈɒɹɨn(d)ʒ/","audio":""},{"text":"/ˈɑɹɪ̈nd͡ʒ/","audio":""}],"meanings":[{"partOfSpeech":"noun","definitions":[{"definition":"An evergreen tree of the genus Citrus such as Citrus sinensis.","synonyms":[],"antonyms":[]},{"definition":"The fruit of an orange tree; a citrus fruit with a slightly sour flavour.","synonyms":[],"antonyms":[]},{"definition":"The colour of a ripe fruit of an orange tree, midway between red and yellow.","synonyms":["yellowred"],"antonyms":[]},{"definition":"Orange juice.","synonyms":[],"antonyms":[]},{"definition":"Orange coloured and flavoured cordial.","synonyms":[],"antonyms":[]},{"definition":"Orange coloured and flavoured soft drink.","synonyms":[],"antonyms":[]}],"synonyms":["yellowred"],"antonyms":[]},{"partOfSpeech":"verb","definitions":[{"definition":"To color orange.","synonyms":[],"antonyms":[]},{"definition":"To become orange.","synonyms":[],"antonyms":[]}],"synonyms":[],"antonyms":[]},{"partOfSpeech":"adjective","definitions":[{"definition":"Having the colour of the fruit of an orange tree; yellowred; reddish-yellow.","synonyms":[],"antonyms":[]}],"synonyms":[],"antonyms":["nonorange"]}],"license":{"name":"CC BY-SA 3.0","url":"https://creativecommons.org/licenses/by-sa/3.0"},"sourceUrls":["https://en.wiktionary.org/wiki/orange"]}]
Loading