11using System ;
22using System . Linq . Expressions ;
33using System . Net . Http ;
4- using System . Text . RegularExpressions ;
54using System . Threading . Tasks ;
65using Dibix . Http . Client ;
76using Dibix . Testing . Data ;
87using Microsoft . Extensions . DependencyInjection ;
98using Newtonsoft . Json ;
10- using Newtonsoft . Json . Linq ;
11- using Newtonsoft . Json . Serialization ;
129
1310namespace Dibix . Testing . Http
1411{
@@ -55,13 +52,13 @@ protected async Task<TResponseContent> InvokeApi<TService, TResponseContent>(TSe
5552 protected async Task < TResponseContent > InvokeApiAndAssertResponse < TService , TResponseContent > ( TService service , Expression < Func < TService , Task < HttpResponse < TResponseContent > > > > methodSelector , string expectedText = null , string outputName = null , Action < JsonSerializerSettings > configureSerializer = null )
5653 {
5754 HttpResponse < TResponseContent > response = await InvokeApiCore ( service , methodSelector ) . ConfigureAwait ( false ) ;
58- Assert ( response , expectedText , outputName , configureSerializer ) ;
55+ AssertJsonResponse ( response . ResponseContent , configureSerializer , outputName , expectedText ) ;
5956 return response . ResponseContent ;
6057 }
6158 protected async Task < TResponseContent > InvokeApiAndAssertResponse < TService , TResponseContent > ( HttpTestContext context , Expression < Func < TService , Task < HttpResponse < TResponseContent > > > > methodSelector , string expectedText = null , string outputName = null , Action < JsonSerializerSettings > configureSerializer = null )
6259 {
6360 HttpResponse < TResponseContent > response = await CreateServiceAndInvokeApi ( context , methodSelector ) . ConfigureAwait ( false ) ;
64- Assert ( response , expectedText , outputName , configureSerializer ) ;
61+ AssertJsonResponse ( response . ResponseContent , configureSerializer , outputName , expectedText ) ;
6562 return response . ResponseContent ;
6663 }
6764
@@ -85,32 +82,6 @@ private static Task<TResponse> CreateServiceAndInvokeApi<TService, TResponse>(Ht
8582 return InvokeApiCore ( service , methodSelector ) ;
8683 }
8784
88- private void Assert < TResponseContent > ( HttpResponse < TResponseContent > response , string expectedText , string outputName , Action < JsonSerializerSettings > configureSerializer )
89- {
90- JsonSerializerSettings settings = new JsonSerializerSettings
91- {
92- ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy ( ) } ,
93- Formatting = Formatting . Indented ,
94- DateTimeZoneHandling = DateTimeZoneHandling . Unspecified
95- } ;
96- configureSerializer ? . Invoke ( settings ) ;
97-
98- const string extension = "json" ;
99- string outputNameResolved = outputName ?? TestContext . TestName ;
100- string expectedTextResolved = expectedText ?? GetEmbeddedResourceContent ( $ "{ outputNameResolved } .{ extension } ") ;
101- string actualText = JsonConvert . SerializeObject ( response . ResponseContent , settings ) ;
102- JToken actualTextDom = JToken . Parse ( actualText ) ;
103- string expectedTextReplaced = Regex . Replace ( expectedTextResolved , @"\{(?<path>[A-Za-z.]+)\}" , x =>
104- {
105- string path = x . Groups [ "path" ] . Value ;
106- if ( ! ( actualTextDom . SelectToken ( path ) is JValue value ) || value . Value == null )
107- throw new InvalidOperationException ( $ "Replace pattern did not match a JSON path in the actual document: { path } ({ x . Index } )") ;
108-
109- return value . Value . ToString ( ) ;
110- } ) ;
111- AssertEqual ( expectedTextReplaced , actualText , outputNameResolved , extension : extension ) ;
112- }
113-
11485 private static HttpTestContext CreateTestContext ( IHttpClientFactory httpClientFactory , HttpClientOptions httpClientOptions , IHttpAuthorizationProvider authorizationProvider )
11586 {
11687 return new HttpTestContext ( httpClientFactory , httpClientOptions , authorizationProvider ) ;
0 commit comments