|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +using RichardSzalay.MockHttp; |
| 6 | + |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Refit.Tests; |
| 10 | + |
| 11 | +public interface IGeneralRequests |
| 12 | +{ |
| 13 | + [Post("/foo")] |
| 14 | + Task Empty(); |
| 15 | + |
| 16 | + [Post("/foo")] |
| 17 | + Task SingleParameter(string id); |
| 18 | + |
| 19 | + [Post("/foo")] |
| 20 | + Task MultiParameter(string id, string name); |
| 21 | + |
| 22 | + [Post("/foo")] |
| 23 | + Task SingleGenericMultiParameter<TValue>(string id, string name, TValue generic); |
| 24 | +} |
| 25 | + |
| 26 | +public interface IDuplicateNames |
| 27 | +{ |
| 28 | + [Post("/foo")] |
| 29 | + Task SingleParameter(string id); |
| 30 | + |
| 31 | + [Post("/foo")] |
| 32 | + Task SingleParameter(int id); |
| 33 | +} |
| 34 | + |
| 35 | +public class CachedRequestBuilderTests |
| 36 | +{ |
| 37 | + [Fact] |
| 38 | + public async Task CacheHasCorrectNumberOfElementsTest() |
| 39 | + { |
| 40 | + var mockHttp = new MockHttpMessageHandler(); |
| 41 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 42 | + |
| 43 | + var fixture = RestService.For<IGeneralRequests>("http://bar", settings); |
| 44 | + |
| 45 | + // get internal dictionary to check count |
| 46 | + var requestBuilderField = fixture.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Single(x => x.Name == "requestBuilder"); |
| 47 | + var requestBuilder = requestBuilderField.GetValue(fixture) as CachedRequestBuilderImplementation; |
| 48 | + |
| 49 | + mockHttp |
| 50 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 51 | + .Respond(HttpStatusCode.OK); |
| 52 | + await fixture.Empty(); |
| 53 | + Assert.Single(requestBuilder.MethodDictionary); |
| 54 | + |
| 55 | + mockHttp |
| 56 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 57 | + .WithQueryString("id", "id") |
| 58 | + .Respond(HttpStatusCode.OK); |
| 59 | + await fixture.SingleParameter("id"); |
| 60 | + Assert.Equal(2, requestBuilder.MethodDictionary.Count); |
| 61 | + |
| 62 | + mockHttp |
| 63 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 64 | + .WithQueryString("id", "id") |
| 65 | + .WithQueryString("name", "name") |
| 66 | + .Respond(HttpStatusCode.OK); |
| 67 | + await fixture.MultiParameter("id", "name"); |
| 68 | + Assert.Equal(3, requestBuilder.MethodDictionary.Count); |
| 69 | + |
| 70 | + mockHttp |
| 71 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 72 | + .WithQueryString("id", "id") |
| 73 | + .WithQueryString("name", "name") |
| 74 | + .WithQueryString("generic", "generic") |
| 75 | + .Respond(HttpStatusCode.OK); |
| 76 | + await fixture.SingleGenericMultiParameter("id", "name", "generic"); |
| 77 | + Assert.Equal(4, requestBuilder.MethodDictionary.Count); |
| 78 | + |
| 79 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public async Task NoDuplicateEntriesTest() |
| 84 | + { |
| 85 | + var mockHttp = new MockHttpMessageHandler(); |
| 86 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 87 | + |
| 88 | + var fixture = RestService.For<IGeneralRequests>("http://bar", settings); |
| 89 | + |
| 90 | + // get internal dictionary to check count |
| 91 | + var requestBuilderField = fixture.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Single(x => x.Name == "requestBuilder"); |
| 92 | + var requestBuilder = requestBuilderField.GetValue(fixture) as CachedRequestBuilderImplementation; |
| 93 | + |
| 94 | + // send the same request repeatedly to ensure that multiple dictionary entries are not created |
| 95 | + mockHttp |
| 96 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 97 | + .WithQueryString("id", "id") |
| 98 | + .Respond(HttpStatusCode.OK); |
| 99 | + await fixture.SingleParameter("id"); |
| 100 | + Assert.Single(requestBuilder.MethodDictionary); |
| 101 | + |
| 102 | + mockHttp |
| 103 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 104 | + .WithQueryString("id", "id") |
| 105 | + .Respond(HttpStatusCode.OK); |
| 106 | + await fixture.SingleParameter("id"); |
| 107 | + Assert.Single(requestBuilder.MethodDictionary); |
| 108 | + |
| 109 | + mockHttp |
| 110 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 111 | + .WithQueryString("id", "id") |
| 112 | + .Respond(HttpStatusCode.OK); |
| 113 | + await fixture.SingleParameter("id"); |
| 114 | + Assert.Single(requestBuilder.MethodDictionary); |
| 115 | + |
| 116 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public async Task SameNameDuplicateEntriesTest() |
| 121 | + { |
| 122 | + var mockHttp = new MockHttpMessageHandler(); |
| 123 | + var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp }; |
| 124 | + |
| 125 | + var fixture = RestService.For<IDuplicateNames>("http://bar", settings); |
| 126 | + |
| 127 | + // get internal dictionary to check count |
| 128 | + var requestBuilderField = fixture.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Single(x => x.Name == "requestBuilder"); |
| 129 | + var requestBuilder = requestBuilderField.GetValue(fixture) as CachedRequestBuilderImplementation; |
| 130 | + |
| 131 | + // send the two different requests with the same name |
| 132 | + mockHttp |
| 133 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 134 | + .WithQueryString("id", "id") |
| 135 | + .Respond(HttpStatusCode.OK); |
| 136 | + await fixture.SingleParameter("id"); |
| 137 | + Assert.Single(requestBuilder.MethodDictionary); |
| 138 | + |
| 139 | + mockHttp |
| 140 | + .Expect(HttpMethod.Post, "http://bar/foo") |
| 141 | + .WithQueryString("id", "10") |
| 142 | + .Respond(HttpStatusCode.OK); |
| 143 | + await fixture.SingleParameter(10); |
| 144 | + Assert.Equal(2, requestBuilder.MethodDictionary.Count); |
| 145 | + |
| 146 | + mockHttp.VerifyNoOutstandingExpectation(); |
| 147 | + } |
| 148 | +} |
0 commit comments