-
Notifications
You must be signed in to change notification settings - Fork 245
/
mod.rs
230 lines (207 loc) 路 8.65 KB
/
mod.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
//TODO move telemetry logging functionality to this file
#[cfg(test)]
mod test {
use std::any::TypeId;
use tower::BoxError;
use tower::ServiceBuilder;
use tower_service::Service;
use tracing_futures::WithSubscriber;
use crate::assert_snapshot_subscriber;
use crate::graphql;
use crate::plugin::DynPlugin;
use crate::plugin::Plugin;
use crate::plugins::telemetry::Telemetry;
use crate::services::router;
use crate::services::subgraph;
use crate::services::supergraph;
#[tokio::test(flavor = "multi_thread")]
async fn test_router_service() {
let test_harness: PluginTestHarness<Telemetry> = PluginTestHarness::builder().build().await;
async {
let mut response = test_harness
.call_router(
router::Request::fake_builder()
.body("query { foo }")
.build()
.expect("expecting valid request"),
|_r| {
tracing::info!("response");
router::Response::fake_builder()
.header("custom-header", "val1")
.data(serde_json::json!({"data": "res"}))
.build()
.expect("expecting valid response")
},
)
.await
.expect("expecting successful response");
response.next_response().await;
}
.with_subscriber(assert_snapshot_subscriber!())
.await
}
#[tokio::test(flavor = "multi_thread")]
async fn test_supergraph_service() {
let test_harness: PluginTestHarness<Telemetry> = PluginTestHarness::builder().build().await;
async {
let mut response = test_harness
.call_supergraph(
supergraph::Request::fake_builder()
.query("query { foo }")
.variable("a", "b")
.build()
.expect("expecting valid request"),
|_r| {
tracing::info!("response");
supergraph::Response::fake_builder()
.header("custom-header", "val1")
.data(serde_json::json!({"data": "res"}))
.build()
.expect("expecting valid response")
},
)
.await
.expect("expecting successful response");
response.next_response().await;
}
.with_subscriber(assert_snapshot_subscriber!())
.await
}
#[tokio::test(flavor = "multi_thread")]
async fn test_subgraph_service() {
let test_harness: PluginTestHarness<Telemetry> = PluginTestHarness::builder().build().await;
async {
test_harness
.call_subgraph(
subgraph::Request::fake_builder()
.subgraph_name("subgraph")
.subgraph_request(http::Request::new(
graphql::Request::fake_builder()
.query("query { foo }")
.build(),
))
.build(),
|_r| {
tracing::info!("response");
subgraph::Response::fake2_builder()
.header("custom-header", "val1")
.data(serde_json::json!({"data": "res"}).to_string())
.build()
.expect("expecting valid response")
},
)
.await
.expect("expecting successful response");
}
.with_subscriber(assert_snapshot_subscriber!())
.await
}
#[tokio::test(flavor = "multi_thread")]
async fn test_when_header() {
let test_harness: PluginTestHarness<Telemetry> = PluginTestHarness::builder()
.yaml(include_str!(
"testdata/experimental_when_header.router.yaml"
))
.build()
.await;
async {
let mut response = test_harness
.call_supergraph(
supergraph::Request::fake_builder()
.header("custom-header1", "val1")
.header("custom-header2", "val2")
.query("query { foo }")
.build()
.expect("expecting valid request"),
|_r| {
tracing::info!("response");
supergraph::Response::fake_builder()
.header("custom-header1", "val1")
.header("custom-header2", "val2")
.data(serde_json::json!({"data": "res"}))
.build()
.expect("expecting valid response")
},
)
.await
.expect("expecting successful response");
response.next_response().await;
}
.with_subscriber(assert_snapshot_subscriber!())
.await
}
// Maybe factor this out after making it more usable
// The difference with this and the `TestHarness` is that this has much less of the router being wired up and is useful for testing a single plugin in isolation.
// In particular the `TestHarness` isn't good for testing things with logging.
// For now let's try and increase the coverage of the telemetry plugin using this and see how it goes.
struct PluginTestHarness<T: Plugin> {
plugin: Box<dyn DynPlugin>,
phantom: std::marker::PhantomData<T>,
}
#[buildstructor::buildstructor]
impl<T: Plugin> PluginTestHarness<T> {
#[builder]
async fn new(yaml: Option<&'static str>) -> Self {
let factory = crate::plugin::plugins()
.find(|factory| factory.type_id == TypeId::of::<T>())
.expect("plugin not registered");
let name = &factory.name.replace("apollo.", "");
let config = yaml
.map(|yaml| serde_yaml::from_str::<serde_json::Value>(yaml).unwrap())
.map(|mut config| {
config
.as_object_mut()
.expect("invalid yaml")
.remove(name)
.expect("no config for plugin")
})
.unwrap_or_else(|| serde_json::Value::Object(Default::default()));
let plugin = factory
.create_instance_without_schema(&config)
.await
.expect("failed to create plugin");
Self {
plugin,
phantom: Default::default(),
}
}
#[allow(dead_code)]
async fn call_router(
&self,
request: router::Request,
response_fn: fn(router::Request) -> router::Response,
) -> Result<router::Response, BoxError> {
let service: router::BoxService = router::BoxService::new(
ServiceBuilder::new()
.service_fn(move |req: router::Request| async move { Ok((response_fn)(req)) }),
);
self.plugin.router_service(service).call(request).await
}
async fn call_supergraph(
&self,
request: supergraph::Request,
response_fn: fn(supergraph::Request) -> supergraph::Response,
) -> Result<supergraph::Response, BoxError> {
let service: supergraph::BoxService =
supergraph::BoxService::new(ServiceBuilder::new().service_fn(
move |req: supergraph::Request| async move { Ok((response_fn)(req)) },
));
self.plugin.supergraph_service(service).call(request).await
}
async fn call_subgraph(
&self,
request: subgraph::Request,
response_fn: fn(subgraph::Request) -> subgraph::Response,
) -> Result<subgraph::Response, BoxError> {
let name = request.subgraph_name.clone();
let service: subgraph::BoxService =
subgraph::BoxService::new(ServiceBuilder::new().service_fn(
move |req: subgraph::Request| async move { Ok((response_fn)(req)) },
));
self.plugin
.subgraph_service(&name.expect("subgraph name must be populated"), service)
.call(request)
.await
}
}
}