-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathserver_spec.rs
269 lines (237 loc) · 8.55 KB
/
server_spec.rs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#[cfg(test)]
pub mod test {
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{anyhow, Result};
use async_graphql::Value;
use http_cache_reqwest::{Cache, CacheMode, HttpCache, HttpCacheOptions};
use hyper::body::Bytes;
use reqwest::Client;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use tailcall::cli::javascript::init_worker_io;
use tailcall::core::blueprint::{Script, Upstream};
use tailcall::core::cache::InMemoryCache;
use tailcall::core::http::Response;
use tailcall::core::runtime::TargetRuntime;
use tailcall::core::worker::{Command, Event};
use tailcall::core::{EnvIO, FileIO, HttpIO};
use tailcall_http_cache::HttpCacheManager;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[derive(Clone)]
struct TestHttp {
client: ClientWithMiddleware,
}
impl Default for TestHttp {
fn default() -> Self {
Self { client: ClientBuilder::new(Client::new()).build() }
}
}
impl TestHttp {
fn init(upstream: &Upstream) -> Arc<Self> {
let mut builder = Client::builder()
.tcp_keepalive(Some(Duration::from_secs(upstream.tcp_keep_alive)))
.timeout(Duration::from_secs(upstream.timeout))
.connect_timeout(Duration::from_secs(upstream.connect_timeout))
.http2_keep_alive_interval(Some(Duration::from_secs(upstream.keep_alive_interval)))
.http2_keep_alive_timeout(Duration::from_secs(upstream.keep_alive_timeout))
.http2_keep_alive_while_idle(upstream.keep_alive_while_idle)
.pool_idle_timeout(Some(Duration::from_secs(upstream.pool_idle_timeout)))
.pool_max_idle_per_host(upstream.pool_max_idle_per_host)
.user_agent(upstream.user_agent.clone())
.danger_accept_invalid_certs(!upstream.verify_ssl);
// Add Http2 Prior Knowledge
if upstream.http2_only {
builder = builder.http2_prior_knowledge();
}
// Add Http Proxy
if let Some(ref proxy) = upstream.proxy {
builder = builder.proxy(
reqwest::Proxy::http(proxy.url.clone())
.expect("Failed to set proxy in http client"),
);
}
let mut client = ClientBuilder::new(builder.build().expect("Failed to build client"));
if upstream.http_cache > 0 {
client = client.with(Cache(HttpCache {
mode: CacheMode::Default,
manager: HttpCacheManager::new(upstream.http_cache),
options: HttpCacheOptions::default(),
}))
}
Arc::new(Self { client: client.build() })
}
}
#[async_trait::async_trait]
impl HttpIO for TestHttp {
async fn execute(&self, request: reqwest::Request) -> Result<Response<Bytes>> {
let response = self.client.execute(request).await;
Response::from_reqwest(
response?
.error_for_status()
.map_err(|err| err.without_url())?,
)
.await
}
}
#[derive(Clone)]
struct TestFileIO {}
impl TestFileIO {
fn init() -> Self {
TestFileIO {}
}
}
#[async_trait::async_trait]
impl FileIO for TestFileIO {
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> anyhow::Result<()> {
let mut file = tokio::fs::File::create(path).await?;
file.write_all(content)
.await
.map_err(|e| anyhow!("{}", e))?;
Ok(())
}
async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
let mut file = tokio::fs::File::open(path).await?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.await
.map_err(|e| anyhow!("{}", e))?;
Ok(String::from_utf8(buffer)?)
}
}
#[derive(Clone)]
struct TestEnvIO {
vars: HashMap<String, String>,
}
impl EnvIO for TestEnvIO {
fn get(&self, key: &str) -> Option<Cow<'_, str>> {
self.vars.get(key).map(Cow::from)
}
}
impl TestEnvIO {
pub fn init() -> Self {
Self { vars: std::env::vars().collect() }
}
}
pub fn init(script: Option<Script>) -> TargetRuntime {
let http = TestHttp::init(&Default::default());
let http2 = TestHttp::init(&Upstream::default().http2_only(true));
let file = TestFileIO::init();
let env = TestEnvIO::init();
TargetRuntime {
http,
http2_only: http2,
env: Arc::new(env),
file: Arc::new(file),
cache: Arc::new(InMemoryCache::default()),
extensions: Arc::new(vec![]),
cmd_worker: match &script {
Some(script) => Some(init_worker_io::<Event, Command>(script.to_owned())),
None => None,
},
worker: match &script {
Some(script) => Some(init_worker_io::<Value, Value>(script.to_owned())),
None => None,
},
}
}
}
#[cfg(test)]
mod server_spec {
use reqwest::Client;
use serde_json::json;
use tailcall::cli::server::Server;
use tailcall::core::config::reader::ConfigReader;
async fn test_server(configs: &[&str], url: &str) {
let runtime = crate::test::init(None);
let reader = ConfigReader::init(runtime);
let config = reader.read_all(configs).await.unwrap();
let mut server = Server::new(config);
let server_up_receiver = server.server_up_receiver();
tokio::spawn(async move {
server.start().await.unwrap();
});
server_up_receiver
.await
.expect("Server did not start up correctly");
// required since our cert is self signed
let client = Client::builder()
.use_rustls_tls()
.danger_accept_invalid_certs(true)
.build()
.unwrap();
let query = json!({
"query": "{ greet }"
});
let mut tasks = vec![];
for _ in 0..100 {
let client = client.clone();
let url = url.to_owned();
let query = query.clone();
let task: tokio::task::JoinHandle<Result<serde_json::Value, anyhow::Error>> =
tokio::spawn(async move {
let response = client.post(url).json(&query).send().await?;
let response_body: serde_json::Value = response.json().await?;
Ok(response_body)
});
tasks.push(task);
}
for task in tasks {
let response_body = task
.await
.expect("Spawned task should success")
.expect("Request should success");
let expected_response = json!({
"data": {
"greet": "Hello World!"
}
});
assert_eq!(
response_body, expected_response,
"Unexpected response from server"
);
}
}
#[tokio::test]
async fn server_start() {
test_server(
&["tests/server/config/server-start.graphql"],
"http://localhost:8800/graphql",
)
.await
}
#[tokio::test]
async fn server_start_http2_pcks8() {
test_server(
&["tests/server/config/server-start-http2-pkcs8.graphql"],
"https://localhost:8801/graphql",
)
.await
}
#[tokio::test]
async fn server_start_http2_rsa() {
test_server(
&["tests/server/config/server-start-http2-rsa.graphql"],
"https://localhost:8802/graphql",
)
.await
}
#[tokio::test]
async fn server_start_http2_nokey() {
let configs = &["tests/server/config/server-start-http2-nokey.graphql"];
let runtime = crate::test::init(None);
let reader = ConfigReader::init(runtime);
let config = reader.read_all(configs).await.unwrap();
let server = Server::new(config);
assert!(server.start().await.is_err())
}
#[tokio::test]
async fn server_start_http2_ec() {
test_server(
&["tests/server/config/server-start-http2-ec.graphql"],
"https://localhost:8804/graphql",
)
.await
}
}