Ejemplo 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();
        }
Ejemplo 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;
            }
        }
Ejemplo 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);
        }
Ejemplo 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;
     }
 }
Ejemplo n.º 5
0
        public DummyRestServiceTest()
        {
            _httpClient   = new HttpClient();
            _httpEndpoint = new HttpEndpoint();
            _service      = CreateService(_httpEndpoint, ServiceConfig);

            _httpEndpoint.Configure(RestConfig);
            _httpEndpoint.OpenAsync(null).Wait();
        }
Ejemplo 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);
        }
Ejemplo n.º 7
0
        private HttpEndpoint CreateLocalEndpoint()
        {
            var endpoint = new HttpEndpoint();

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

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

            return(endpoint);
        }
Ejemplo 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);
        }
Ejemplo 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);
        }