Example #1
0
        /// <summary>
        /// Setup a new <see cref="MockPlug"/> interceptor candidate for a uri and its child paths.
        /// </summary>
        /// <remarks>
        /// This mechanism has not been completed and is only a WIP.
        /// Must further configure ordered <see cref="IMockInvokeExpectationParameter"/> parameters to make validation possible.
        /// Note: endPointScore is only set on the first set for a specific baseUri. Subsequent values are ignored.
        /// </remarks>
        /// <param name="baseUri">Base Uri to intercept.</param>
        /// <param name="name">Debug name for setup</param>
        /// <param name="endPointScore">The score to return to <see cref="IPlugEndpoint.GetScoreWithNormalizedUri"/> for this uri.</param>
        /// <returns>A new interceptor instance that may intercept the uri, depending on its additional matching parameters.</returns>
        public static IMockPlug Setup(XUri baseUri, string name, int endPointScore)
        {
            List <MockPlug> mocks;
            var             key = baseUri.SchemeHostPortPath;

            lock (_mocks) {
                if (!_mocks.TryGetValue(key, out mocks))
                {
                    mocks = new List <MockPlug>();
                    MockInvokeDelegate callback = (plug, verb, uri, request, response) => {
                        _log.DebugFormat("checking setups for match on {0}:{1}", verb, uri);
                        MockPlug bestMatch  = null;
                        var      matchScore = 0;
                        foreach (var match in mocks)
                        {
                            var score = match.GetMatchScore(verb, uri, request);
                            if (score > matchScore)
                            {
                                bestMatch  = match;
                                matchScore = score;
                            }
                        }
                        if (bestMatch == null)
                        {
                            _log.Debug("no match");
                            response.Return(DreamMessage.Ok(new XDoc("empty")));
                        }
                        else
                        {
                            _log.DebugFormat("[{0}] matched", bestMatch.Name);
                            response.Return(bestMatch.Invoke(verb, uri, request));
                        }
                    };
                    MockEndpoint.Instance.Register(new MockInvokee(baseUri, callback, endPointScore));
                    MockEndpoint.Instance.AllDeregistered += Instance_AllDeregistered;
                    _mocks.Add(key, mocks);
                }
            }
            var mock = new MockPlug(baseUri, name);

            mocks.Add(mock);
            return(mock);
        }