diff --git a/Cargo.lock b/Cargo.lock index 6dba656..d83a4c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,6 +336,7 @@ dependencies = [ "async-graphql-axum", "axum", "dotenvy", + "futures", "http-body-util", "insta", "metrics", @@ -445,6 +446,16 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "bstr" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -834,6 +845,21 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.31" @@ -913,6 +939,7 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ + "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -957,6 +984,19 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "globset" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata 0.4.8", + "regex-syntax 0.8.5", +] + [[package]] name = "h2" version = "0.4.6" @@ -1365,10 +1405,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8" dependencies = [ "console", + "globset", "lazy_static", "linked-hash-map", + "pest", + "pest_derive", "serde", "similar", + "walkdir", ] [[package]] @@ -2271,6 +2315,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.26" @@ -3251,6 +3304,16 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -3394,6 +3457,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index c233a14..bb904b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,8 +49,9 @@ tracing-opentelemetry = "0.27.0" tracing-subscriber = { version = "0.3.18", features = ["std", "env-filter"] } [dev-dependencies] +futures = "0.3.31" http-body-util = "0.1.2" -insta = { version = "1.41.1", features = ["json"] } +insta = { version = "1.41.1", features = ["glob", "json", "redactions"] } mime = "0.3.17" serde_json = "1.0" tower = { version = "0.5.1", features = ['util'] } diff --git a/src/model/snapshot_inputs/create_draft.json b/src/model/snapshot_inputs/create_draft.json new file mode 100644 index 0000000..83ec800 --- /dev/null +++ b/src/model/snapshot_inputs/create_draft.json @@ -0,0 +1,5 @@ +{ + "operationName": "CreateDraftMutation", + "variables": {}, + "query": "mutation CreateDraftMutation { createDraft(title: \"Draft title\", body: \"Draft body text\") { id title } }" +} diff --git a/src/model/snapshot_inputs/hello.json b/src/model/snapshot_inputs/hello.json new file mode 100644 index 0000000..14bc693 --- /dev/null +++ b/src/model/snapshot_inputs/hello.json @@ -0,0 +1,5 @@ +{ + "operationName": "HelloQuery", + "variables": {}, + "query": "query HelloQuery { hello }" +} diff --git a/src/model/snapshots/axum_graphql__model__tests__snapshot_graqphql_query_async@create_draft.json.snap b/src/model/snapshots/axum_graphql__model__tests__snapshot_graqphql_query_async@create_draft.json.snap new file mode 100644 index 0000000..28028d9 --- /dev/null +++ b/src/model/snapshots/axum_graphql__model__tests__snapshot_graqphql_query_async@create_draft.json.snap @@ -0,0 +1,17 @@ +--- +source: src/model/tests.rs +expression: body_json +input_file: src/model/snapshot_inputs/create_draft.json +snapshot_kind: text +--- +{ + "data": { + "createDraft": { + "id": 1, + "title": "Draft title" + } + }, + "extensions": { + "traceId": "[traceId]" + } +} diff --git a/src/model/snapshots/axum_graphql__model__tests__snapshot_graqphql_query_async@hello.json.snap b/src/model/snapshots/axum_graphql__model__tests__snapshot_graqphql_query_async@hello.json.snap new file mode 100644 index 0000000..78d5fdf --- /dev/null +++ b/src/model/snapshots/axum_graphql__model__tests__snapshot_graqphql_query_async@hello.json.snap @@ -0,0 +1,14 @@ +--- +source: src/model/tests.rs +expression: body_json +input_file: src/model/snapshot_inputs/hello.json +snapshot_kind: text +--- +{ + "data": { + "hello": "Hello everybody!" + }, + "extensions": { + "traceId": "[traceId]" + } +} diff --git a/src/model/snapshots/axum_graphql__model__tests__snapshot_hello_query.snap b/src/model/snapshots/axum_graphql__model__tests__snapshot_hello_query.snap deleted file mode 100644 index c1e6799..0000000 --- a/src/model/snapshots/axum_graphql__model__tests__snapshot_hello_query.snap +++ /dev/null @@ -1,6 +0,0 @@ ---- -source: src/model/tests.rs -expression: body -snapshot_kind: text ---- -b"{\"data\":{\"hello\":\"Hello everybody!\"},\"extensions\":{\"traceId\":\"00000000000000000000000000000000\"}}" diff --git a/src/model/tests.rs b/src/model/tests.rs index 5f8044e..d9553f3 100644 --- a/src/model/tests.rs +++ b/src/model/tests.rs @@ -1,7 +1,10 @@ +use std::{path::Path, str}; + use axum::{ body::Body, http::{header, Method, Request, StatusCode}, }; +use futures::executor::block_on; use http_body_util::BodyExt; use serde_json::{json, Value}; use tower::{Service, ServiceExt}; @@ -102,14 +105,17 @@ mod helpers { } } -#[tokio::test] -async fn snapshot_hello_query() { +async fn snapshot_graqphql_query_async>(path: P) { // arrange let app = helpers::get_app().await; - let json_request_body: Value = json!({ - "operationName":"HelloQuery", - "variables":{}, - "query":"query HelloQuery { hello }" + let json_request_body: Value = serde_json::from_slice( + &std::fs::read(&path).expect("file should exist and have read permissions set"), + ) + .unwrap_or_else(|_| { + panic!( + "File `{}`, should contain valid JSON", + path.as_ref().display() + ) }); // act @@ -126,8 +132,21 @@ async fn snapshot_hello_query() { .unwrap(); // assert - let body = response.into_body().collect().await.unwrap().to_bytes(); - insta::assert_debug_snapshot!(body); + let body_bytes = response.into_body().collect().await.unwrap().to_bytes(); + let body = str::from_utf8(&body_bytes).unwrap(); + let body_json: Value = serde_json::from_str(body).unwrap(); + insta::assert_json_snapshot!(body_json, {".extensions.traceId" => "[traceId]"}); + assert_eq!( + body_json["extensions"]["traceId"], + "00000000000000000000000000000000" + ); +} + +#[tokio::test] +async fn snapshot_graphql_queries() { + insta::glob!("snapshot_inputs/*.json", |path| { + block_on(snapshot_graqphql_query_async(path)); + }); } #[tokio::test]