Get() public static method

public static Get ( string route ) : EndpointBuilder
route string
return EndpointBuilder
示例#1
0
        public async Task TestRouter()
        {
            var routeTable = new RouteTable(
                Route.Get("/").With(() => new TextResponse("root")),
                Route.Get("/1").With(() => new TextResponse("one")),
                Route.Get("/2").With(() => new TextResponse("two")),
                Route.Get("/3").With(() => new TextResponse("three")),
                Route.Get("/{number}/capture").With(() => new TextResponse("some number"))
                );

            var router = new Router(routeTable);

            var req1    = new HttpRequest(RemoteEndPoint, false, "localhost", Method.GET, new Uri("http://localhost/invalid"), "/invalid", string.Empty, new Dictionary <string, string>(), ContentTypes.Plain, 0, false, new MemoryStream());
            var result1 = await router.HandleRequest(req1, DateTime.UtcNow);

            var resp1 = result1.HttpResponse;

            Assert.AreEqual(HttpStatusCode.NotFound, resp1.StatusCode);

            var req2    = new HttpRequest(RemoteEndPoint, false, "localhost", Method.GET, new Uri("http://localhost/1"), "/1", string.Empty, new Dictionary <string, string>(), ContentTypes.Plain, 0, false, new MemoryStream());
            var result2 = await router.HandleRequest(req2, DateTime.UtcNow);

            var resp2 = result2.HttpResponse;

            Assert.IsInstanceOf <TextResponse>(resp2);
            Assert.AreEqual(ContentTypes.Plain, resp2.ContentType);
            using (var ms = new MemoryStream())
            {
                var bytesWritten = await resp2.Body.WriteToAsync(ms);

                Assert.AreEqual("one".Length, bytesWritten);
                Assert.AreEqual("one", System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, bytesWritten));
            }

            var req3 = new HttpRequest(RemoteEndPoint, false, "localhost", Method.GET, new Uri("http://localhost/8/capture"), "/8/capture", string.Empty, new Dictionary <string, string>(), ContentTypes.Plain, 0, false, new MemoryStream());
            await router.HandleRequest(req3, DateTime.UtcNow);

            Assert.AreEqual("8", req3.PathVariables["number"]);
        }
示例#2
0
        public void TestRouteTable()
        {
            Action noOp = () => {};

            Endpoint ep1 = Route.Get("/files/*").WithAction(noOp);
            Endpoint ep2 = Route.Get("/accounts/{id}").WithAction(noOp);
            Endpoint ep3 = Route.Get("/").WithAction(noOp);
            Endpoint ep4 = Route.Post("/accounts/").WithAction(noOp);
            Endpoint ep5 = Route.Get("/accounts/{id}/data").WithAction(noOp);
            Endpoint ep6 = Route.Get("/*").WithAction(noOp);

            var routeTable = new RouteTable(ep1, ep2, ep3, ep4, ep5, ep6);

            int matchedIndex;
            IReadOnlyDictionary <string, string> pathVariables;

            matchedIndex = routeTable.TryMatchEndpoint(Method.GET, new Uri("http://localhost/"), out pathVariables);
            Assert.AreSame(ep3, routeTable[matchedIndex]);

            matchedIndex = routeTable.TryMatchEndpoint(Method.GET, new Uri("http://localhost/accounts/111/data?keys=name"), out pathVariables);
            Assert.AreSame(ep5, routeTable[matchedIndex]);
            Assert.AreEqual("111", pathVariables["id"]);
            Assert.AreEqual(-1, routeTable.TryMatchEndpoint(Method.POST, new Uri("http://localhost/accounts/111/data"), out pathVariables));
            Assert.IsNull(pathVariables);

            matchedIndex = routeTable.TryMatchEndpoint(Method.GET, new Uri("http://localhost/files/images/test.png"), out pathVariables);
            Assert.AreSame(routeTable[matchedIndex], ep1);

            matchedIndex = routeTable.TryMatchEndpoint(Method.GET, new Uri("http://localhost/accounts/222"), out pathVariables);
            Assert.AreSame(ep2, routeTable[matchedIndex]);
            Assert.AreEqual("222", pathVariables["id"]);

            matchedIndex = routeTable.TryMatchEndpoint(Method.GET, new Uri("http://localhost/whatever"), out pathVariables);
            Assert.AreSame(ep6, routeTable[matchedIndex]);

            Assert.AreEqual(-1, routeTable.TryMatchEndpoint(Method.POST, new Uri("http://localhost/"), out pathVariables));
        }