本文整理汇总了C#中IHttpServer.Stub方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpServer.Stub方法的具体用法?C# IHttpServer.Stub怎么用?C# IHttpServer.Stub使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpServer
的用法示例。
在下文中一共展示了IHttpServer.Stub方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestFixtureSetUp
public void TestFixtureSetUp() {
m_jwksServer = HttpMockFactory.Create( out m_host );
m_jwksServer.Stub(
x => x.Get( GOOD_PATH_WITH_JWKS )
).Return( GOOD_JSON ).OK();
m_jwksServer.Stub(
x => x.Get( BAD_PATH )
).Return( GOOD_JSON ).WithStatus( HttpStatusCode.InternalServerError );
}
示例2: Should_return_expected_ok_response
public void Should_return_expected_ok_response()
{
_stubHttp = HttpMockRepository.At(_hostUrl);
_stubHttp
.Stub(x => x.Get("/api2/status"))
.Return("Hello")
.OK();
_stubHttp
.Stub(x => x.Get("/api2/echo"))
.Return("Echo")
.NotFound();
_stubHttp
.Stub(x => x.Get("/api2/echo2"))
.Return("Nothing")
.WithStatus(HttpStatusCode.Unauthorized);
Console.WriteLine(_stubHttp.WhatDoIHave());
var wc = new WebClient();
Assert.That(wc.DownloadString(string.Format("{0}/api2/status", _hostUrl)), Is.EqualTo("Hello"));
try
{
Console.WriteLine(wc.DownloadString(_hostUrl + "/api2/echo"));
}
catch (Exception ex)
{
Assert.That(ex, Is.InstanceOf(typeof (WebException)));
Assert.That(((WebException) ex).Status, Is.EqualTo(WebExceptionStatus.ProtocolError));
}
try
{
wc.DownloadString(_hostUrl + "/api2/echo2");
}
catch (Exception ex)
{
Assert.That(ex, Is.InstanceOf(typeof (WebException)));
Assert.That(((WebException) ex).Status, Is.EqualTo(WebExceptionStatus.ProtocolError));
}
}
示例3: Index_WhenHttpIsStubbed_ReturnStubbedValue
public void Index_WhenHttpIsStubbed_ReturnStubbedValue()
{
stubHttp = HttpMockRepository.At("http://localhost:9009");
stubHttp.Stub(x => x.Get("/Stub")).Return("I AM A STUB").OK();
var page = Host.Instance.NavigateToInitialPage<Page>("/");
var actual = page.Find.Element(By.CssSelector("*")).Text;
Assert.IsTrue(actual.Contains("I AM A STUB"));
}
示例4: Should_hit_the_same_url_multiple_times
public void Should_hit_the_same_url_multiple_times()
{
string endpoint = _hostUrl;
_stubHttp = HttpMockRepository.At(endpoint);
_stubHttp
.Stub(x => x.Get("/api2/echo"))
.Return("Echo")
.NotFound();
_stubHttp
.Stub(x => x.Get("/api2/echo2"))
.Return("Nothing")
.WithStatus(HttpStatusCode.Unauthorized);
for (int count = 0; count < 6; count++)
{
RequestEcho(endpoint);
}
}
示例5: Should_hit_the_same_url_multiple_times
public void Should_hit_the_same_url_multiple_times()
{
string endpoint = "Http://localhost:9191";
_stubHttp = HttpMockRepository.At(endpoint);
_stubHttp
.Stub(x => x.Get("/api2/echo"))
.Return("Echo")
.NotFound();
_stubHttp
.Stub(x => x.Get("/api2/echo2"))
.Return("Nothing")
.WithStatus(HttpStatusCode.Unauthorized);
Console.WriteLine(_stubHttp.WhatDoIHave());
for (int count = 0; count < 6; count++) {
RequestEcho(endpoint);
}
}
示例6: HttpPackageFixture
public HttpPackageFixture()
{
server = HttpMockRepository.At(new Uri(Prefix));
var path = Path.Combine(Assembly.GetExecutingAssembly().Directory(), "testdata", "test-0.5.zip");
server.Stub(x => x.Get("/realpkg")).AddHeader("Content-Disposition", "attachment; filename=test-0.5.zip").ReturnFile(path).OK();
server.Stub(x => x.Get("/realpkgnotype")).ReturnFile(path).OK();
server.Stub(x => x.Get("/test-0.5.zip")).ReturnFile(path).OK();
server.Stub(x => x.Head("/realpkg")).Return("").OK();
server.Stub(x => x.Get("/redirectedpkg"))
.Return("")
.AddHeader("Location", $"{Prefix}/realpkg")
.WithStatus(HttpStatusCode.Moved);
server.Stub(x => x.Head("/redirectedpkg"))
.Return("")
.AddHeader("Location", $"{Prefix}/realpkg")
.WithStatus(HttpStatusCode.Moved);
server.Start();
}
示例7: Should_start_listening_before_stubs_have_been_set
public void Should_start_listening_before_stubs_have_been_set()
{
_stubHttp = HttpMockRepository.At(_hostUrl);
_stubHttp.Stub(x => x.Get("/endpoint"))
.Return("listening")
.OK();
using (var tcpClient = new TcpClient())
{
var uri = new Uri(_hostUrl);
tcpClient.Connect(uri.Host, uri.Port);
Assert.That(tcpClient.Connected, Is.True);
tcpClient.Close();
}
string result = new WebClient().DownloadString(string.Format("{0}/endpoint", _hostUrl));
Assert.That(result, Is.EqualTo("listening"));
}
示例8: SUT_should_return_stubbed_response_for_custom_verbs
public void SUT_should_return_stubbed_response_for_custom_verbs()
{
_stubHttp = HttpMockRepository.At(_hostUrl);
const string expected = "<xml><>response>Hello World</response></xml>";
_stubHttp.Stub(x => x.CustomVerb("/endpoint", "PURGE"))
.Return(expected)
.OK();
var request = (HttpWebRequest) WebRequest.Create(string.Format("{0}/endpoint", _hostUrl));
request.Method = "PURGE";
request.Host = "nonstandard.host";
request.Headers.Add("X-Go-Faster", "11");
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
var responseBody = new StreamReader(stream).ReadToEnd();
Assert.That(responseBody, Is.EqualTo(expected));
}
}
示例9: SUT_should_return_stubbed_response
public void SUT_should_return_stubbed_response()
{
_stubHttp = HttpMockRepository.At(_hostUrl);
const string expected = "<xml><>response>Hello World</response></xml>";
_stubHttp.Stub(x => x.Get("/endpoint"))
.Return(expected)
.OK();
string result = new WebClient().DownloadString(string.Format("{0}/endpoint", _hostUrl));
Assert.That(result, Is.EqualTo(expected));
}
示例10: SUT_should_get_back_exact_content_in_the_last_request_body
public void SUT_should_get_back_exact_content_in_the_last_request_body(int count)
{
_stubHttp = HttpMockRepository.At(_hostUrl);
string expected = string.Format("<xml><>response>{0}</response></xml>", string.Join(" ", Enumerable.Range(0, count)));
var requestHandler = _stubHttp.Stub(x => x.Post("/endpoint"));
requestHandler.Return(expected).OK();
using (var wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/xml";
wc.UploadString(string.Format("{0}/endpoint", _hostUrl), expected);
}
var requestBody = ((RequestHandler)requestHandler).LastRequest().Body;
Assert.That(requestBody, Is.EqualTo(expected));
}
示例11: Should_support_range_requests
public void Should_support_range_requests()
{
_stubHttp = HttpMockRepository.At(_hostUrl);
string query = "/path/file";
int fileSize = 2048;
string pathToFile = CreateFile(fileSize);
try
{
_stubHttp.Stub(x => x.Get(query))
.ReturnFileRange(pathToFile, 0, 1023)
.WithStatus(HttpStatusCode.PartialContent);
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(_hostUrl + query);
request.Method = "GET";
request.AddRange(0, 1023);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
byte[] downloadData = new byte[response.ContentLength];
using (response)
{
response.GetResponseStream().Read(downloadData, 0, downloadData.Length);
}
Assert.That(downloadData.Length, Is.EqualTo(1024));
}
finally
{
try
{
File.Delete(pathToFile);
}
catch
{
}
}
}
示例12: Should_start_listening_before_stubs_have_been_set
public void Should_start_listening_before_stubs_have_been_set()
{
_stubHttp = HttpMockRepository.At("http://localhost:9191");
_stubHttp.Stub(x => x.Get("/endpoint"))
.Return("listening")
.OK();
using (var tcpClient = new TcpClient()) {
tcpClient.Connect(new Uri("Http://localhost:9191/").Host, new Uri("Http://localhost:9191/").Port);
Assert.That(tcpClient.Connected, Is.True);
tcpClient.Close();
}
string result = new WebClient().DownloadString("http://localhost:9191/endpoint");
Console.WriteLine("RESPONSE: {0}", result);
Assert.That(result, Is.EqualTo("listening"));
}
示例13: SUT_should_return_stubbed_response
public void SUT_should_return_stubbed_response()
{
_stubHttp = HttpMockRepository.At("http://localhost:9191");
const string expected = "<xml><>response>Hello World</response></xml>";
_stubHttp.Stub(x => x.Get("/endpoint"))
.Return(expected)
.OK();
Console.WriteLine(_stubHttp.WhatDoIHave());
string result = new WebClient().DownloadString("http://localhost:9191/endpoint");
Console.WriteLine("RESPONSE: {0}", result);
Assert.That(result, Is.EqualTo(expected));
}
示例14: Should_support_range_requests
public void Should_support_range_requests()
{
string host = "http://localhost:9191";
_stubHttp = HttpMockRepository.At(host);
string query = "/path/file";
int fileSize = 2048;
string pathToFile = CreateFile(fileSize);
try
{
_stubHttp.Stub(x => x.Get(query))
.ReturnFileRange(pathToFile, 0, 1023)
.WithStatus(HttpStatusCode.PartialContent);
Console.WriteLine(_stubHttp.WhatDoIHave());
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(host + query);
request.Method = "GET";
request.AddRange(0, 1023);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
byte[] downloadData = new byte[response.ContentLength];
using (response)
{
response.GetResponseStream().Read(downloadData, 0, downloadData.Length);
}
Assert.That(downloadData.Length, Is.EqualTo(1024));
} finally {
try
{
File.Delete(pathToFile);
} catch (Exception ex) //IOException (or other) can happen
{
Console.WriteLine("WARNING: TearDown of test couldn't be complete: " + ex);
}
}
}