33
44#include < gtest/gtest.h>
55
6+ #include " graphqlservice/GraphQLParse.h"
7+
68#include " graphqlservice/internal/Grammar.h"
79
810#include < tao/pegtl/contrib/analyze.hpp>
@@ -169,6 +171,62 @@ TEST(PegtlExecutableCase, InvalidStringEscapeSequence)
169171 caughtException = true ;
170172 }
171173
174+ EXPECT_TRUE (caughtException) << " should catch a parse exception" ;
175+ EXPECT_FALSE (parsedQuery) << " should not successfully parse the query" ;
176+ }
177+
178+ using namespace std ::literals;
179+
180+ constexpr auto queryWithDepth3 = R"gql( query {
181+ foo {
182+ bar
183+ }
184+ })gql" sv;
185+
186+ TEST (PegtlExecutableCase, ParserDepthLimitNotExceeded)
187+ {
188+ bool parsedQuery = false ;
189+
190+ try
191+ {
192+ auto query = peg::parseString (queryWithDepth3, 3 );
193+
194+ parsedQuery = query.root != nullptr ;
195+ }
196+ catch (const peg::parse_error& ex)
197+ {
198+ FAIL () << ex.what ();
199+ }
200+
201+ EXPECT_TRUE (parsedQuery) << " should parse the query" ;
202+ }
203+
204+ TEST (PegtlExecutableCase, ParserDepthLimitExceeded)
205+ {
206+ bool parsedQuery = false ;
207+ bool caughtException = false ;
208+
209+ try
210+ {
211+ auto query = peg::parseString (queryWithDepth3, 2 );
212+
213+ parsedQuery = query.root != nullptr ;
214+ }
215+ catch (const peg::parse_error& ex)
216+ {
217+ ASSERT_NE (nullptr , ex.what ());
218+
219+ using namespace std ::literals;
220+
221+ const std::string_view error { ex.what () };
222+ constexpr auto expected =
223+ " GraphQL:4:3: Exceeded 2 nested depth limit for https://spec.graphql.org/October2021/#SelectionSet" sv;
224+
225+ EXPECT_TRUE (error == expected) << ex.what ();
226+
227+ caughtException = true ;
228+ }
229+
172230 EXPECT_TRUE (caughtException) << " should catch a parse exception" ;
173231 EXPECT_FALSE (parsedQuery) << " should not successfully parse the query" ;
174232}
0 commit comments