public static void SetupGraphqlRequest <TResult>(this FakeHttpClient @this, Func <GraphQlQuery, TResult> intercepter)
 => @this.Intercept(HttpMethod.Post, null, (request) =>
 {
     var json         = request.Content.ReadAsStringAsync().Result;
     var query        = Newtonsoft.Json.JsonConvert.DeserializeObject <GraphQlQuery>(json);
     var result       = intercepter(query);
     var responseJson = Newtonsoft.Json.JsonConvert.SerializeObject(result);
     var response     = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
     response.Content = new StringContent(responseJson, Encoding.UTF8, "application/json");
     return(response);
 });
Exemple #2
0
        public async Task <GraphQlQuery> ExecuteClient(string clientName, string code)
        {
            var generatedCode = await Generate();

            code = code.Trim();

            var finalCode = $@"
using static {clientName};

public class _testClass{{
    public static async System.Threading.Tasks.Task Execute({clientName} client){{
        await client.{code};
    }}
}}
";

            this.httpClient = FakeHttpClient.Create();

            GraphQlQuery query = null;

            this.httpClient.SetupGraphqlRequest((q) =>
            {
                query = q;
                return(this.httpIntercepter?.Invoke(q) ?? new { });
            });

            var assembly = new Compiler().Compile(generatedCode, finalCode);

            var clientType    = assembly.GetType(clientName);
            var client        = Activator.CreateInstance(clientType, this.httpClient);
            var executeMethod = assembly.GetType("_testClass").GetMethod("Execute");

            await(executeMethod.Invoke(null, new[] { client }) as Task);

            this.httpClient.VerifyAll();

            return(query);
        }