public void BeforeEach()
 {
     _endpoint = new Endpoint
     {
         Request = { Url = "/something" },
         Responses = new List<Response> { new Response { Status = 200 }}
     };
 }
        public static Endpoint Verify(Endpoint endpoint)
        {
            endpoint = endpoint ?? new Endpoint();

            return new Endpoint
            {
                Request = VerifyRequest(endpoint.Request),
                Responses = VerifyResponses(endpoint.Responses)
            };
        }
        public void Find_ShouldRetrieveEndpoint_ByUrl()
        {
            var inserted = new Endpoint { Request = new Request {Url = "/phantom"} };
            var incoming = new Endpoint { Request = new Request {Url = "/phantom", Post = "A string!"} };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.NotNull(actual);
        }
        public void Find_ShouldRetreiveEndpoint_WhenPostMatches()
        {
            var inserted = new Endpoint { Request = new Request {Url = "/phantom", Post = "A string!"} };
            var incoming = new Endpoint
            {
                Request = new Request {Url = "/phantom", Method = new List<string> {"GET"}, Post = "A string!"}
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.NotNull(actual);
        }
        public void Find_ShouldRetrieveEndpoint_WhenFileIsntFound_ButPostMatchesIncomingPost()
        {
            var inserted = new Endpoint
            {
                Request = new Request { Url = "/phantom", Post = "Some post contents!", File = "../../Files/nowhere.txt" }
            };
            var incoming = new Endpoint
            {
                Request = new Request {Url = "/phantom", Method = new List<string> {"GET"}, Post = "Some post contents!"}
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.NotNull(actual);
        }
Exemple #6
0
        public Response Find(Endpoint incoming)
        {
            KeyValuePair<uint, Endpoint> keyValue;
            try {
                keyValue = (from stored in _dictionary where stored.Value.Matches(incoming) select stored).First();
            } catch {
                return null;
            }

            var id = keyValue.Key;
            var endpoint = keyValue.Value;

            int sightings = _sightings[id];
            _sightings[id] = sightings + 1;

            return keyValue.Value.Responses[sightings % endpoint.Responses.Count];
        }
Exemple #7
0
        private static Endpoint ParseEndpoint(YamlMappingNode yamlEndpoint)
        {
            var endpoint = new Endpoint();

            foreach(var requestResponse in yamlEndpoint.Children) {
                switch(requestResponse.Key.ToString()) {
                    case "request":
                        {
                            endpoint.Request = ParseRequest((YamlMappingNode) requestResponse.Value);
                            break;
                        }
                    case "response":
                        {
                            endpoint.Responses = ParseResponses(requestResponse);
                            break;
                        }
                }
            }

            return endpoint;
        }
        public void Find_ShouldReturnNull_WhenQueryDoesntMatch()
        {
            var inserted = new Endpoint
            {
                Request =
               new Request {Url = "/phantom", Query = new NameValueCollection {{"alpha", "a"}, {"beta", "b"},}}
            };
            var incoming = new Endpoint
            {
                Request =
               new Request {
                  Url = "/phantom",
                  Method = new List<string> {"GET"},
                  Post = "A string!",
                  Query = new NameValueCollection {{"alpha", "c"}, {"beta", "b"}, {"kappa", "k"}}
               }
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.IsNull(actual);
        }
Exemple #9
0
 internal bool Matches(Endpoint other)
 {
     return Request.Matches(other.Request);
 }
Exemple #10
0
 /// <summary>
 /// Add a new endpoint configuration
 /// </summary>
 /// <param name="endpoint">The new endpoint data</param>
 /// <param name="id">The new generated id, for use with Replace/Delete</param>
 /// <returns>True if the operation succeeded</returns>
 public bool Add(Endpoint endpoint, out uint id)
 {
     return _endpointDb.Insert(endpoint, out id);
 }
        public void Find_ShouldReturnNull_WhenFileDoesntMatchIncomingPost()
        {
            var inserted = new Endpoint
            {
                Request = new Request {Url = "/phantom", File = "../../Files/someFileContents.txt"}
            };
            var incoming = new Endpoint
            {
                Request = new Request {Url = "/phantom", Method = new List<string> {"GET"}, Post = "Some non-existant file contents!"}
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.IsNull(actual);
        }
        public void Find_ShouldReturnNull_WhenEndpointNotFound()
        {
            var inserted = new Endpoint { Request = new Request {Url = "/phantom"} };
            var incoming = new Endpoint { Request = new Request {Url = "/of/the/opera", Post = "A string!"} };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.IsNull(actual);
        }
        public void Find_ShouldRetrieveEndpoint_WhenUrlIsARegexMatchOnIncomingRequest()
        {
            var inserted = new Endpoint
            {
                Request = new Request { Url = "/phantom$", Method = new List<string> {"POST"}}
            };
            var incoming = new Endpoint
            {
                Request = new Request { Url = "/ghost/or/phantom", Method = new List<string> { "POST" }}
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.NotNull(actual);
        }
Exemple #14
0
        public bool Insert(Endpoint endpoint, out uint id)
        {
            id = NextId();
            var verified = Defaults.Verify(endpoint);

            if(!_dictionary.TryAdd(id, verified))
                return false;

            var methods = verified.Request.Method.Aggregate("", (current, verb) => current + (verb + ",")).TrimEnd(',');
            _sightings.Add(id, 0);
            if(Notify) Out.Notice(string.Format(Loaded, methods, verified.Request.Url));
            return true;
        }
Exemple #15
0
 public bool Insert(Endpoint endpoint)
 {
     uint i;
     return Insert(endpoint, out i);
 }
Exemple #16
0
        public bool Replace(uint id, Endpoint endpoint)
        {
            if(!_dictionary.ContainsKey(id))
                return false;

            _dictionary[id] = Defaults.Verify(endpoint);
            return true;
        }
Exemple #17
0
 /// <summary>
 /// Swap out the configuration of one of the endpoints
 /// </summary>
 /// <param name="id">The id of the endpoint to replace</param>
 /// <param name="endpoint">The new endpoint data</param>
 /// <returns>True if the operation succeeded</returns>
 public bool Replace(uint id, Endpoint endpoint)
 {
     return _endpointDb.Replace(id, endpoint);
 }
        public void Find_ShouldReturnNull_WhenHeadersDontMatch()
        {
            var inserted = new Endpoint
            {
                Request =
               new Request {
                  Url = "/phantom",
                  Headers =
                     new NameValueCollection {
                        {"Content-Type", "application/json"},
                        {"Content-Disposition", "attachment"},
                     }
               }
            };
            var incoming = new Endpoint
            {
                Request =
               new Request {
                  Url = "/phantom",
                  Method = new List<string> {"GET"},
                  Post = "A string!",
                  Headers =
                     new NameValueCollection {
                        {"Content-Type", "application/xml"},
                        {"Content-Disposition", "attachment"},
                        {"Server", "somethingSpecial"}
                     }
               }
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.IsNull(actual);
        }
Exemple #19
0
        private Response FindEndpoint(HttpListenerContext context)
        {
            var incoming = new Endpoint
            {
                Request = {
               Url = context.Request.Url.AbsolutePath,
               Method = new List<string> {context.Request.HttpMethod.ToUpper()},
               Headers = CreateNameValueCollection(context.Request.Headers, caseSensitiveKeys: false),
               Query = CreateNameValueCollection(context.Request.QueryString),
               Post = utils.ReadPost(context.Request)
            }
            };

            var found = _endpointDb.Find(incoming);
            return found;
        }
        public void Find_ShouldReturnNull_WhenMethodNotInList()
        {
            var inserted = new Endpoint
            {
                Request = new Request {Url = "/phantom", Method = new List<string> {"POST", "HEAD"}}
            };
            var incoming = new Endpoint
            {
                Request = new Request {Url = "/phantom", Method = new List<string> {"GET"}, Post = "A string!"}
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.IsNull(actual);
        }
Exemple #21
0
 protected bool Equals(Endpoint other)
 {
     return Equals(Request, other.Request) && Equals(Responses, other.Responses);
 }
        public void Find_ShouldReturnNull_WhenPostIsDifferent()
        {
            var inserted = new Endpoint { Request = new Request {Url = "/phantom", Post = "Tsk tsk."} };
            var incoming = new Endpoint
            {
                Request = new Request {Url = "/phantom", Method = new List<string> {"GET"}, Post = "A string!"}
            };

            _endpointDb.Insert(inserted);

            var actual = _endpointDb.Find(incoming);

            Assert.IsNull(actual);
        }