Example #1
0
 private async Task <bool> SaveMockRelationInDB(Response response,
                                                ReceivedRequest receivedRequest,
                                                string requestMethodType)
 {
     try{
         var outcome = false;
         if (response != null &&
             receivedRequest != null &&
             requestMethodType != null)
         {
             var request = new Request(receivedRequest.Url, receivedRequest.Data)
             {
                 Type = requestMethodType
             };
             var mockRelationToSave = new MockRelation(request, response);
             _dataManager.AddMockRelation(mockRelationToSave);
             outcome = true;
         }
         return(outcome);
     }
     catch (Exception ex) {
         _logger.LogError(ex, "SaveMockRelationInDB");
         return(false);
     }
 }
Example #2
0
        public async Task <bool> AddMockRelation(MockRelation mockRelation)
        {
            try{
                var outcome = false;

                if (mockRelation != null)
                {
                    using (var db = new LiteDatabase(_dbFilePath))
                    {
                        var mockRelationsCollection = db.GetCollection <MockRelation>(_mockRelationCollectionName);

                        var mockRelationFiltered = mockRelationsCollection.FindOne(x => x.Request.Url == mockRelation.Request.Url &&
                                                                                   x.Request.Data == mockRelation.Request.Data);

                        if (mockRelationFiltered == null)
                        {
                            mockRelationsCollection.Insert(mockRelation);
                            mockRelationsCollection.EnsureIndex(x => x.Request.Url);
                            outcome = true;
                        }
                    }
                }

                return(outcome);
            }
            catch (Exception ex) {
                _logger.LogError(ex, "AddMockRelation");
                return(false);
            }
        }
Example #3
0
        private async Task <MockRelation> GetMockRelationOfReceivedRequest(ReceivedRequest receivedRequest, string requestType)
        {
            try{
                MockRelation outcome = null;

                var mockRelations = await this._dataManager.LoadMockRelations();

                var mockRelationsOfType = this._dataManager.GetMockRelations(mockRelations, requestType);

                if (receivedRequest != null &&
                    !string.IsNullOrEmpty(receivedRequest.Url) &&
                    mockRelationsOfType != null)
                {
                    foreach (var mockRelation in mockRelationsOfType)
                    {
                        if (mockRelation?.Request != null)
                        {
                            var matchedRoute = _routeMatcher.MatchRoute(receivedRequest.Url, mockRelation.Request.Url);
                            var urlIsEqual   = false;
                            var dataIsEqual  = false;
                            var hasData      = false;

                            if (matchedRoute != null && matchedRoute.IsMatch)
                            {
                                urlIsEqual = true;
                            }

                            if (receivedRequest.Data != null)
                            {
                                hasData = true;
                            }

                            if (hasData && urlIsEqual)
                            {
                                var jsonEscaped = JsonConvert.ToString(receivedRequest.Data);
                                if (mockRelation.Request.Data == jsonEscaped)
                                {
                                    dataIsEqual = true;
                                }
                            }

                            if ((urlIsEqual && !hasData) || (urlIsEqual && dataIsEqual))
                            {
                                outcome = mockRelation;
                                break;
                            }
                        }
                    }
                }

                return(outcome);
            }
            catch (Exception ex) {
                _logger.LogError(ex, "GetMockRelationOfReceivedRequest");
                return(null);
            }
        }
Example #4
0
        public async Task <Response> GetResponseFromRequest(ReceivedRequest receivedRequest, string requestMethodType)
        {
            try{
                Response outcome = null;
                if (receivedRequest != null && requestMethodType != null)
                {
                    MockRelation _mockRelationOfReceivedRequest = null;

                    _mockRelationOfReceivedRequest = await GetMockRelationOfReceivedRequest(receivedRequest, requestMethodType);

                    if (_mockRelationOfReceivedRequest != null)
                    {
                        outcome = _mockRelationOfReceivedRequest.Response;
                    }
                }
                return(outcome);
            }
            catch (Exception ex) {
                _logger.LogError(ex, "GetResponseFromRequest");
                return(null);
            }
        }