1 //! Simple HTTP implementation which supports both async and traditional execution environments
2 //! with minimal dependencies. This is used as the basis for REST and RPC clients.
7 use std::convert::TryFrom;
8 #[cfg(not(feature = "tokio"))]
10 use std::net::ToSocketAddrs;
11 use std::time::Duration;
13 #[cfg(feature = "tokio")]
14 use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
15 #[cfg(feature = "tokio")]
16 use tokio::net::TcpStream;
18 #[cfg(not(feature = "tokio"))]
21 #[cfg(not(feature = "tokio"))]
22 use std::net::TcpStream;
24 /// Timeout for operations on TCP streams.
25 const TCP_STREAM_TIMEOUT: Duration = Duration::from_secs(5);
27 /// Timeout for reading the first byte of a response. This is separate from the general read
28 /// timeout as it is not uncommon for Bitcoin Core to be blocked waiting on UTXO cache flushes for
29 /// upwards of a minute or more. Note that we always retry once when we time out, so the maximum
30 /// time we allow Bitcoin Core to block for is twice this value.
31 const TCP_STREAM_RESPONSE_TIMEOUT: Duration = Duration::from_secs(120);
33 /// Maximum HTTP message header size in bytes.
34 const MAX_HTTP_MESSAGE_HEADER_SIZE: usize = 8192;
36 /// Maximum HTTP message body size in bytes. Enough for a hex-encoded block in JSON format and any
37 /// overhead for HTTP chunked transfer encoding.
38 const MAX_HTTP_MESSAGE_BODY_SIZE: usize = 2 * 4_000_000 + 32_000;
40 /// Endpoint for interacting with an HTTP-based API.
42 pub struct HttpEndpoint {
49 /// Creates an endpoint for the given host and default HTTP port.
50 pub fn for_host(host: String) -> Self {
54 path: String::from("/"),
58 /// Specifies a port to use with the endpoint.
59 pub fn with_port(mut self, port: u16) -> Self {
60 self.port = Some(port);
64 /// Specifies a path to use with the endpoint.
65 pub fn with_path(mut self, path: String) -> Self {
70 /// Returns the endpoint host.
71 pub fn host(&self) -> &str {
75 /// Returns the endpoint port.
76 pub fn port(&self) -> u16 {
83 /// Returns the endpoint path.
84 pub fn path(&self) -> &str {
89 impl<'a> std::net::ToSocketAddrs for &'a HttpEndpoint {
90 type Iter = <(&'a str, u16) as std::net::ToSocketAddrs>::Iter;
92 fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
93 (self.host(), self.port()).to_socket_addrs()
97 /// Client for making HTTP requests.
98 pub(crate) struct HttpClient {
103 /// Opens a connection to an HTTP endpoint.
104 pub fn connect<E: ToSocketAddrs>(endpoint: E) -> std::io::Result<Self> {
105 let address = match endpoint.to_socket_addrs()?.next() {
107 return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "could not resolve to any addresses"));
109 Some(address) => address,
111 let stream = std::net::TcpStream::connect_timeout(&address, TCP_STREAM_TIMEOUT)?;
112 stream.set_read_timeout(Some(TCP_STREAM_TIMEOUT))?;
113 stream.set_write_timeout(Some(TCP_STREAM_TIMEOUT))?;
115 #[cfg(feature = "tokio")]
117 stream.set_nonblocking(true)?;
118 TcpStream::from_std(stream)?
124 /// Sends a `GET` request for a resource identified by `uri` at the `host`.
126 /// Returns the response body in `F` format.
128 pub async fn get<F>(&mut self, uri: &str, host: &str) -> std::io::Result<F>
129 where F: TryFrom<Vec<u8>, Error = std::io::Error> {
130 let request = format!(
131 "GET {} HTTP/1.1\r\n\
133 Connection: keep-alive\r\n\
135 let response_body = self.send_request_with_retry(&request).await?;
136 F::try_from(response_body)
139 /// Sends a `POST` request for a resource identified by `uri` at the `host` using the given HTTP
140 /// authentication credentials.
142 /// The request body consists of the provided JSON `content`. Returns the response body in `F`
145 pub async fn post<F>(&mut self, uri: &str, host: &str, auth: &str, content: serde_json::Value) -> std::io::Result<F>
146 where F: TryFrom<Vec<u8>, Error = std::io::Error> {
147 let content = content.to_string();
148 let request = format!(
149 "POST {} HTTP/1.1\r\n\
151 Authorization: {}\r\n\
152 Connection: keep-alive\r\n\
153 Content-Type: application/json\r\n\
154 Content-Length: {}\r\n\
156 {}", uri, host, auth, content.len(), content);
157 let response_body = self.send_request_with_retry(&request).await?;
158 F::try_from(response_body)
161 /// Sends an HTTP request message and reads the response, returning its body. Attempts to
162 /// reconnect and retry if the connection has been closed.
163 async fn send_request_with_retry(&mut self, request: &str) -> std::io::Result<Vec<u8>> {
164 let endpoint = self.stream.peer_addr().unwrap();
165 match self.send_request(request).await {
166 Ok(bytes) => Ok(bytes),
168 // Reconnect and retry on fail. This can happen if the connection was closed after
169 // the keep-alive limits are reached, or generally if the request timed out due to
170 // Bitcoin Core being stuck on a long-running operation or its RPC queue being
172 // Block 100ms before retrying the request as in many cases the source of the error
173 // may be persistent for some time.
174 #[cfg(feature = "tokio")]
175 tokio::time::sleep(Duration::from_millis(100)).await;
176 #[cfg(not(feature = "tokio"))]
177 std::thread::sleep(Duration::from_millis(100));
178 *self = Self::connect(endpoint)?;
179 self.send_request(request).await
184 /// Sends an HTTP request message and reads the response, returning its body.
185 async fn send_request(&mut self, request: &str) -> std::io::Result<Vec<u8>> {
186 self.write_request(request).await?;
187 self.read_response().await
190 /// Writes an HTTP request message.
191 async fn write_request(&mut self, request: &str) -> std::io::Result<()> {
192 #[cfg(feature = "tokio")]
194 self.stream.write_all(request.as_bytes()).await?;
195 self.stream.flush().await
197 #[cfg(not(feature = "tokio"))]
199 self.stream.write_all(request.as_bytes())?;
204 /// Reads an HTTP response message.
205 async fn read_response(&mut self) -> std::io::Result<Vec<u8>> {
206 #[cfg(feature = "tokio")]
207 let stream = self.stream.split().0;
208 #[cfg(not(feature = "tokio"))]
209 let stream = std::io::Read::by_ref(&mut self.stream);
211 let limited_stream = stream.take(MAX_HTTP_MESSAGE_HEADER_SIZE as u64);
213 #[cfg(feature = "tokio")]
214 let mut reader = tokio::io::BufReader::new(limited_stream);
215 #[cfg(not(feature = "tokio"))]
216 let mut reader = std::io::BufReader::new(limited_stream);
218 macro_rules! read_line {
219 () => { read_line!(0) };
220 ($retry_count: expr) => { {
221 let mut line = String::new();
222 let mut timeout_count: u64 = 0;
223 let bytes_read = loop {
224 #[cfg(feature = "tokio")]
225 let read_res = reader.read_line(&mut line).await;
226 #[cfg(not(feature = "tokio"))]
227 let read_res = reader.read_line(&mut line);
229 Ok(bytes_read) => break bytes_read,
230 Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
232 if timeout_count > $retry_count {
238 Err(e) => return Err(e),
245 // Remove trailing CRLF
246 if line.ends_with('\n') { line.pop(); if line.ends_with('\r') { line.pop(); } }
253 // Read and parse status line
254 // Note that we allow retrying a few times to reach TCP_STREAM_RESPONSE_TIMEOUT.
255 let status_line = read_line!(TCP_STREAM_RESPONSE_TIMEOUT.as_secs() / TCP_STREAM_TIMEOUT.as_secs())
256 .ok_or(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "no status line"))?;
257 let status = HttpStatus::parse(&status_line)?;
259 // Read and parse relevant headers
260 let mut message_length = HttpMessageLength::Empty;
262 let line = read_line!()
263 .ok_or(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "no headers"))?;
264 if line.is_empty() { break; }
266 let header = HttpHeader::parse(&line)?;
267 if header.has_name("Content-Length") {
268 let length = header.value.parse()
269 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
270 if let HttpMessageLength::Empty = message_length {
271 message_length = HttpMessageLength::ContentLength(length);
276 if header.has_name("Transfer-Encoding") {
277 message_length = HttpMessageLength::TransferEncoding(header.value.into());
283 // TODO: Handle 3xx redirection responses.
284 return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "not found"));
288 let read_limit = MAX_HTTP_MESSAGE_BODY_SIZE - reader.buffer().len();
289 reader.get_mut().set_limit(read_limit as u64);
290 match message_length {
291 HttpMessageLength::Empty => { Ok(Vec::new()) },
292 HttpMessageLength::ContentLength(length) => {
293 if length == 0 || length > MAX_HTTP_MESSAGE_BODY_SIZE {
294 Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "out of range"))
296 let mut content = vec![0; length];
297 #[cfg(feature = "tokio")]
298 reader.read_exact(&mut content[..]).await?;
299 #[cfg(not(feature = "tokio"))]
300 reader.read_exact(&mut content[..])?;
304 HttpMessageLength::TransferEncoding(coding) => {
305 if !coding.eq_ignore_ascii_case("chunked") {
306 Err(std::io::Error::new(
307 std::io::ErrorKind::InvalidInput, "unsupported transfer coding"))
309 let mut content = Vec::new();
310 #[cfg(feature = "tokio")]
312 // Since chunked_transfer doesn't have an async interface, only use it to
313 // determine the size of each chunk to read.
315 // TODO: Replace with an async interface when available.
316 // https://github.com/frewsxcv/rust-chunked-transfer/issues/7
318 // Read the chunk header which contains the chunk size.
319 let mut chunk_header = String::new();
320 reader.read_line(&mut chunk_header).await?;
321 if chunk_header == "0\r\n" {
322 // Read the terminator chunk since the decoder consumes the CRLF
323 // immediately when this chunk is encountered.
324 reader.read_line(&mut chunk_header).await?;
327 // Decode the chunk header to obtain the chunk size.
328 let mut buffer = Vec::new();
329 let mut decoder = chunked_transfer::Decoder::new(chunk_header.as_bytes());
330 decoder.read_to_end(&mut buffer)?;
332 // Read the chunk body.
333 let chunk_size = match decoder.remaining_chunks_size() {
335 Some(chunk_size) => chunk_size,
337 let chunk_offset = content.len();
338 content.resize(chunk_offset + chunk_size + "\r\n".len(), 0);
339 reader.read_exact(&mut content[chunk_offset..]).await?;
340 content.resize(chunk_offset + chunk_size, 0);
344 #[cfg(not(feature = "tokio"))]
346 let mut decoder = chunked_transfer::Decoder::new(reader);
347 decoder.read_to_end(&mut content)?;
356 /// HTTP response status code as defined by [RFC 7231].
358 /// [RFC 7231]: https://tools.ietf.org/html/rfc7231#section-6
359 struct HttpStatus<'a> {
363 impl<'a> HttpStatus<'a> {
364 /// Parses an HTTP status line as defined by [RFC 7230].
366 /// [RFC 7230]: https://tools.ietf.org/html/rfc7230#section-3.1.2
367 fn parse(line: &'a String) -> std::io::Result<HttpStatus<'a>> {
368 let mut tokens = line.splitn(3, ' ');
370 let http_version = tokens.next()
371 .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no HTTP-Version"))?;
372 if !http_version.eq_ignore_ascii_case("HTTP/1.1") &&
373 !http_version.eq_ignore_ascii_case("HTTP/1.0") {
374 return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid HTTP-Version"));
377 let code = tokens.next()
378 .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no Status-Code"))?;
379 if code.len() != 3 || !code.chars().all(|c| c.is_ascii_digit()) {
380 return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid Status-Code"));
383 let _reason = tokens.next()
384 .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no Reason-Phrase"))?;
389 /// Returns whether the status is successful (i.e., 2xx status class).
390 fn is_ok(&self) -> bool {
391 self.code.starts_with('2')
395 /// HTTP response header as defined by [RFC 7231].
397 /// [RFC 7231]: https://tools.ietf.org/html/rfc7231#section-7
398 struct HttpHeader<'a> {
403 impl<'a> HttpHeader<'a> {
404 /// Parses an HTTP header field as defined by [RFC 7230].
406 /// [RFC 7230]: https://tools.ietf.org/html/rfc7230#section-3.2
407 fn parse(line: &'a String) -> std::io::Result<HttpHeader<'a>> {
408 let mut tokens = line.splitn(2, ':');
409 let name = tokens.next()
410 .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no header name"))?;
411 let value = tokens.next()
412 .ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "no header value"))?
414 Ok(Self { name, value })
417 /// Returns whether the header field has the given name.
418 fn has_name(&self, name: &str) -> bool {
419 self.name.eq_ignore_ascii_case(name)
423 /// HTTP message body length as defined by [RFC 7230].
425 /// [RFC 7230]: https://tools.ietf.org/html/rfc7230#section-3.3.3
426 enum HttpMessageLength {
428 ContentLength(usize),
429 TransferEncoding(String),
432 /// An HTTP response body in binary format.
433 pub struct BinaryResponse(pub Vec<u8>);
435 /// An HTTP response body in JSON format.
436 pub struct JsonResponse(pub serde_json::Value);
438 /// Interprets bytes from an HTTP response body as binary data.
439 impl TryFrom<Vec<u8>> for BinaryResponse {
440 type Error = std::io::Error;
442 fn try_from(bytes: Vec<u8>) -> std::io::Result<Self> {
443 Ok(BinaryResponse(bytes))
447 /// Interprets bytes from an HTTP response body as a JSON value.
448 impl TryFrom<Vec<u8>> for JsonResponse {
449 type Error = std::io::Error;
451 fn try_from(bytes: Vec<u8>) -> std::io::Result<Self> {
452 Ok(JsonResponse(serde_json::from_slice(&bytes)?))
458 use super::HttpEndpoint;
461 fn with_default_port() {
462 let endpoint = HttpEndpoint::for_host("foo.com".into());
463 assert_eq!(endpoint.host(), "foo.com");
464 assert_eq!(endpoint.port(), 80);
468 fn with_custom_port() {
469 let endpoint = HttpEndpoint::for_host("foo.com".into()).with_port(8080);
470 assert_eq!(endpoint.host(), "foo.com");
471 assert_eq!(endpoint.port(), 8080);
476 let endpoint = HttpEndpoint::for_host("foo.com".into()).with_path("/path".into());
477 assert_eq!(endpoint.host(), "foo.com");
478 assert_eq!(endpoint.path(), "/path");
482 fn without_uri_path() {
483 let endpoint = HttpEndpoint::for_host("foo.com".into());
484 assert_eq!(endpoint.host(), "foo.com");
485 assert_eq!(endpoint.path(), "/");
489 fn convert_to_socket_addrs() {
490 let endpoint = HttpEndpoint::for_host("foo.com".into());
491 let host = endpoint.host();
492 let port = endpoint.port();
494 use std::net::ToSocketAddrs;
495 match (&endpoint).to_socket_addrs() {
496 Err(e) => panic!("Unexpected error: {:?}", e),
497 Ok(mut socket_addrs) => {
498 match socket_addrs.next() {
499 None => panic!("Expected socket address"),
501 assert_eq!(addr, (host, port).to_socket_addrs().unwrap().next().unwrap());
502 assert!(socket_addrs.next().is_none());
511 pub(crate) mod client_tests {
513 use std::io::BufRead;
516 /// Server for handling HTTP client requests with a stock response.
517 pub struct HttpServer {
518 address: std::net::SocketAddr,
519 handler: std::thread::JoinHandle<()>,
520 shutdown: std::sync::Arc<std::sync::atomic::AtomicBool>,
523 /// Body of HTTP response messages.
524 pub enum MessageBody<T: ToString> {
531 pub fn responding_with_ok<T: ToString>(body: MessageBody<T>) -> Self {
532 let response = match body {
533 MessageBody::Empty => "HTTP/1.1 200 OK\r\n\r\n".to_string(),
534 MessageBody::Content(body) => {
535 let body = body.to_string();
537 "HTTP/1.1 200 OK\r\n\
538 Content-Length: {}\r\n\
540 {}", body.len(), body)
542 MessageBody::ChunkedContent(body) => {
543 let mut chuncked_body = Vec::new();
545 use chunked_transfer::Encoder;
546 let mut encoder = Encoder::with_chunks_size(&mut chuncked_body, 8);
547 encoder.write_all(body.to_string().as_bytes()).unwrap();
550 "HTTP/1.1 200 OK\r\n\
551 Transfer-Encoding: chunked\r\n\
553 {}", String::from_utf8(chuncked_body).unwrap())
556 HttpServer::responding_with(response)
559 pub fn responding_with_not_found() -> Self {
560 let response = "HTTP/1.1 404 Not Found\r\n\r\n".to_string();
561 HttpServer::responding_with(response)
564 fn responding_with(response: String) -> Self {
565 let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
566 let address = listener.local_addr().unwrap();
568 let shutdown = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
569 let shutdown_signaled = std::sync::Arc::clone(&shutdown);
570 let handler = std::thread::spawn(move || {
571 for stream in listener.incoming() {
572 let mut stream = stream.unwrap();
573 stream.set_write_timeout(Some(TCP_STREAM_TIMEOUT)).unwrap();
575 let lines_read = std::io::BufReader::new(&stream)
577 .take_while(|line| !line.as_ref().unwrap().is_empty())
579 if lines_read == 0 { continue; }
581 for chunk in response.as_bytes().chunks(16) {
582 if shutdown_signaled.load(std::sync::atomic::Ordering::SeqCst) {
585 if let Err(_) = stream.write(chunk) { break; }
586 if let Err(_) = stream.flush() { break; }
592 Self { address, handler, shutdown }
596 self.shutdown.store(true, std::sync::atomic::Ordering::SeqCst);
597 self.handler.join().unwrap();
600 pub fn endpoint(&self) -> HttpEndpoint {
601 HttpEndpoint::for_host(self.address.ip().to_string()).with_port(self.address.port())
606 fn connect_to_unresolvable_host() {
607 match HttpClient::connect(("example.invalid", 80)) {
608 Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::Other),
609 Ok(_) => panic!("Expected error"),
614 fn connect_with_no_socket_address() {
615 match HttpClient::connect(&vec![][..]) {
616 Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput),
617 Ok(_) => panic!("Expected error"),
622 fn connect_with_unknown_server() {
623 match HttpClient::connect(("::", 80)) {
624 #[cfg(target_os = "windows")]
625 Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::AddrNotAvailable),
626 #[cfg(not(target_os = "windows"))]
627 Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::ConnectionRefused),
628 Ok(_) => panic!("Expected error"),
633 async fn connect_with_valid_endpoint() {
634 let server = HttpServer::responding_with_ok::<String>(MessageBody::Empty);
636 match HttpClient::connect(&server.endpoint()) {
637 Err(e) => panic!("Unexpected error: {:?}", e),
643 async fn read_empty_message() {
644 let server = HttpServer::responding_with("".to_string());
646 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
647 match client.get::<BinaryResponse>("/foo", "foo.com").await {
649 assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof);
650 assert_eq!(e.get_ref().unwrap().to_string(), "no status line");
652 Ok(_) => panic!("Expected error"),
657 async fn read_incomplete_message() {
658 let server = HttpServer::responding_with("HTTP/1.1 200 OK".to_string());
660 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
661 match client.get::<BinaryResponse>("/foo", "foo.com").await {
663 assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof);
664 assert_eq!(e.get_ref().unwrap().to_string(), "no headers");
666 Ok(_) => panic!("Expected error"),
671 async fn read_too_large_message_headers() {
672 let response = format!(
673 "HTTP/1.1 302 Found\r\n\
675 \r\n", "Z".repeat(MAX_HTTP_MESSAGE_HEADER_SIZE));
676 let server = HttpServer::responding_with(response);
678 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
679 match client.get::<BinaryResponse>("/foo", "foo.com").await {
681 assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof);
682 assert_eq!(e.get_ref().unwrap().to_string(), "no headers");
684 Ok(_) => panic!("Expected error"),
689 async fn read_too_large_message_body() {
690 let body = "Z".repeat(MAX_HTTP_MESSAGE_BODY_SIZE + 1);
691 let server = HttpServer::responding_with_ok::<String>(MessageBody::Content(body));
693 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
694 match client.get::<BinaryResponse>("/foo", "foo.com").await {
696 assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
697 assert_eq!(e.get_ref().unwrap().to_string(), "out of range");
699 Ok(_) => panic!("Expected error"),
705 async fn read_message_with_unsupported_transfer_coding() {
706 let response = String::from(
707 "HTTP/1.1 200 OK\r\n\
708 Transfer-Encoding: gzip\r\n\
711 let server = HttpServer::responding_with(response);
713 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
714 match client.get::<BinaryResponse>("/foo", "foo.com").await {
716 assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
717 assert_eq!(e.get_ref().unwrap().to_string(), "unsupported transfer coding");
719 Ok(_) => panic!("Expected error"),
724 async fn read_empty_message_body() {
725 let server = HttpServer::responding_with_ok::<String>(MessageBody::Empty);
727 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
728 match client.get::<BinaryResponse>("/foo", "foo.com").await {
729 Err(e) => panic!("Unexpected error: {:?}", e),
730 Ok(bytes) => assert_eq!(bytes.0, Vec::<u8>::new()),
735 async fn read_message_body_with_length() {
736 let body = "foo bar baz qux".repeat(32);
737 let content = MessageBody::Content(body.clone());
738 let server = HttpServer::responding_with_ok::<String>(content);
740 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
741 match client.get::<BinaryResponse>("/foo", "foo.com").await {
742 Err(e) => panic!("Unexpected error: {:?}", e),
743 Ok(bytes) => assert_eq!(bytes.0, body.as_bytes()),
748 async fn read_chunked_message_body() {
749 let body = "foo bar baz qux".repeat(32);
750 let chunked_content = MessageBody::ChunkedContent(body.clone());
751 let server = HttpServer::responding_with_ok::<String>(chunked_content);
753 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
754 match client.get::<BinaryResponse>("/foo", "foo.com").await {
755 Err(e) => panic!("Unexpected error: {:?}", e),
756 Ok(bytes) => assert_eq!(bytes.0, body.as_bytes()),
761 async fn reconnect_closed_connection() {
762 let server = HttpServer::responding_with_ok::<String>(MessageBody::Empty);
764 let mut client = HttpClient::connect(&server.endpoint()).unwrap();
765 assert!(client.get::<BinaryResponse>("/foo", "foo.com").await.is_ok());
766 match client.get::<BinaryResponse>("/foo", "foo.com").await {
767 Err(e) => panic!("Unexpected error: {:?}", e),
768 Ok(bytes) => assert_eq!(bytes.0, Vec::<u8>::new()),
773 fn from_bytes_into_binary_response() {
775 match BinaryResponse::try_from(bytes.to_vec()) {
776 Err(e) => panic!("Unexpected error: {:?}", e),
777 Ok(response) => assert_eq!(&response.0, bytes),
782 fn from_invalid_bytes_into_json_response() {
783 let json = serde_json::json!({ "result": 42 });
784 match JsonResponse::try_from(json.to_string().as_bytes()[..5].to_vec()) {
786 Ok(_) => panic!("Expected error"),
791 fn from_valid_bytes_into_json_response() {
792 let json = serde_json::json!({ "result": 42 });
793 match JsonResponse::try_from(json.to_string().as_bytes().to_vec()) {
794 Err(e) => panic!("Unexpected error: {:?}", e),
795 Ok(response) => assert_eq!(response.0, json),