コード例 #1
0
        public void WritesWork()
        {
            var body         = new MemoryStream(4);
            var expectedBody = new byte[] { 65, 66, 67, 68 };
            var cts          = new CancellationTokenSource();
            var env          = new Dictionary <string, object>(StringComparer.Ordinal)
            {
                { "owin.CallCancelled", cts.Token },
                { "owin.ResponseBody", body },
            };

            var res = new OwinResponse(env);

            res.Write("AB");
            res.WriteAsync("CD").Wait();

            body.GetBuffer().ShouldBe(expectedBody);
            res.CallCancelled.ShouldBe(cts.Token);
        }
コード例 #2
0
        public void Configuration(IAppBuilder app)
        {
            app.UseDispatcher(dispatcher =>
            {
                // list all the things:
                dispatcher.Get("/things", (environment, next) =>
                {
                    var response = new OwinResponse(environment)
                    {
                        StatusCode  = 200,
                        ContentType = "text/plain"
                    };

                    response.Write("# All the things:");
                    response.Write(Environment.NewLine);
                    response.Write(Environment.NewLine);

                    foreach (var thing in _things.Values)
                    {
                        response.Write(String.Concat("- Thing #", thing.Id, ": ", thing.Name));
                        response.Write(Environment.NewLine);
                    }

                    return(Task.FromResult((object)null));
                });

                // create a new thing:
                dispatcher.Post("/things", async(environment, next) =>
                {
                    var request = new OwinRequest(environment);
                    var form    = await request.ReadFormAsync();

                    var response = new OwinResponse(environment);

                    var thingName = form["name"];

                    if (thingName == null)
                    {
                        response.StatusCode = 400;
                        await response.WriteAsync("The thing to POST is missing a name.");
                        return;
                    }

                    _things.Add(++_lastThingId, new Thing
                    {
                        Id   = _lastThingId,
                        Name = thingName
                    });
                    var uri = String.Concat("/things/", _lastThingId);

                    response.StatusCode          = 201;
                    response.Headers["Location"] = uri;
                    response.ContentType         = "text/plain";
                    await response.WriteAsync(uri);
                });

                // list all the things:
                dispatcher.Get("/things/{id}", (environment, @params, next) =>
                {
                    var response = new OwinResponse(environment);

                    int id;
                    if (!int.TryParse(@params.id, out id))
                    {
                        response.StatusCode = 404;
                        return(response.WriteAsync("Not found."));
                    }

                    if (!_things.ContainsKey(id))
                    {
                        response.StatusCode = 404;
                        return(response.WriteAsync("Not found."));
                    }

                    var thing = _things[id];

                    response.StatusCode  = 200;
                    response.ContentType = "text/plain";

                    return(response.WriteAsync(String.Concat("Thing #", thing.Id, " is ", thing.Name, ".")));
                });
            });
        }
コード例 #3
0
        public void WritesWork()
        {
            var body = new MemoryStream(4);
            var expectedBody = new byte[] { 65, 66, 67, 68 };
            var cts = new CancellationTokenSource();
            var env = new Dictionary<string, object>(StringComparer.Ordinal)
                {
                    { "owin.CallCancelled", cts.Token },
                    { "owin.ResponseBody", body },
                };

            var res = new OwinResponse(env);
            res.Write("AB");
            res.WriteAsync("CD").Wait();

            body.GetBuffer().ShouldBe(expectedBody);
            res.CallCancelled.ShouldBe(cts.Token);
        }