本文整理汇总了C#中Microsoft.AspNet.Http.Internal.DefaultHttpContext.GetFeature方法的典型用法代码示例。如果您正苦于以下问题:C# DefaultHttpContext.GetFeature方法的具体用法?C# DefaultHttpContext.GetFeature怎么用?C# DefaultHttpContext.GetFeature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNet.Http.Internal.DefaultHttpContext
的用法示例。
在下文中一共展示了DefaultHttpContext.GetFeature方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadFormAsync_SimpleData_ReturnsParsedFormCollection
public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection()
{
// Arrange
var formContent = Encoding.UTF8.GetBytes("foo=bar&baz=2");
var context = new DefaultHttpContext();
context.Request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
context.Request.Body = new MemoryStream(formContent);
// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
Assert.Null(formFeature);
// Act
var formCollection = await context.Request.ReadFormAsync();
// Assert
Assert.Equal("bar", formCollection["foo"]);
Assert.Equal("2", formCollection["baz"]);
// Cached
formFeature = context.GetFeature<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formFeature.Form, formCollection);
}
示例2: ExpectedKeysAreAvailable
public Task ExpectedKeysAreAvailable()
{
var handler = new ClientHandler(env =>
{
var context = new DefaultHttpContext((IFeatureCollection)env);
// TODO: Assert.True(context.RequestAborted.CanBeCanceled);
Assert.Equal("HTTP/1.1", context.Request.Protocol);
Assert.Equal("GET", context.Request.Method);
Assert.Equal("https", context.Request.Scheme);
Assert.Equal("/A/Path", context.Request.PathBase.Value);
Assert.Equal("/and/file.txt", context.Request.Path.Value);
Assert.Equal("?and=query", context.Request.QueryString.Value);
Assert.NotNull(context.Request.Body);
Assert.NotNull(context.Request.Headers);
Assert.NotNull(context.Response.Headers);
Assert.NotNull(context.Response.Body);
Assert.Equal(200, context.Response.StatusCode);
Assert.Null(context.GetFeature<IHttpResponseFeature>().ReasonPhrase);
Assert.Equal("example.com", context.Request.Host.Value);
return Task.FromResult(0);
}, new PathString("/A/Path/"));
var httpClient = new HttpClient(handler);
return httpClient.GetAsync("https://example.com/A/Path/and/file.txt?and=query");
}
示例3: SetItems_NewCollectionUsed
public void SetItems_NewCollectionUsed()
{
var context = new DefaultHttpContext(new FeatureCollection());
Assert.Null(context.GetFeature<IItemsFeature>());
var items = new Dictionary<object, object>();
context.Items = items;
Assert.NotNull(context.GetFeature<IItemsFeature>());
Assert.Same(items, context.Items);
var item = new object();
items["foo"] = item;
Assert.Same(item, context.Items["foo"]);
}
示例4: GetItems_DefaultCollectionProvided
public void GetItems_DefaultCollectionProvided()
{
var context = new DefaultHttpContext(new FeatureCollection());
Assert.Null(context.GetFeature<IItemsFeature>());
var items = context.Items;
Assert.NotNull(context.GetFeature<IItemsFeature>());
Assert.NotNull(items);
Assert.Same(items, context.Items);
var item = new object();
context.Items["foo"] = item;
Assert.Same(item, context.Items["foo"]);
}
示例5: CreateRequest
private static HttpRequest CreateRequest(IDictionary<string, string[]> headers)
{
var context = new DefaultHttpContext();
context.GetFeature<IHttpRequestFeature>().Headers = headers;
return context.Request;
}
示例6: ReadFormAsync_MultipartWithFieldAndFile_ReturnsParsedFormCollection
public async Task ReadFormAsync_MultipartWithFieldAndFile_ReturnsParsedFormCollection()
{
var formContent = Encoding.UTF8.GetBytes(MultipartFormWithFieldAndFile);
var context = new DefaultHttpContext();
context.Request.ContentType = MultipartContentType;
context.Request.Body = new MemoryStream(formContent);
// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
Assert.Null(formFeature);
var formCollection = await context.Request.ReadFormAsync();
Assert.NotNull(formCollection);
// Cached
formFeature = context.GetFeature<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formFeature.Form, formCollection);
Assert.Same(formCollection, context.Request.Form);
// Content
Assert.Equal(1, formCollection.Count);
Assert.Equal("Foo", formCollection["description"]);
Assert.NotNull(formCollection.Files);
Assert.Equal(1, formCollection.Files.Count);
var file = formCollection.Files["myfile1"];
Assert.Equal("text/html", file.ContentType);
Assert.Equal(@"form-data; name=""myfile1""; filename=""temp.html""", file.ContentDisposition);
var body = file.OpenReadStream();
using (var reader = new StreamReader(body))
{
var content = reader.ReadToEnd();
Assert.Equal(content, "<html><body>Hello World</body></html>");
}
}
示例7: ReadForm_MultipartWithField_ReturnsParsedFormCollection
public async Task ReadForm_MultipartWithField_ReturnsParsedFormCollection()
{
var formContent = Encoding.UTF8.GetBytes(MultipartFormWithField);
var context = new DefaultHttpContext();
context.Request.ContentType = MultipartContentType;
context.Request.Body = new MemoryStream(formContent);
// Not cached yet
var formFeature = context.GetFeature<IFormFeature>();
Assert.Null(formFeature);
var formCollection = context.Request.Form;
Assert.NotNull(formCollection);
// Cached
formFeature = context.GetFeature<IFormFeature>();
Assert.NotNull(formFeature);
Assert.NotNull(formFeature.Form);
Assert.Same(formCollection, formFeature.Form);
Assert.Same(formCollection, await context.Request.ReadFormAsync());
// Content
Assert.Equal(1, formCollection.Count);
Assert.Equal("Foo", formCollection["description"]);
Assert.NotNull(formCollection.Files);
Assert.Equal(0, formCollection.Files.Count);
}