summaryrefslogtreecommitdiff
path: root/tests/integration/serve_tests.rs
blob: cfe7e4d6a8ca2dba095bab520554de16e5c68934 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::io::Read;

use pretty_assertions::assert_eq;
use regex::Regex;
use test_util as util;

#[tokio::test]
async fn deno_serve_port_0() {
  let mut child = util::deno_cmd()
    .current_dir(util::testdata_path())
    .arg("serve")
    .arg("--port")
    .arg("0")
    .arg("./serve/port_0.ts")
    .stdout_piped()
    .spawn()
    .unwrap();
  let stdout = child.stdout.as_mut().unwrap();
  let mut buffer = [0; 52];
  let _read = stdout.read(&mut buffer).unwrap();
  let msg = std::str::from_utf8(&buffer).unwrap();
  let port_regex = Regex::new(r":(\d+)").unwrap();
  let port = port_regex.captures(msg).unwrap().get(1).unwrap().as_str();

  let cert = reqwest::Certificate::from_pem(include_bytes!(
    "../testdata/tls/RootCA.crt"
  ))
  .unwrap();

  let client = reqwest::Client::builder()
    .add_root_certificate(cert)
    .http2_prior_knowledge()
    .build()
    .unwrap();

  let res = client
    .get(&format!("http://127.0.0.1:{port}"))
    .send()
    .await
    .unwrap();
  assert_eq!(200, res.status());

  let body = res.text().await.unwrap();
  assert_eq!(body, "deno serve --port 0 works!");

  child.kill().unwrap();
  child.wait().unwrap();
}

#[tokio::test]
async fn deno_serve_no_args() {
  let mut child = util::deno_cmd()
    .current_dir(util::testdata_path())
    .arg("serve")
    .arg("--port")
    .arg("0")
    .arg("./serve/no_args.ts")
    .stdout_piped()
    .spawn()
    .unwrap();
  let stdout = child.stdout.as_mut().unwrap();
  let mut buffer = [0; 52];
  let _read = stdout.read(&mut buffer).unwrap();
  let msg = std::str::from_utf8(&buffer).unwrap();
  let port_regex = Regex::new(r":(\d+)").unwrap();
  let port = port_regex.captures(msg).unwrap().get(1).unwrap().as_str();

  let cert = reqwest::Certificate::from_pem(include_bytes!(
    "../testdata/tls/RootCA.crt"
  ))
  .unwrap();

  let client = reqwest::Client::builder()
    .add_root_certificate(cert)
    .http2_prior_knowledge()
    .build()
    .unwrap();

  let res = client
    .get(&format!("http://127.0.0.1:{port}"))
    .send()
    .await
    .unwrap();
  assert_eq!(200, res.status());

  let body = res.text().await.unwrap();
  assert_eq!(body, "deno serve with no args in fetch() works!");

  child.kill().unwrap();
  child.wait().unwrap();
}