public void Ignore_TrailingSlash_Tests() { string baseUri = "http://localhost/myservice"; List <HttpOperationDescription> operationList1 = GenerateOperationsList1(); UriAndMethodOperationSelector httpOperationSelector = new UriAndMethodOperationSelector(new Uri(baseUri), operationList1, TrailingSlashMode.Ignore); string baseUriWithSlash = "http://localhost/myservice/"; List <HttpOperationDescription> operationList2 = GenerateOperationsList2(); UriAndMethodOperationSelector httpOperationSelector2 = new UriAndMethodOperationSelector(new Uri(baseUriWithSlash), operationList2, TrailingSlashMode.Ignore); //{"GetCollection", new UriTemplate("")}, VerifySelectOperationWithIgnoreMode(baseUri, "GetCollection", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/", "GetCollection", httpOperationSelector); // {"PQuery", new UriTemplate("p?value={value}")}, VerifySelectOperationWithIgnoreMode(baseUri + "/" + "p", "PQuery", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "p/", "PQuery", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "p", "PQuery", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "p?value=1", "PQuery", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "p/?value=1", "PQuery", httpOperationSelector); // {"QQuery", new UriTemplate("q/?value={value}")}, VerifySelectOperationWithIgnoreMode(baseUri + "/" + "q/?value=hello", "QQuery", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "q?value=hello", "QQuery", httpOperationSelector); // {"BarWildcard", new UriTemplate("bar/{*foo}")}, VerifySelectOperationWithIgnoreMode(baseUri + "/" + "bar/fooVal", "BarWildcard", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "bar/", "BarWildcard", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "bar", "BarWildcard", httpOperationSelector); // {"GetXY", new UriTemplate("x/y")}, VerifySelectOperationWithIgnoreMode(baseUri + "/" + "x/y", "GetXY", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "x/y/", "GetXY", httpOperationSelector); // {"GetRS", new UriTemplate("r/s/")}, VerifySelectOperationWithIgnoreMode(baseUri + "/" + "r/s/", "GetRS", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "r/s", "GetRS", httpOperationSelector); // {"Test", new UriTemplate("test/{a=1}")}, VerifySelectOperationWithIgnoreMode(baseUri + "/" + "test/", "Test", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "test/", "Test", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "test/aVal", "Test", httpOperationSelector); VerifySelectOperationWithIgnoreMode(baseUri + "/" + "test/aVal/", "Test", httpOperationSelector); // {"GetCollection", new UriTemplate("?value={value}")}, VerifySelectOperationWithIgnoreMode(baseUriWithSlash, "GetCollection", httpOperationSelector2); VerifySelectOperationWithIgnoreMode(baseUri + "/", "GetCollection", httpOperationSelector2); VerifySelectOperationWithIgnoreMode(baseUriWithSlash + "?value=hello", "GetCollection", httpOperationSelector2); VerifySelectOperationWithIgnoreMode(baseUri + "?value=hello", "GetCollection", httpOperationSelector2); // {"QQuery", new UriTemplate("q/{name}/?value={value}")}, VerifySelectOperationWithIgnoreMode(baseUriWithSlash + "q/nameVal/?value=hello", "QQuery", httpOperationSelector2); VerifySelectOperationWithIgnoreMode(baseUriWithSlash + "q/nameVal?value=hello", "QQuery", httpOperationSelector2); //{"GetId", new UriTemplate("{id}")}, VerifySelectOperationWithIgnoreMode(baseUriWithSlash + "idVal", "GetId", httpOperationSelector2); VerifySelectOperationWithIgnoreMode(baseUriWithSlash + "idVal/", "GetId", httpOperationSelector2); }
public static void VerifySelectOperationWithAutoRedirectMode(string requestUri, string operationName, UriAndMethodOperationSelector httpOperationSelector, bool shouldAutoRedirect) { string matchedOperationName; bool matchDifferByTrailingSlash; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri); bool result = httpOperationSelector.TrySelectOperation(request, out matchedOperationName, out matchDifferByTrailingSlash); Assert.IsTrue(result); Assert.AreEqual(matchedOperationName, operationName, String.Format("request {0} should match the operation {1}", requestUri, operationName)); Assert.AreEqual(matchDifferByTrailingSlash, shouldAutoRedirect); Uri originalUri = new Uri(requestUri); UriBuilder uriBuilder = new UriBuilder(originalUri); uriBuilder.Path = originalUri.AbsolutePath.EndsWith("/") ? uriBuilder.Path.TrimEnd('/') : uriBuilder.Path + "/"; Uri backSlashAlteredUri = uriBuilder.Uri; if (!shouldAutoRedirect) { matchedOperationName = httpOperationSelector.SelectOperation(request); Assert.AreEqual(matchedOperationName, operationName, String.Format("request {0} should match the operation {1}", requestUri, operationName)); } else { ExceptionAssert.Throws <HttpResponseException>( () => httpOperationSelector.SelectOperation(request), (responseException) => { HttpResponseMessage expectedResponse = StandardHttpResponseMessageBuilder.CreateTemporaryRedirectResponse(request, originalUri, backSlashAlteredUri); HttpAssert.AreEqual(expectedResponse, responseException.Response); }); } }
public static void VerifySelectOperationWithIgnoreMode(string requestUri, string operationName, UriAndMethodOperationSelector httpOperationSelector) { HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, requestUri); string matchedOperationName = httpOperationSelector.SelectOperation(message); Assert.AreEqual(matchedOperationName, operationName, String.Format("request {0} should match the operation {1}", requestUri, operationName)); }