11using System . Text . Json ;
22using System . Text . Json . Serialization ;
3+ using System . Threading . Tasks ;
4+ using Flurl ;
35using Flurl . Http ;
46using Flurl . Http . Configuration ;
57using Flurl . Http . Newtonsoft ;
@@ -13,7 +15,7 @@ namespace FlurlGraphQL.Tests
1315 public class FlurlGraphQLConfigTests : BaseFlurlGraphQLTest
1416 {
1517 [ TestMethod ]
16- public void TestGlobalConfig ( )
18+ public void TestGlobalGraphQLConfig ( )
1719 {
1820 var defaultConfig = FlurlGraphQLConfig . DefaultConfig ;
1921 Assert . AreEqual ( FlurlGraphQLConfig . DefaultPersistedQueryFieldName , defaultConfig . PersistedQueryPayloadFieldName ) ;
@@ -34,9 +36,9 @@ public void TestGlobalConfig()
3436 }
3537
3638 [ TestMethod ]
37- public void TestSystemTextJsonSerializerConfig ( )
39+ public void TestSystemTextJsonSerializerFlurlRequestLevelConfig ( )
3840 {
39- var graphqlRequest = "No Op Query " . WithSettings ( s =>
41+ var graphqlRequest = "http://www.no-op-url.com/ " . WithSettings ( s =>
4042 {
4143 s . JsonSerializer = new DefaultJsonSerializer ( new JsonSerializerOptions ( )
4244 {
@@ -50,21 +52,64 @@ public void TestSystemTextJsonSerializerConfig()
5052 var graphqlJsonSerializer = graphqlRequest . GraphQLJsonSerializer as FlurlGraphQLSystemTextJsonSerializer ;
5153
5254 Assert . IsNotNull ( graphqlJsonSerializer ) ;
55+ Assert . IsInstanceOfType ( graphqlJsonSerializer , typeof ( FlurlGraphQLSystemTextJsonSerializer ) ) ;
5356 Assert . AreEqual ( 99 , graphqlJsonSerializer . JsonSerializerOptions . MaxDepth ) ;
5457 Assert . AreEqual ( true , graphqlJsonSerializer . JsonSerializerOptions . WriteIndented ) ;
5558 Assert . AreEqual ( JsonIgnoreCondition . WhenWritingNull , graphqlJsonSerializer . JsonSerializerOptions . DefaultIgnoreCondition ) ;
5659 }
5760
61+ //TODO: Find out why Global Configuration isn't working and Fix Unit Test...
62+ [ Ignore ]
63+ [ TestMethod ]
64+ public void TestSystemTextJsonSerializerFlurlGlobalConfig ( )
65+ {
66+ FlurlHttp . Clients . WithDefaults ( builder =>
67+ builder . WithSettings ( s =>
68+ {
69+ s . JsonSerializer = new DefaultJsonSerializer ( new JsonSerializerOptions ( )
70+ {
71+ MaxDepth = 99 ,
72+ WriteIndented = true ,
73+ DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
74+ } ) ;
75+ } )
76+ ) ;
77+
78+ var graphqlRequest = "http://www.no-op-url.com/" . AppendPathSegment ( "graphql" ) . WithGraphQLQuery ( "No Op Query" ) ;
79+
80+ var graphqlJsonSerializer = graphqlRequest . GraphQLJsonSerializer as FlurlGraphQLSystemTextJsonSerializer ;
81+ Assert . IsNotNull ( graphqlJsonSerializer ) ;
82+ Assert . IsInstanceOfType ( graphqlJsonSerializer , typeof ( FlurlGraphQLSystemTextJsonSerializer ) ) ;
83+ Assert . AreEqual ( 99 , graphqlJsonSerializer . JsonSerializerOptions . MaxDepth ) ;
84+ Assert . AreEqual ( true , graphqlJsonSerializer . JsonSerializerOptions . WriteIndented ) ;
85+ Assert . AreEqual ( JsonIgnoreCondition . WhenWritingNull , graphqlJsonSerializer . JsonSerializerOptions . DefaultIgnoreCondition ) ;
86+ }
87+
88+ [ TestMethod ]
89+ public void TestSystemTextJsonSerializerGraphQLSpecificRequestLevelConfig ( )
90+ {
91+ var graphqlRequest = "http://www.no-op-url.com/" . WithSettings ( s =>
92+ {
93+ //Initialize core Flurl as Newtonsoft (opposite of what we are trying to test)...
94+ s . JsonSerializer = new NewtonsoftJsonSerializer ( new JsonSerializerSettings ( ) ) ;
95+ } )
96+ . ToGraphQLRequest ( )
97+ //THEN Override GraphQL with System.Text.Json...
98+ . UseGraphQLSystemTextJson ( ) ;
99+
100+ var graphqlJsonSerializer = graphqlRequest . GraphQLJsonSerializer as FlurlGraphQLSystemTextJsonSerializer ;
101+ Assert . IsInstanceOfType ( graphqlJsonSerializer , typeof ( FlurlGraphQLSystemTextJsonSerializer ) ) ;
102+ }
58103
59104 [ TestMethod ]
60- public void TestNewtonsoftJsonSerializerConfig ( )
105+ public void TestNewtonsoftJsonSerializerFlurlRequestLevelConfig ( )
61106 {
62- var graphqlRequest = "No Op Query " . WithSettings ( s =>
107+ var graphqlRequest = "http://www.no-op-url.com/ " . WithSettings ( s =>
63108 {
64109 s . JsonSerializer = new NewtonsoftJsonSerializer ( new JsonSerializerSettings ( )
65110 {
66111 MaxDepth = 99 ,
67- NullValueHandling = NullValueHandling . Include ,
112+ NullValueHandling = Newtonsoft . Json . NullValueHandling . Include ,
68113 TypeNameHandling = TypeNameHandling . None
69114 } ) ;
70115 } )
@@ -73,9 +118,48 @@ public void TestNewtonsoftJsonSerializerConfig()
73118 var graphqlJsonSerializer = graphqlRequest . GraphQLJsonSerializer as FlurlGraphQLNewtonsoftJsonSerializer ;
74119
75120 Assert . IsNotNull ( graphqlJsonSerializer ) ;
121+ Assert . IsInstanceOfType ( graphqlJsonSerializer , typeof ( FlurlGraphQLNewtonsoftJsonSerializer ) ) ;
76122 Assert . AreEqual ( 99 , graphqlJsonSerializer . JsonSerializerSettings . MaxDepth ) ;
77- Assert . AreEqual ( NullValueHandling . Include , graphqlJsonSerializer . JsonSerializerSettings . NullValueHandling ) ;
123+ Assert . AreEqual ( Newtonsoft . Json . NullValueHandling . Include , graphqlJsonSerializer . JsonSerializerSettings . NullValueHandling ) ;
78124 Assert . AreEqual ( TypeNameHandling . None , graphqlJsonSerializer . JsonSerializerSettings . TypeNameHandling ) ;
79125 }
126+
127+ //TODO: Find out why Global Configuration isn't working and Fix Unit Test...
128+ [ Ignore ]
129+ [ TestMethod ]
130+ public void TestNewtonsoftJsonSerializerFlurlGlobalConfig ( )
131+ {
132+ FlurlHttp . Clients . UseNewtonsoft ( new JsonSerializerSettings ( )
133+ {
134+ MaxDepth = 99 ,
135+ NullValueHandling = Newtonsoft . Json . NullValueHandling . Include ,
136+ TypeNameHandling = TypeNameHandling . None
137+ } ) ;
138+
139+ var graphqlRequest = "http://www.no-op-url.com/" . AppendPathSegment ( "graphql" ) . WithGraphQLQuery ( "No Op Query" ) ;
140+
141+ var graphqlJsonSerializer = graphqlRequest . GraphQLJsonSerializer as FlurlGraphQLNewtonsoftJsonSerializer ;
142+ Assert . IsNotNull ( graphqlJsonSerializer ) ;
143+ Assert . IsInstanceOfType ( graphqlJsonSerializer , typeof ( FlurlGraphQLNewtonsoftJsonSerializer ) ) ;
144+ Assert . AreEqual ( 99 , graphqlJsonSerializer . JsonSerializerSettings . MaxDepth ) ;
145+ Assert . AreEqual ( Newtonsoft . Json . NullValueHandling . Include , graphqlJsonSerializer . JsonSerializerSettings . NullValueHandling ) ;
146+ Assert . AreEqual ( TypeNameHandling . None , graphqlJsonSerializer . JsonSerializerSettings . TypeNameHandling ) ;
147+ }
148+
149+ [ TestMethod ]
150+ public void TestNewtonsoftJsonSerializerGraphQLSpecificRequestLevelConfig ( )
151+ {
152+ var graphqlRequest = "http://www.no-op-url.com/" . WithSettings ( s =>
153+ {
154+ //Initialize core Flurl as Newtonsoft (opposite of what we are trying to test)...
155+ s . JsonSerializer = new DefaultJsonSerializer ( new JsonSerializerOptions ( ) ) ;
156+ } )
157+ . ToGraphQLRequest ( )
158+ //THEN Override GraphQL with Newtonsoft.Json...
159+ . UseGraphQLNewtonsoftJson ( ) ;
160+
161+ var graphqlJsonSerializer = graphqlRequest . GraphQLJsonSerializer as FlurlGraphQLNewtonsoftJsonSerializer ;
162+ Assert . IsInstanceOfType ( graphqlJsonSerializer , typeof ( FlurlGraphQLNewtonsoftJsonSerializer ) ) ;
163+ }
80164 }
81165}
0 commit comments