Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OwinFeatureCollectionTests.cs « test « Owin « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e92dab8444e913fb867c1141bddebbafc0d0b53f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Xunit;

namespace Microsoft.AspNetCore.Owin;

public class OwinHttpEnvironmentTests
{
    private T Get<T>(IFeatureCollection features)
    {
        return (T)features[typeof(T)];
    }

    private T Get<T>(IDictionary<string, object> env, string key)
    {
        object value;
        return env.TryGetValue(key, out value) ? (T)value : default(T);
    }

    [Fact]
    public void OwinHttpEnvironmentCanBeCreated()
    {
        var env = new Dictionary<string, object>
            {
                { "owin.RequestMethod", HttpMethods.Post },
                { "owin.RequestPath", "/path" },
                { "owin.RequestPathBase", "/pathBase" },
                { "owin.RequestQueryString", "name=value" },
            };
        var features = new OwinFeatureCollection(env);

        var requestFeature = Get<IHttpRequestFeature>(features);
        Assert.Equal(requestFeature.Method, HttpMethods.Post);
        Assert.Equal("/path", requestFeature.Path);
        Assert.Equal("/pathBase", requestFeature.PathBase);
        Assert.Equal("?name=value", requestFeature.QueryString);
    }

    [Fact]
    public void OwinHttpEnvironmentCanBeModified()
    {
        var env = new Dictionary<string, object>
            {
                { "owin.RequestMethod", HttpMethods.Post },
                { "owin.RequestPath", "/path" },
                { "owin.RequestPathBase", "/pathBase" },
                { "owin.RequestQueryString", "name=value" },
            };
        var features = new OwinFeatureCollection(env);

        var requestFeature = Get<IHttpRequestFeature>(features);
        requestFeature.Method = HttpMethods.Get;
        requestFeature.Path = "/path2";
        requestFeature.PathBase = "/pathBase2";
        requestFeature.QueryString = "?name=value2";

        Assert.Equal(HttpMethods.Get, Get<string>(env, "owin.RequestMethod"));
        Assert.Equal("/path2", Get<string>(env, "owin.RequestPath"));
        Assert.Equal("/pathBase2", Get<string>(env, "owin.RequestPathBase"));
        Assert.Equal("name=value2", Get<string>(env, "owin.RequestQueryString"));
    }
}