33using System . IO ;
44using System . Net ;
55using System . Net . Http ;
6- using System . Reflection ;
7- using System . Runtime . CompilerServices ;
86using System . Text ;
9- using System . Text . RegularExpressions ;
107using System . Threading ;
118using System . Threading . Tasks ;
129using Moq ;
@@ -24,15 +21,6 @@ namespace Vonage.Test
2421 public class TestBase
2522 {
2623 private const string MockedMethod = "SendAsync" ;
27- private const string JsonRegexPattern = "(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )|\\ s+" ;
28-
29- protected string ApiUrl =>
30- this . configuration . VonageUrls . Nexmo . AbsoluteUri . Substring ( 0 ,
31- this . configuration . VonageUrls . Nexmo . AbsoluteUri . Length - 1 ) ;
32-
33- protected string RestUrl =>
34- this . configuration . VonageUrls . Rest . AbsoluteUri . Substring ( 0 ,
35- this . configuration . VonageUrls . Rest . AbsoluteUri . Length - 1 ) ;
3624
3725 protected readonly string ApiKey = Environment . GetEnvironmentVariable ( "VONAGE_API_KEY" ) ?? "testkey" ;
3826 protected readonly string ApiSecret = Environment . GetEnvironmentVariable ( "VONAGE_API_Secret" ) ?? "testSecret" ;
@@ -43,16 +31,17 @@ public class TestBase
4331 protected readonly string PrivateKey = Environment . GetEnvironmentVariable ( "PRIVATE_KEY" ) ??
4432 TokenHelper . GetKey ( ) ;
4533
34+ protected Configuration configuration ;
35+
4636 protected TestBase ( ) => this . configuration = new Configuration ( ) ;
4737
48- #if NETCOREAPP2_0_OR_GREATER
49- private static readonly Assembly ThisAssembly = typeof ( TestBase ) . GetTypeInfo ( ) . Assembly ;
50- #else
51- private static readonly Assembly ThisAssembly = typeof ( TestBase ) . Assembly ;
52- #endif
38+ protected string ApiUrl =>
39+ this . configuration . VonageUrls . Nexmo . AbsoluteUri . Substring ( 0 ,
40+ this . configuration . VonageUrls . Nexmo . AbsoluteUri . Length - 1 ) ;
5341
54- private static readonly string TestAssemblyName = ThisAssembly . GetName ( ) . Name ;
55- protected Configuration configuration ;
42+ protected string RestUrl =>
43+ this . configuration . VonageUrls . Rest . AbsoluteUri . Substring ( 0 ,
44+ this . configuration . VonageUrls . Rest . AbsoluteUri . Length - 1 ) ;
5645
5746 protected VonageClient BuildVonageClient ( Credentials credentials ) =>
5847 new VonageClient ( credentials , this . configuration , new TimeProvider ( ) ) ;
@@ -66,14 +55,6 @@ protected Credentials BuildCredentialsForBasicAuthentication() =>
6655 protected Credentials BuildCredentialsForBearerAuthentication ( ) =>
6756 Credentials . FromAppIdAndPrivateKey ( this . AppId , this . PrivateKey ) ;
6857
69- private static string GetAssemblyDirectory ( )
70- {
71- var location = ThisAssembly . CodeBase ;
72- var uri = new UriBuilder ( location ) ;
73- var path = Uri . UnescapeDataString ( uri . Path ) ;
74- return Path . GetDirectoryName ( path ) ;
75- }
76-
7758 protected void Setup ( string uri , Maybe < string > responseContent , string requestContent = null ,
7859 HttpStatusCode expectedCode = HttpStatusCode . OK ) =>
7960 this . Setup ( uri ,
@@ -95,7 +76,7 @@ private void Setup(string uri, Maybe<HttpContent> httpContent, HttpStatusCode ex
9576 . Setup < Task < HttpResponseMessage > > ( MockedMethod ,
9677 ItExpr . IsAny < HttpRequestMessage > ( ) ,
9778 ItExpr . IsAny < CancellationToken > ( ) )
98- . Callback < HttpRequestMessage , CancellationToken > ( ( actualHttpRequestMessage , cancellationToken ) =>
79+ . Callback < HttpRequestMessage , CancellationToken > ( ( actualHttpRequestMessage , _ ) =>
9980 {
10081 Assert . Equal ( uri , actualHttpRequestMessage . RequestUri . AbsoluteUri ) ;
10182 if ( requestContent == null )
@@ -107,31 +88,5 @@ private void Setup(string uri, Maybe<HttpContent> httpContent, HttpStatusCode ex
10788 . Verifiable ( ) ;
10889 this . configuration . ClientHandler = mockHandler . Object ;
10990 }
110-
111- protected string GetResponseJson ( [ CallerMemberName ] string name = null ) => this . ReadJsonFile ( name , "response" ) ;
112-
113- protected string GetRequestJson ( [ CallerMemberName ] string name = null ) => this . ReadJsonFile ( name , "request" ) ;
114-
115- private string ReadJsonFile ( string name , string fileType )
116- {
117- var typeNamespace = this . GetType ( ) . Namespace ;
118- if ( typeNamespace is null )
119- {
120- return string . Empty ;
121- }
122-
123- var path = Path . Combine (
124- GetAssemblyDirectory ( ) ,
125- typeNamespace . Substring ( TestAssemblyName . Length ) ,
126- "Data" ,
127- this . GetType ( ) . Name ,
128- $ "{ name } -{ fileType } .json") ;
129- if ( ! File . Exists ( path ) )
130- {
131- throw new FileNotFoundException ( $ "File not found at { path } .") ;
132- }
133-
134- return Regex . Replace ( File . ReadAllText ( path ) , JsonRegexPattern , "$1" ) ;
135- }
13691 }
13792}
0 commit comments