예제 #1
0
        private static bool QueryStringMatch(NockedRequest nockedRequest, NameValueCollection requiredQueryHeaders, NameValueCollection queryHeaders)
        {
            var matched = true;

            foreach (var requiredKey in requiredQueryHeaders.AllKeys)
            {
                var requiredValue = requiredQueryHeaders[requiredKey];
                Log(nockedRequest, string.Format("Trying to match query '{0}'", requiredKey));

                var matchingKey = queryHeaders.AllKeys.FirstOrDefault(x => string.Equals(requiredKey, x));

                if (string.IsNullOrEmpty(matchingKey))
                {
                    Log(nockedRequest, string.Format("Query '{0}' could not be found", requiredKey));
                    matched = false;
                    break;
                }

                if (!string.Equals(requiredValue, queryHeaders[requiredKey]))
                {
                    Log(nockedRequest, string.Format("Query header value '{0}' did not match nocked request value '{1}'.", queryHeaders[requiredKey], requiredValue));
                    matched = false;
                    break;
                }
                else
                {
                    Log(nockedRequest, string.Format("Query header value '{0}' matched nocked request value '{1}'.", queryHeaders[requiredKey], requiredValue));
                }
            }

            Log(nockedRequest, "Query headers matched: " + matched);
            return(matched);
        }
예제 #2
0
        private static bool CheckMethod(NockHttpWebRequest request, NockedRequest nockedRequest)
        {
            Log(nockedRequest, string.Format("Trying to match requested method '{0}' against nocked method '{1}'", request.Method, nockedRequest.Method.ToString()));
            var match = (string.Equals(nockedRequest.Method.ToString(), request.Method, StringComparison.OrdinalIgnoreCase));

            Log(nockedRequest, "Method match: " + match);
            return(match);
        }
예제 #3
0
        private static bool CheckUrl(NockHttpWebRequest request, NockedRequest nockedRequest)
        {
            var requestUrl = request.RequestUri;
            var nockUrl    = (nockedRequest.Url + nockedRequest.Path);

            Log(nockedRequest, string.Format("Trying to match requested url '{0}' against nocked url '{1}'", requestUrl, nockUrl));
            var match = UrlMatcher.IsMatch(request, nockedRequest);

            Log(nockedRequest, "Url match: " + match);
            return(match);
        }
예제 #4
0
파일: Nock.cs 프로젝트: plog17/nock.net
        public nock Reply(HttpStatusCode statusCode, string response, NameValueCollection headers = null)
        {
            if (_built)
            {
                throw new Exception("The nock has already been built");
            }

            if (string.IsNullOrEmpty(_path))
            {
                throw new ArgumentException("Path must be defined");
            }

            if (headers == null)
            {
                headers = new NameValueCollection();
            }

            if (response == null)
            {
                response = string.Empty;
            }

            _nockedRequest                      = new NockedRequest(_url);
            _nockedRequest.Path                 = _path;
            _nockedRequest.Response             = response;
            _nockedRequest.StatusCode           = statusCode;
            _nockedRequest.Method               = _method;
            _nockedRequest.Body                 = _body;
            _nockedRequest.BodyMatcher          = _bodyMatcher;
            _nockedRequest.BodyMatcherString    = _bodyMatcherString;
            _nockedRequest.BodyMatcherFunc      = _bodyMatcherFunc;
            _nockedRequest.BodyMatcherType      = _bodyMatcherType;
            _nockedRequest.ResponseHeaders      = headers;
            _nockedRequest.HeaderMatcher        = _headerMatcher;
            _nockedRequest.RequestHeaders       = _requestHeaders;
            _nockedRequest.RequestHeaderMatcher = _requestHeadersMatcher;
            _nockedRequest.ResponseCreator      = _responseCreator;

            _nockedRequest.Query        = _query;
            _nockedRequest.QueryFunc    = _queryFunc;
            _nockedRequest.QueryMatcher = _queryMatcher;
            _nockedRequest.QueryResult  = _queryResult;

            _nockedRequest.Times  = 1;
            _nockedRequest.Logger = _logger;
            NockedRequests.Add(_nockedRequest);
            _built = true;

            return(this);
        }
