本文整理汇总了C#中HttpServer.GET方法的典型用法代码示例。如果您正苦于以下问题:C# HttpServer.GET方法的具体用法?C# HttpServer.GET怎么用?C# HttpServer.GET使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpServer
的用法示例。
在下文中一共展示了HttpServer.GET方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExampleCode
public void ExampleCode()
{
using (var server = new HttpServer("http://*:1234/"))
{
// simple basic usage, all subscriptions will run in a single event-loop
server.GET("/hello/{Name}")
.Subscribe(ctx => ctx.Respond("Hello, " + ctx.Request.UriArguments.Name + "!"));
Browser.ExecuteGet("http://localhost:1234/hello/George")
.ReadAllContent()
.Should().Be.EqualTo("Hello, George!");
// use Rx LINQ operators
server.POST("/hi/{Name}")
.Where(ctx => ctx.Request.UriArguments.Name == "George")
.Subscribe(ctx => ctx.Respond("Hi, George!"));
server.POST("/hi/{Name}")
.Where(ctx => ctx.Request.UriArguments.Name == "Pete")
.Subscribe(ctx => ctx.Respond("Hi, Pete!"));
Browser.ExecutePost("http://localhost:1234/hi/George")
.ReadAllContent()
.Should().Be.EqualTo("Hi, George!");
Browser.ExecutePost("http://localhost:1234/hi/Pete")
.ReadAllContent()
.Should().Be.EqualTo("Hi, Pete!");
// This becomes a problem:
//Browser.ExecutePost("http://localhost:1234/hi/Fran").StatusCode.Should().Be.EqualTo(404);
}
}
示例2: CanHandleQueryStringArguments
public void CanHandleQueryStringArguments()
{
using (var server = new HttpServer("http://*:1234/"))
{
server.GET("customers")
.Subscribe(ctx => ctx.Respond(string.Format("customers where name equals to {0}", ctx.Request.QueryString.Name)));
Browser.ExecuteGet("http://localhost:1234/customers?name=jose")
.ReadAllContent().Should().Be.EqualTo("customers where name equals to jose");
}
}
示例3: CanHandleUriArguments
public void CanHandleUriArguments()
{
using (var server = new HttpServer("http://*:1234/"))
{
server.GET("customer/{name}")
.Subscribe(ctx => ctx.Respond(string.Format("hello {0}", ctx.Request.UriArguments.name)));
Browser.ExecuteGet("http://localhost:1234/customer/peter")
.ReadAllContent()
.Should().Be.EqualTo("hello peter");
}
}
示例4: CanReturnAStringFromWildcardURL
public void CanReturnAStringFromWildcardURL()
{
using (var server = new HttpServer("http://*:1234/"))
{
server.GET("/hello/{Name}")
.Subscribe(ctx => ctx.Respond("hello, " + ctx.Request.UriArguments.Name + "!"));
Browser.ExecuteGet("http://localhost:1234/hello/George")
.ReadAllContent()
.Should().Be.EqualTo("hello, George!");
}
}
示例5: CanReturnAnString
public void CanReturnAnString()
{
using (var server = new HttpServer("http://*:1234/"))
{
server.GET("/")
.Subscribe(ctx => ctx.Respond("hello world"));
Browser.ExecuteGet("http://localhost:1234")
.ReadAllContent()
.Should().Be.EqualTo("hello world");
}
}
示例6: CanReturnArbitraryHeader
public void CanReturnArbitraryHeader()
{
using (var server = new HttpServer("http://*:1234/"))
{
var headers = new Dictionary<string, string> { { "X-Something", "value" }, { "X-Another", "value again" } };
server.GET("/test")
.Subscribe(ctx => ctx.Respond("I have some headers for you", 200, headers));
HttpWebResponse response = Browser.ExecuteGet("http://localhost:1234/test");
Dictionary<string, string> contentHeaders = response.Headers.ToDictionary().Select(pair => new KeyValuePair<string, string>(pair.Key, pair.Value.FirstOrDefault())).ToDictionary(pair => pair.Key, pair => pair.Value);
foreach (var header in headers)
contentHeaders.Should().Contain(header);
}
}
示例7: WhenRequestingAnUnhandledRoute_ThenReturn404
public void WhenRequestingAnUnhandledRoute_ThenReturn404()
{
using (var server = new HttpServer("http://*:1234/"))
{
server.GET("customer/{name}")
.Subscribe(ctx => ctx.Respond(string.Format("hello {0}", ctx.Request.UriArguments.name)));
Executing.This(() => Browser.ExecuteGet("http://localhost:1234/customersssss/peter"))
.Should().Throw<WebException>()
.And.Exception.Response.OfType<HttpWebResponse>().StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound);
}
}
示例8: CanReturnBinaryData
public void CanReturnBinaryData()
{
using (var server = new HttpServer("http://*:1234/"))
{
var expectedResponse = new byte[] { 0, 1, 2, 3, 4};
server.GET("/")
.Subscribe(ctx => ctx.Response(expectedResponse).Send());
var response = Browser.ExecuteGet("http://localhost:1234");
byte[] data = new BinaryReader(response.GetResponseStream()).ReadBytes(1337);
data.Length.Should().Be.EqualTo(expectedResponse.Length);
for (int i = 0; i != data.Length; i++) data[i].Should().Be.EqualTo(expectedResponse[i]);
}
}
示例9: CanReturnAStaticFile
public void CanReturnAStaticFile()
{
using (var server = new HttpServer("http://*:1234/"))
{
server.GET("/")
.Subscribe(ctx => ctx.StaticFileResponse(@"samples\example_1.txt").Send());
Browser.ExecuteGet("http://localhost:1234")
.ReadAllContent()
.Should().Contain(string.Join(Environment.NewLine, Enumerable.Range(1, 9)));
}
}