File: unix-test.rs

package info (click to toggle)
rust-tiny-http 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 416 kB
  • sloc: makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,045 bytes parent folder | download
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
44
#![cfg(unix)]

extern crate tiny_http;

use std::{
    io::{Read, Write},
    os::unix::net::UnixStream,
    path::{Path, PathBuf},
};

#[allow(dead_code)]
mod support;

#[test]
fn unix_basic_handling() {
    let server = tiny_http::Server::http_unix(Path::new("/tmp/tiny-http-test.sock")).unwrap();
    let path: PathBuf = server
        .server_addr()
        .to_unix()
        .unwrap()
        .as_pathname()
        .unwrap()
        .into();
    let mut client = UnixStream::connect(path).unwrap();

    write!(
        client,
        "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
    )
    .unwrap();

    let request = server.recv().unwrap();
    assert!(*request.method() == tiny_http::Method::Get);
    //assert!(request.url() == "/");
    request
        .respond(tiny_http::Response::from_string("hello world".to_owned()))
        .unwrap();

    server.try_recv().unwrap();

    let mut content = String::new();
    client.read_to_string(&mut content).unwrap();
    assert!(content.ends_with("hello world"));
}