예제 #5
0
        public static bool IsMatch(NockHttpWebRequest webRequest, NockedRequest nockedRequest)
        {
            var requestUrl = webRequest.RequestUri;
            var nockUrl    = (nockedRequest.Url + nockedRequest.Path);

            if (nockUrl.Contains("*"))
            {
                if (string.Equals(nockUrl, requestUrl))
                {
                    return(true);
                }

                var regEx = nockUrl
                            .Replace("/", "\\/")
                            .Replace(".", "\\.")
                            .Replace("*", "[^ ]*")
                            .Replace("?", "\\?");

                try
                {
                    return(Regex.IsMatch(requestUrl, "^" + regEx + "$"));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Nock.net: An error occurred while trying to match the url based on wildcards: " + ex.Message);
                }

                return(false);
            }
            else
            {
                var nockContainsQuery    = nockUrl.Contains("?");
                var requestContainsQuery = requestUrl.Contains("?");

                if (nockContainsQuery)
                {
                    return(string.Equals(nockUrl, requestUrl));
                }
                else
                {
                    if (requestContainsQuery)
                    {
                        requestUrl = requestUrl.Substring(0, requestUrl.IndexOf("?"));
                    }

                    return(string.Equals(nockUrl, requestUrl));
                }
            }
        }
예제 #6
0
        private static void Log(NockedRequest nockedRequest, string message)
        {
            if (nockedRequest.Logger == null)
            {
                return;
            }

            try
            {
                nockedRequest.Logger("Nock.net: " + message);
            }
            catch (Exception)
            {
            }
        }
예제 #7
0
        private static bool CheckHeaders(NockHttpWebRequest request, NockedRequest nockedRequest)
        {
            if (nockedRequest.HeaderMatcher == HeaderMatcher.None)
            {
                return(true);
            }

            if (!RequestHeadersMatch(nockedRequest, nockedRequest.RequestHeaders, request.Headers))
            {
                return(false);
            }

            if (nockedRequest.HeaderMatcher == HeaderMatcher.ExactMatch && nockedRequest.RequestHeaders.Count != request.Headers.Count)
            {
                Log(nockedRequest, "Exact header match is enabled and the number of nocked request headers and actual request headers did not match");
                return(false);
            }

            if (nockedRequest.RequestHeaderMatcher != null)
            {
                try
                {
                    var match = nockedRequest.RequestHeaderMatcher(request.Headers);
                    Log(nockedRequest, "Custom header matcher result: " + match);
                    return(match);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Nock.net: An error occurred while trying to check the request headers: " + ex.Message);
                    Log(nockedRequest, "An error occurred while trying to check the request headers: " + ex.Message);
                    Log(nockedRequest, "Custom header matcher result: false");
                    return(false);
                }
            }

            Log(nockedRequest, "Header matcher result: true");
            return(true);
        }
