本文整理汇总了C#中Nancy.Request类的典型用法代码示例。如果您正苦于以下问题:C# Request类的具体用法?C# Request怎么用?C# Request使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Request类属于Nancy命名空间,在下文中一共展示了Request类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PublicEndpoint
public static void PublicEndpoint(this Response response, Request request)
{
response
.WithHeader("Access-Control-Allow-Origin", "*")
.WithHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE, GET")
.WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type, X-Requested-With");
}
示例2: Formats
public static Format[] Formats(Request request)
{
var dictionary = request.Form as IDictionary<string, object>;
string name = "{0}.Name";
string legality = "{0}.Legality";
string[] keys = dictionary.Select(x => x.Key)
.Where(x => x.StartsWith("Format"))
.ToArray();
keys = keys.Select(x => x.Substring(0,x.IndexOf('.'))).Distinct().ToArray();
List<Format> formats = new List<Format>();
if(keys != null && keys.Length > 0)
{
keys = keys.OrderBy(x => x).ToArray();
foreach(string key in keys)
{
formats.Add(new Format(){
Name = dictionary[string.Format(name, key)].ToString(),
Legality = dictionary[string.Format(legality, key)].ToString()
});
}
}
return formats.ToArray();
}
示例3: Should_have_a_file_with_the_correct_data_in_it
public void Should_have_a_file_with_the_correct_data_in_it()
{
// Given
var expected = "wazaa";
var stream = new MemoryStream(BuildMultipartFileValues(new Dictionary<string, Tuple<string, string, string>>
{
{ "sample.txt", new Tuple<string, string, string>("content/type", expected, "name")}
}));
var headers = new Dictionary<string, IEnumerable<string>>
{
{ "content-type", new[] { "multipart/form-data; boundary=----NancyFormBoundary" } }
};
// When
var request = new Request("POST", "/", headers, CreateRequestStream(stream), "http");
// Then
var fileValue = request.Files.Single().Value;
var actualBytes = new byte[fileValue.Length];
fileValue.Read(actualBytes, 0, (int)fileValue.Length);
var actual = Encoding.ASCII.GetString(actualBytes);
actual.ShouldEqual(expected);
}
示例4: configureResponse
private void configureResponse(ResponseFormat expectedFormat)
{
Request req;
var url = new Url { Scheme = "http", Path = "/" };
switch (expectedFormat)
{
case ResponseFormat.Html:
req = new Request("GET", url, null, new Dictionary<string, IEnumerable<string>> { { "Accept", _acceptHtmlHeaders } },
null);
break;
case ResponseFormat.Json:
req = new Request("GET", url, null, new Dictionary<string, IEnumerable<string>> { { "Accept", _acceptJsonHeaders } },
null);
break;
case ResponseFormat.Xml:
req = new Request("GET", url, null, new Dictionary<string, IEnumerable<string>> { { "Accept", _acceptXmlHeaders } },
null);
break;
default:
Assert.Fail("Invalid response format");
return;
}
_euclidApi.Context = new NancyContext { Request = req };
_euclidApi.Response = _formatter;
A.CallTo(() => _formatter.Context).Returns(_euclidApi.Context);
}
示例5: If_the_stream_ends_with_carriage_return_characters_it_should_not_affect_the_multipart
public void If_the_stream_ends_with_carriage_return_characters_it_should_not_affect_the_multipart()
{
// Given
var expected = "#!/usr/bin/env rake\n# Add your own tasks in files placed in lib/tasks ending in .rake,\n# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.\n\nrequire File.expand_path('../config/application', __FILE__)\n\nOnlinebackupWebclient::Application.load_tasks";
var data = string.Format("--69989\r\nContent-Disposition: form-data; name=\"Stream\"; filename=\"Rakefile\"\r\nContent-Type: text/plain\r\n\r\n{0}\r\n--69989--\r\n", expected);
var stream = new MemoryStream(Encoding.ASCII.GetBytes(data));
var headers = new Dictionary<string, IEnumerable<string>>
{
{"Content-Type", new [] { "multipart/form-data; boundary=69989"} },
{"Content-Length", new [] {"403"} }
};
// When
var request = new Request("POST", "/", headers, CreateRequestStream(stream), "http");
// Then
var fileValue = request.Files.Single().Value;
var actualBytes = new byte[fileValue.Length];
fileValue.Read(actualBytes, 0, (int)fileValue.Length);
var actual = Encoding.ASCII.GetString(actualBytes);
actual.ShouldEqual(expected);
}
示例6: Create_WithRequest_CallsMockContentServiceAndAssignsRequestResponsePairsOnNancyContextItem
public void Create_WithRequest_CallsMockContentServiceAndAssignsRequestResponsePairsOnNancyContextItem()
{
var request = new Request("GET", "/events", "HTTP");
var requestResponsePairs = new List<ProviderServiceInteraction>
{
new ProviderServiceInteraction() { Request = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/events" }, Response = new ProviderServiceResponse() },
new ProviderServiceInteraction() { Request = new ProviderServiceRequest { Method = HttpVerb.Post, Path = "/events" }, Response = new ProviderServiceResponse() },
};
var mockMockContextService = Substitute.For<IMockContextService>();
var mockCultureService = Substitute.For<ICultureService>();
var mockRequestTraceFactory = Substitute.For<IRequestTraceFactory>();
var mockTextResource = Substitute.For<ITextResource>();
mockMockContextService.GetExpectedRequestResponsePairs().Returns(requestResponsePairs);
INancyContextFactory nancyContextFactory = new PactAwareContextFactory(
mockMockContextService,
mockCultureService,
mockRequestTraceFactory,
mockTextResource);
var context = nancyContextFactory.Create(request);
Assert.Equal(requestResponsePairs, context.Items["PactMockInteractions"]);
mockMockContextService.Received(1).GetExpectedRequestResponsePairs();
}
示例7: CompressResponse
private Response CompressResponse(Request request, Response response)
{
if (!response.ContentType.Contains("image")
&& request.Headers.AcceptEncoding.Any(x => x.Contains("gzip"))
&& (!response.Headers.ContainsKey("Content-Encoding") || response.Headers["Content-Encoding"] != "gzip"))
{
var data = new MemoryStream();
response.Contents.Invoke(data);
data.Position = 0;
if (data.Length < 1024)
{
response.Contents = stream =>
{
data.CopyTo(stream);
stream.Flush();
};
}
else
{
response.Headers["Content-Encoding"] = "gzip";
response.Contents = s =>
{
var gzip = new GZipStream(s, CompressionMode.Compress, true);
data.CopyTo(gzip);
gzip.Close();
};
}
}
return response;
}
示例8: ExtractTokenFromHeader
private static string ExtractTokenFromHeader(Request request)
{
var authorization =
request.Headers.Authorization;
if (string.IsNullOrEmpty(authorization))
{
return null;
}
if (!authorization.StartsWith(Scheme))
{
return null;
}
try
{
var encodedToken = authorization.Substring(Scheme.Length).Trim();
return String.IsNullOrWhiteSpace(encodedToken) ? null : encodedToken;
}
catch (FormatException)
{
return null;
}
}
示例9: Nachricht_aus_Request_extrahieren
private string Nachricht_aus_Request_extrahieren(Request request)
{
using (var sr = new StreamReader(Request.Body))
{
return sr.ReadToEnd();
}
}
示例10: Convert
public ProviderServiceRequest Convert(Request from)
{
if (from == null)
{
return null;
}
var httpVerb = _httpVerbMapper.Convert(from.Method.ToUpper());
var to = new ProviderServiceRequest
{
Method = httpVerb,
Path = from.Path,
Query = !String.IsNullOrEmpty(from.Url.Query) ? from.Url.Query.TrimStart('?') : null
};
if (from.Headers != null && from.Headers.Any())
{
var fromHeaders = from.Headers.ToDictionary(x => x.Key, x => String.Join(", ", x.Value));
to.Headers = fromHeaders;
}
if (from.Body != null && from.Body.Length > 0)
{
var content = ConvertStreamToString(from.Body);
var httpBodyContent = _httpBodyContentMapper.Convert(content, to.Headers);
to.Body = httpBodyContent.Body;
}
return to;
}
示例11: Rulings
public static Ruling[] Rulings(Request request)
{
var dictionary = request.Form as IDictionary<string, object>;
string releasedAt = "{0}.ReleasedAt";
string rule = "{0}.Rule";
string[] keys = dictionary.Select(x => x.Key)
.Where(x => x.StartsWith("Ruling"))
.ToArray();
keys = keys.Select(x => x.Substring(0,x.IndexOf('.'))).Distinct().ToArray();
List<Ruling> rulings = new List<Ruling>();
if(keys != null && keys.Length > 0)
{
keys = keys.OrderBy(x => x).ToArray();
foreach(string key in keys)
{
rulings.Add(new Ruling(){
ReleasedAt = DateTime.Parse(dictionary[string.Format(releasedAt, key)].ToString()),
Rule = dictionary[string.Format(rule, key)].ToString()
});
}
}
return rulings.ToArray();
}
示例12: Should_preserve_the_content_of_the_file_even_though_there_is_data_at_the_end_of_the_multipart
public void Should_preserve_the_content_of_the_file_even_though_there_is_data_at_the_end_of_the_multipart()
{
// Given
var expected = "wazaa";
var stream = new MemoryStream(BuildMultipartFileValues(new Dictionary<string, Tuple<string, string, string>>
{
{ "sample.txt", new Tuple<string, string, string>("content/type", expected, "name")}
}, null, "epilogue"));
var headers = new Dictionary<string, IEnumerable<string>>
{
{ "content-type", new[] { "multipart/form-data; boundary=----NancyFormBoundary" } }
};
// When
var request = new Request("POST", new Url { Path = "/" }, CreateRequestStream(stream), headers);
// Then
var fileValue = request.Files.Single().Value;
var actualBytes = new byte[fileValue.Length];
fileValue.Read(actualBytes, 0, (int)fileValue.Length);
var actual = Encoding.ASCII.GetString(actualBytes);
actual.ShouldEqual(expected);
}
示例13: Get
public string Get(Request request)
{
using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(request.Url.ToString()));
return Convert.ToBase64String(hash);
}
}
示例14: IsSessionHijacked
public bool IsSessionHijacked(Request request)
{
if (!_sessionDetector.IsInSession(request)) return false;
// ToDo: use real cookie name
var secureCookie = _cookieReader.Read(request, "_nsid");
return (secureCookie == null || !secureCookie.IsSecured || secureCookie.Hash != _hashGenerator.GenerateHash(request));
}
示例15: HandleRequest
/// <summary>
/// Handles an incoming <see cref="Request"/> async.
/// </summary>
/// <param name="nancyEngine">The <see cref="INancyEngine"/> instance.</param>
/// <param name="request">An <see cref="Request"/> instance, containing the information about the current request.</param>
/// <param name="onComplete">Delegate to call when the request is complete</param>
/// <param name="onError">Deletate to call when any errors occur</param>
public static void HandleRequest(
this INancyEngine nancyEngine,
Request request,
Action<NancyContext> onComplete,
Action<Exception> onError)
{
HandleRequest(nancyEngine, request, null, onComplete, onError, CancellationToken.None);
}