예제 #1
0
        private static WebServiceClient CreateClientCore(string type, string ipAddress = "1.2.3.4",
                                                         HttpStatusCode status         = HttpStatusCode.OK, string?contentType = null, string content = "")
        {
            var service = type.Replace("Async", "");

            if (contentType == null)
            {
                contentType = $"application/vnd.maxmind.com-{service}+json";
            }

            var stringContent = new StringContent(content);

            stringContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            stringContent.Headers.Add("Content-Length", content.Length.ToString());
            var message = new HttpResponseMessage(status)
            {
                Content = stringContent
            };

            // HttpClient mock
            var uri      = new Uri($"https://geoip.maxmind.com/geoip/v2.1/{service}/{ipAddress}");
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Get, uri.ToString())
            .WithHeaders("Accept", "application/json")
            .Respond(message);

            var options = Options.Create(new WebServiceClientOptions
            {
                AccountId  = 6,
                LicenseKey = "0123456789",
                Host       = "geoip.maxmind.com",
                Timeout    = 3000,
                Locales    = new List <string> {
                    "en"
                }
            });

            // HttpWebRequest mock
            var contentsBytes  = Encoding.UTF8.GetBytes(content);
            var responseStream = new MemoryStream(contentsBytes);

            var syncWebRequest = new MockSyncClient(new Response(uri, status, contentType, responseStream));

            var webServiceClient = new WebServiceClient(
                options.Value.AccountId,
                options.Value.LicenseKey,
                options.Value.Locales,
                options.Value.Host,
                options.Value.Timeout,
                null,
                false,
                syncWebRequest,
                new HttpClient(mockHttp)
                );

            return(webServiceClient);
        }
예제 #2
0
        private static WebServiceClient CreateClient(string type, string ipAddress = "1.2.3.4",
                                                     HttpStatusCode status         = HttpStatusCode.OK, string?contentType = null, string content = "")
        {
            var service = type.Replace("Async", "");

            if (contentType == null)
            {
                contentType = $"application/vnd.maxmind.com-{service}+json";
            }

            var stringContent = new StringContent(content);

            stringContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            stringContent.Headers.Add("Content-Length", content.Length.ToString());
            var message = new HttpResponseMessage(status)
            {
                Content = stringContent
            };

            // HttpClient mock
            var uri      = new Uri($"https://geoip.maxmind.com/geoip/v2.1/{service}/{ipAddress}");
            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(HttpMethod.Get, uri.ToString())
            .WithHeaders("Accept", "application/json")
            .Respond(message);

#if !NETCOREAPP1_1
            // HttpWebRequest mock
            var contentsBytes  = Encoding.UTF8.GetBytes(content);
            var responseStream = new MemoryStream(contentsBytes);

            var syncWebRequest = new MockSyncClient(new Response(uri, status, contentType, responseStream));
#endif

            return(new WebServiceClient(6, "0123456789",
                                        locales: new List <string> {
                "en"
            },
                                        host: "geoip.maxmind.com",
                                        timeout: 3000,
                                        httpMessageHandler: mockHttp
#if !NETCOREAPP1_1
                                        , syncWebRequest: syncWebRequest
#endif
                                        ));
        }