예제 #8
0
        private static bool CheckBody(NockHttpWebRequest request, NockedRequest nockedRequest, string body)
        {
            if (nockedRequest.BodyMatcher != BodyMatcher.None)
            {
                switch (nockedRequest.BodyMatcher)
                {
                case BodyMatcher.Body:
                    if (!string.Equals(nockedRequest.Body, body))
                    {
                        Log(nockedRequest, string.Format("The requested body '{0}' does not match the nocked request body '{1}'", body, nockedRequest.Body));
                        return(false);
                    }
                    break;

                case BodyMatcher.StringFunc:
                    try
                    {
                        var match = nockedRequest.BodyMatcherString(body);
                        Log(nockedRequest, "Custom body matcher result: " + match);
                        return(match);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Nock.net: An error occurred while trying to check the request body: " + ex.Message);
                        Log(nockedRequest, "An error occurred while trying to check the request body: " + ex.Message);
                        Log(nockedRequest, "Body matcher result: false");
                        return(false);
                    }

                case BodyMatcher.TypedFunc:
                    var    property   = nockedRequest.BodyMatcherFunc.GetType().GetProperty("Method");
                    var    value      = property.GetValue(nockedRequest.BodyMatcherFunc);
                    var    methodInfo = (System.Reflection.MethodInfo)value;
                    object obj        = null;

                    try
                    {
                        obj = JsonConvert.DeserializeObject(body, nockedRequest.BodyMatcherType);
                    }
                    catch (Exception ex)
                    {
                        obj = null;
                        Console.WriteLine("Nock.net: The response body could not be deserialized into type: " + nockedRequest.BodyMatcherType.Name + ". " + ex.Message);
                        Log(nockedRequest, "Nock.net: The response body could not be deserialized into type: " + nockedRequest.BodyMatcherType.Name + ". " + ex.Message);
                    }

                    bool success = false;

                    try
                    {
                        success = (bool)methodInfo.Invoke(null, new object[] { obj });
                    }
                    catch (Exception ex)
                    {
                        obj = null;
                        Console.WriteLine("Nock.net: An error occurred while trying to check the request body: " + ex.Message);
                        Log(nockedRequest, "An error occurred while trying to check the request body: " + ex.Message);
                        Log(nockedRequest, "Body matcher result: false");
                    }

                    if (!success)
                    {
                        Log(nockedRequest, "Body matcher result: false");
                        return(false);
                    }
                    break;
                }
            }

            Log(nockedRequest, "Body matcher result: true");
            return(true);
        }
예제 #9
0
        private static bool CheckQuery(NockHttpWebRequest request, NockedRequest nockedRequest)
        {
            if (request.Query.Count == 0 && nockedRequest.QueryMatcher == QueryMatcher.None)
            {
                return(true);
            }

            if (request.Query.Count > 0 && !request.RequestUri.Contains("?") && nockedRequest.QueryMatcher == QueryMatcher.None)
            {
                Log(nockedRequest, "A query string has been specified in the request url, but query matcher has not been defined");
                Log(nockedRequest, "Query matcher result: false");
                return(false);
            }

            if (nockedRequest.QueryMatcher == QueryMatcher.None)
            {
                return(true);
            }

            if (nockedRequest.QueryMatcher == QueryMatcher.NameValue || nockedRequest.QueryMatcher == QueryMatcher.NameValueExact)
            {
                if (!QueryStringMatch(nockedRequest, nockedRequest.Query, request.Query))
                {
                    return(false);
                }

                if (nockedRequest.QueryMatcher == QueryMatcher.NameValueExact && nockedRequest.Query.Count != request.Query.Count)
                {
                    Log(nockedRequest, "Exact query match is enabled and the number of nocked request query keys and actual request query keys did not match");
                    return(false);
                }
            }

            if (nockedRequest.QueryMatcher == QueryMatcher.Bool)
            {
                Log(nockedRequest, "Query matcher result: " + nockedRequest.QueryResult);
                return(nockedRequest.QueryResult);
            }

            if (nockedRequest.QueryFunc != null)
            {
                try
                {
                    var url = request.RequestUri;

                    if (url.Contains("?"))
                    {
                        url = url.Substring(0, url.IndexOf("?"));
                    }

                    var match = nockedRequest.QueryFunc(new QueryDetails(url, request.Query));
                    Log(nockedRequest, "Query matcher result: " + match);
                    return(match);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Nock.net: An error occurred while trying to check the query: " + ex.Message);
                    Log(nockedRequest, "An error occurred while trying to check the query: " + ex.Message);
                    Log(nockedRequest, "Custom query matcher result: false");
                    return(false);
                }
            }

            Log(nockedRequest, "Query matcher result: true");
            return(true);
        }