Esempio n. 1
0
        public DummyHttpEndpointTest()
        {
            _ctrl      = new DummyController();
            _serviceV1 = new DummyCommandableHttpService();
            _serviceV2 = new DummyCommandableHttpService();

            _httpEndpoint = new HttpEndpoint();

            var references = References.FromTuples(
                new Descriptor("pip-services3-dummies", "controller", "default", "default", "1.0"), _ctrl,
                new Descriptor("pip-services3", "endpoint", "http", "default", "1.0"), _httpEndpoint
                );

            _serviceV1.Configure(ConfigParams.FromTuples(
                                     "base_route", "/api/v1/dummy"
                                     ));

            _serviceV2.Configure(ConfigParams.FromTuples(
                                     "base_route", "/api/v2/dummy"
                                     ));

            _httpEndpoint.Configure(RestConfig);

            _serviceV1.SetReferences(references);
            _serviceV2.SetReferences(references);

            _httpEndpoint.OpenAsync(null).Wait();
        }
Esempio n. 2
0
        /// <summary>
        /// Opens the component.
        /// </summary>
        /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param>
        /// <returns></returns>
        public async virtual Task OpenAsync(string correlationId)
        {
            if (IsOpen())
            {
                return;
            }

            if (_endpoint == null)
            {
                _endpoint = CreateLocalEndpoint();
                _endpoint.Register(this);
                _localEndpoint = true;
            }

            if (_localEndpoint)
            {
                await _endpoint.OpenAsync(correlationId).ContinueWith(task =>
                {
                    _opened = task.Exception == null;
                });
            }
            else
            {
                _opened = true;
            }
        }
Esempio n. 3
0
        public async Task It_Should_Return_OpenApi_FileAsync()
        {
            // create temp file
            var openApiContent = "swagger yaml content from file";
            var fileName       = Path.GetTempFileName();

            File.WriteAllText(fileName, openApiContent);

            // recreate service with new configuration
            await _httpEndpoint.CloseAsync(null);

            await _service.CloseAsync(null);

            _httpEndpoint = new HttpEndpoint();
            _service      = CreateService(_httpEndpoint, ConfigParams.FromTuples(
                                              "base_route", "/api/v1",
                                              "openapi_file", fileName, // for test only
                                              "swagger.enable", "true"
                                              ));

            _httpEndpoint.Configure(RestConfig);
            await _httpEndpoint.OpenAsync(null);

            var result = await SendRequestAsync("get", "/api/v1/swagger", new { });

            Assert.Equal(openApiContent, result);

            File.Delete(fileName);
        }
Esempio n. 4
0
 /// <summary>
 /// Unsets (clears) previously set references to dependent components.
 /// </summary>
 public virtual void UnsetReferences()
 {
     // Remove registration callback from endpoint
     if (_endpoint != null)
     {
         _endpoint.Unregister(this);
         _endpoint = null;
     }
 }
Esempio n. 5
0
        public DummyRestServiceTest()
        {
            _httpClient   = new HttpClient();
            _httpEndpoint = new HttpEndpoint();
            _service      = CreateService(_httpEndpoint, ServiceConfig);

            _httpEndpoint.Configure(RestConfig);
            _httpEndpoint.OpenAsync(null).Wait();
        }
Esempio n. 6
0
        private DummyRestService CreateService(HttpEndpoint httpEndpoint, ConfigParams config)
        {
            var service = new DummyRestService();

            var references = References.FromTuples(
                new Descriptor("pip-services3-dummies", "controller", "default", "default", "1.0"), new DummyController(),
                new Descriptor("pip-services3", "endpoint", "http", "default", "1.0"), httpEndpoint
                );

            service.Configure(config);
            service.SetReferences(references);
            return(service);
        }
Esempio n. 7
0
        private HttpEndpoint CreateLocalEndpoint()
        {
            var endpoint = new HttpEndpoint();

            if (_config != null)
            {
                endpoint.Configure(_config);
            }

            if (_references != null)
            {
                endpoint.SetReferences(_references);
            }

            return(endpoint);
        }
Esempio n. 8
0
        /// <summary>
        /// Sets references to dependent components.
        /// </summary>
        /// <param name="references">references to locate the component dependencies.</param>
        public virtual void SetReferences(IReferences references)
        {
            _references = references;

            _logger.SetReferences(references);
            _counters.SetReferences(references);
            _dependencyResolver.SetReferences(references);

            // Get endpoint
            _endpoint      = _dependencyResolver.GetOneOptional("endpoint") as HttpEndpoint;
            _localEndpoint = _endpoint == null;

            // Or create a local one
            if (_endpoint == null)
            {
                _endpoint = CreateLocalEndpoint();
            }

            // Add registration callback to the endpoint
            _endpoint.Register(this);
        }
Esempio n. 9
0
        public async Task It_Should_Return_OpenApi_ResourceAsync()
        {
            var openApiContent = "swagger yaml content from resource";

            // recreate service with new configuration
            await _httpEndpoint.CloseAsync(null);

            await _service.CloseAsync(null);

            _httpEndpoint = new HttpEndpoint();
            _service      = CreateService(_httpEndpoint, ConfigParams.FromTuples(
                                              "base_route", "/api/v1",
                                              "openapi_resource", "DummyRestServiceSwagger.yaml", // for test only
                                              "swagger.enable", "true"
                                              ));

            _httpEndpoint.Configure(RestConfig);
            await _httpEndpoint.OpenAsync(null);

            var result = await SendRequestAsync("get", "/api/v1/swagger", new { });

            Assert.Equal(openApiContent, result);
        }