File: unix.rs

package info (click to toggle)
rust-isahc 1.7.2%2Bds-20
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,044 kB
  • sloc: perl: 258; python: 148; makefile: 24; sh: 1
file content (43 lines) | stat: -rw-r--r-- 1,084 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#![cfg(unix)]

use isahc::{config::Dialer, prelude::*, Request};
use std::{
    io::{self, Write},
    os::unix::net::UnixListener,
    thread,
};
use tempfile::TempDir;

#[cfg(feature = "broken_response_text")]
#[test]
#[rustfmt::skip]
fn send_request_to_unix_socket() {
    let temp_dir = TempDir::new().unwrap();
    let socket_path = temp_dir.path().join("test.sock");
    let listener = UnixListener::bind(&socket_path).unwrap();

    thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut reader = stream.try_clone().unwrap();

        thread::spawn(move || {
            io::copy(&mut reader, &mut io::sink()).unwrap();
        });

        stream.write_all(b"\
            HTTP/1.1 200 OK\r\n\
            Content-Length: 8\r\n\
            \r\n\
            success\n\
        ").unwrap();
    });

    let mut response = Request::get("http://localhost")
        .dial(Dialer::unix_socket(socket_path))
        .body(())
        .unwrap()
        .send()
        .unwrap();

    assert_eq!(response.text().unwrap(), "success\n");
}