private MobileCore(IPlatformInjector injector, Options options)
        {
            Contract.Requires(options != null);
            Logger = options.Logger ?? injector?.CreateLogger() ?? new NullLogger();

            if (injector != null && options.ConfigFileName != null)
            {
                var filename = $"{injector.DefaultResources}.{options.ConfigFileName}";
                try
                {
                    using (var stream = injector.ExecutingAssembly.GetManifestResourceStream(filename))
                    {
                        servicesConfig = MobileCoreJsonParser.Parse(stream);
                    }
                }
                catch (System.Exception e)
                {
                    throw new InitializationException($"{filename} could not be loaded", e);
                }
            }
            else
            {
                if (options.ConfigJson != null)
                {
                    try
                    {
                        MobileCoreJsonParser.Parse(options.ConfigJson);
                    }
                    catch (System.Exception e)
                    {
                        throw new InitializationException("invalid JSON configuration file", e);
                    }
                }
                else
                {
                    throw new InitializationException("Must provide either filename or JSON configuration in Init() options");
                }
            }

            if (options.HttpServiceModule == null)
            {
                HttpClient httpClient = new HttpClient();
                httpClient.Timeout = TimeSpan.FromSeconds(DEFAULT_TIMEOUT);
                var httpServiceModule = new SystemNetHttpServiceModule(httpClient);
                var configuration     = GetServiceConfiguration(httpServiceModule.Type);
                if (configuration == null)
                {
                    configuration = ServiceConfiguration.Builder.Build();
                }
                httpServiceModule.Configure(this, configuration);
                HttpLayer = httpServiceModule;
            }
            else
            {
                HttpLayer = options.HttpServiceModule;
            }
        }
Exemplo n.º 2
0
        private MobileCore(IPlatformInjector injector, Options options)
        {
            Logger = options.Logger ?? injector?.CreateLogger() ?? new NullLogger();

            if (injector != null && options.ConfigFileName != null)
            {
                try
                {
                    using (var stream = injector.GetBundledFileStream(options.ConfigFileName))
                    {
                        mobileConfiguration = MobileCoreConfiguration.Parse(stream);
                    }
                }
                catch (System.Exception e)
                {
                    throw new InitializationException($"{options.ConfigFileName} could not be loaded", e);
                }
            }
            else
            {
                if (options.ConfigJson != null)
                {
                    try
                    {
                        mobileConfiguration = MobileCoreConfiguration.Parse(options.ConfigJson);
                    }
                    catch (System.Exception e)
                    {
                        throw new InitializationException("invalid JSON configuration file", e);
                    }
                }
                else
                {
                    throw new InitializationException("Must provide either filename or JSON configuration in Init() options");
                }
            }
            if (options.HttpServiceModule == null)
            {
                HttpClientHandler httpClientHandler = new HttpClientHandler();
                httpClientHandler.AllowAutoRedirect = options.HttpAllowAutoRedirect;

                HttpClient httpClient = new HttpClient(httpClientHandler);
                httpClient.Timeout = TimeSpan.FromSeconds(DEFAULT_TIMEOUT);
                var httpServiceModule = new SystemNetHttpServiceModule(httpClient);
                var configuration     = GetFirstServiceConfigurationByType(httpServiceModule.Type);
                if (configuration == null)
                {
                    configuration = ServiceConfiguration.Builder.Build();
                }
                httpServiceModule.Configure(this, configuration);
                HttpLayer = httpServiceModule;
            }
            else
            {
                HttpLayer = options.HttpServiceModule;
            }
        }
        public async Task TestGetRequestNotFound()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            IHttpRequest request = module.NewRequest();

            IHttpResponse response = await request.Get("http://does.not.exist.com").Execute();

            Assert.IsFalse(response.Successful);
            Assert.NotNull(response.Error);
        }
        public async Task TestGetRequestSuccessfulNetwork()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            IHttpRequest request = module.NewRequest();

            IHttpResponse response = await request.Get("http://www.mocky.io/v2/5a5f74172e00006e260a8476").Execute();

            Assert.NotNull(response);
            Assert.Null(response.Error);
            Assert.IsTrue(response.Successful);
            Assert.AreEqual("{\n" + " \"story\": {\n"
                            + "     \"title\": \"Test Title\"\n" + " }    \n" + "}", response.Body);
        }
        public async Task TestGetRequestSuccessfulLocal()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            IHttpRequest request = module.NewRequest();

            IHttpResponse response = await request.Get(localPath(GET_TEST_PATH)).Execute();

            Assert.NotNull(response);
            Assert.Null(response.Error);
            Assert.IsTrue(response.Successful);
            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual(GET_TEST_BODY, response.Body);
            var jsonObject = JsonObject.Parse(response.Body);

            Assert.AreEqual(HELLO_WORLD, (string)jsonObject["text"]);
        }
        public async Task TestGetRequestWithHeader()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            IHttpRequest request = module.NewRequest();

            IHttpResponse response = await request
                                     .AddHeader(TEST_HEADER, TEST_HEADER1_VALUE)
                                     .AddHeader(TEST_HEADER, TEST_HEADER2_VALUE).Get(localPath(GET_TEST_WITH_HEADER_PATH)).Execute();

            Assert.NotNull(response);
            Assert.Null(response.Error);
            Assert.IsTrue(response.Successful);
            Assert.AreEqual(200, response.StatusCode);
            var jsonObject = JsonObject.Parse(response.Body);

            Assert.AreEqual(HELLO_WORLD, (string)jsonObject["text"]);
            Assert.AreEqual($"{TEST_HEADER1_VALUE}, {TEST_HEADER2_VALUE}", (string)jsonObject["headerValues"], "header check");
        }
        public void TestType()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            Assert.AreEqual("http", module.Type);
        }