|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Tocsoft.GraphQLCodeGen.Tests |
| 11 | +{ |
| 12 | + public class GraphQlQuery |
| 13 | + { |
| 14 | + public string Query { get; set; } = ""; |
| 15 | + public Dictionary<string, JToken> Variables { get; set; } = new Dictionary<string, JToken>(); |
| 16 | + } |
| 17 | + |
| 18 | + public static class FakeHttpClientGraphQlExtensions |
| 19 | + { |
| 20 | + public static void SetupGraphqlRequest<TResult>(this FakeHttpClient @this, Func<GraphQlQuery, TResult> intercepter) |
| 21 | + => @this.Intercept(HttpMethod.Post, null, (request) => |
| 22 | + { |
| 23 | + var json = request.Content.ReadAsStringAsync().Result; |
| 24 | + var query = Newtonsoft.Json.JsonConvert.DeserializeObject<GraphQlQuery>(json); |
| 25 | + var result = intercepter(query); |
| 26 | + var responseJson = Newtonsoft.Json.JsonConvert.SerializeObject(result); |
| 27 | + var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); |
| 28 | + response.Content = new StringContent(responseJson, Encoding.UTF8, "application/json"); |
| 29 | + return response; |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + public class FakeHttpClient : System.Net.Http.HttpClient |
| 34 | + { |
| 35 | + private readonly FakeHttpMessageHandler handler; |
| 36 | + |
| 37 | + public static FakeHttpClient Create() |
| 38 | + { |
| 39 | + var handler = new FakeHttpMessageHandler(); |
| 40 | + return new FakeHttpClient(handler); |
| 41 | + } |
| 42 | + |
| 43 | + private FakeHttpClient(FakeHttpMessageHandler handler) |
| 44 | + : base(handler) |
| 45 | + { |
| 46 | + this.handler = handler; |
| 47 | + this.BaseAddress = new Uri("http://localhost.test/endpoint"); |
| 48 | + } |
| 49 | + |
| 50 | + public void Post(System.Net.Http.HttpMethod method, Uri uri, Func<System.Net.Http.HttpRequestMessage, System.Net.Http.HttpResponseMessage> intercepter) |
| 51 | + => this.Intercept(HttpMethod.Post, uri, intercepter); |
| 52 | + |
| 53 | + public void Intercept(System.Net.Http.HttpMethod method, Uri uri, Func<System.Net.Http.HttpRequestMessage, System.Net.Http.HttpResponseMessage> intercepter) |
| 54 | + { |
| 55 | + if (uri == null) |
| 56 | + { |
| 57 | + uri = this.BaseAddress; |
| 58 | + } |
| 59 | + |
| 60 | + if (!uri.IsAbsoluteUri) |
| 61 | + { |
| 62 | + uri = new Uri(this.BaseAddress, uri); |
| 63 | + } |
| 64 | + |
| 65 | + this.handler.Intercept(method, uri, intercepter); |
| 66 | + } |
| 67 | + internal void Verify(System.Net.Http.HttpMethod method, Uri uri) |
| 68 | + => this.handler.Verify(method, uri); |
| 69 | + |
| 70 | + internal void VerifyAll() |
| 71 | + => this.handler.VerifyAll(); |
| 72 | + |
| 73 | + private class FakeHttpMessageHandler : System.Net.Http.HttpMessageHandler |
| 74 | + { |
| 75 | + private Dictionary<(System.Net.Http.HttpMethod method, Uri uri), Func<System.Net.Http.HttpRequestMessage, System.Net.Http.HttpResponseMessage>> intercepters |
| 76 | + = new Dictionary<(System.Net.Http.HttpMethod method, Uri uri), Func<System.Net.Http.HttpRequestMessage, System.Net.Http.HttpResponseMessage>>(); |
| 77 | + |
| 78 | + private List<(System.Net.Http.HttpMethod method, Uri uri)> calledIntercepters = new List<(HttpMethod method, Uri uri)>(); |
| 79 | + |
| 80 | + internal FakeHttpMessageHandler() |
| 81 | + { |
| 82 | + } |
| 83 | + |
| 84 | + internal void Intercept(System.Net.Http.HttpMethod method, Uri uri, Func<System.Net.Http.HttpRequestMessage, System.Net.Http.HttpResponseMessage> intercepter) |
| 85 | + { |
| 86 | + this.intercepters[(method, uri)] = intercepter; |
| 87 | + } |
| 88 | + |
| 89 | + internal void Verify(System.Net.Http.HttpMethod method, Uri uri) |
| 90 | + { |
| 91 | + Assert.Contains((method, uri), calledIntercepters); |
| 92 | + } |
| 93 | + internal void VerifyAll() |
| 94 | + { |
| 95 | + Assert.All(intercepters.Keys, a => |
| 96 | + { |
| 97 | + Verify(a.method, a.uri); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 102 | + { |
| 103 | + if (this.intercepters.TryGetValue((request.Method, request.RequestUri), out var func)) |
| 104 | + { |
| 105 | + calledIntercepters.Add((request.Method, request.RequestUri)); |
| 106 | + var resp = func(request); |
| 107 | + return Task.FromResult(resp); |
| 108 | + } |
| 109 | + |
| 110 | + return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.NotImplemented)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments