Beispiel #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);
        }
Beispiel #2
0
 //--- Class Methods ---
 /// <summary>
 /// Register a callback to intercept any calls to a uri and its child paths.
 /// </summary>
 /// <param name="uri">Base Uri to intercept.</param>
 /// <param name="mock">Interception callback.</param>
 public static void Register(XUri uri, MockInvokeDelegate mock)
 {
     MockEndpoint.Instance.Register(uri, mock);
 }
Beispiel #3
0
 //--- Constructors ---
 public MockInvokee(XUri uri, MockInvokeDelegate callback, int endpointScore)
 {
     _uri           = uri;
     _callback      = callback;
     _endpointScore = endpointScore;
 }
Beispiel #4
0
 /// <summary>
 /// Register a callback to intercept any calls to a uri and its child paths.
 /// </summary>
 /// <param name="uri">Base Uri to intercept.</param>
 /// <param name="mock">Interception callback.</param>
 /// <param name="endpointScore">The score to return to <see cref="IPlugEndpoint.GetScoreWithNormalizedUri"/> for this uri.</param>
 public static void Register(XUri uri, MockInvokeDelegate mock, int endpointScore)
 {
     MockEndpoint.Instance.Register(new MockInvokee(uri, mock, endpointScore));
 }
Beispiel #5
0
        //--- Class Methods ---

        /// <summary>
        /// Register a callback to intercept any calls to a uri and its child paths.
        /// </summary>
        /// <param name="uri">Base Uri to intercept.</param>
        /// <param name="mock">Interception callback.</param>
        public static void Register(XUri uri, MockInvokeDelegate mock)
        {
            Register(uri, mock, int.MaxValue);
        }
Beispiel #6
0
 /// <summary>
 /// Register a callback to intercept any calls to a uri and its child paths.
 /// </summary>
 /// <param name="uri">Base Uri to intercept.</param>
 /// <param name="mock">Interception callback.</param>
 /// <param name="endpointScore">The score to return to <see cref="IPlugEndpoint.GetScoreWithNormalizedUri"/> for this uri.</param>
 public static void Register(XUri uri, MockInvokeDelegate mock, int endpointScore)
 {
     MockEndpoint.Instance.Register(new MockInvokee(uri, mock, endpointScore));
 }
Beispiel #7
0
 //--- Class Methods ---
 /// <summary>
 /// Register a callback to intercept any calls to a uri and its child paths.
 /// </summary>
 /// <param name="uri">Base Uri to intercept.</param>
 /// <param name="mock">Interception callback.</param>
 public static void Register(XUri uri, MockInvokeDelegate mock)
 {
     Register(uri, mock, int.MaxValue);
 }
Beispiel #8
0
 //--- Constructors ---
 public MockInvokee(XUri uri, MockInvokeDelegate callback, int endpointScore)
 {
     _uri = uri;
     _callback = callback;
     _endpointScore = endpointScore;
 }