本文整理匯總了C#中System.Net.Http.HttpRequestMessage.SetIsLocal方法的典型用法代碼示例。如果您正苦於以下問題:C# HttpRequestMessage.SetIsLocal方法的具體用法?C# HttpRequestMessage.SetIsLocal怎麽用?C# HttpRequestMessage.SetIsLocal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.SetIsLocal方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ShouldOutputAuthEndpoint
public void ShouldOutputAuthEndpoint()
{
Mock<IVirtualPathUtility> virtualPathMock = new Mock<IVirtualPathUtility>();
virtualPathMock.Setup(vp => vp.ToAbsolute(It.IsAny<string>())).Returns("/Auth/Login.ashx");
Mock<IApplicationSettings> appSettingsMock = new Mock<IApplicationSettings>();
ApiFrontPageController controller = new ApiFrontPageController(virtualPathMock.Object, appSettingsMock.Object);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com/api");
requestMessage.Properties[HttpPropertyKeys.HttpConfigurationKey] = new HttpConfiguration();
requestMessage.SetIsLocal(true);
controller.Request = requestMessage;
var responseData = controller.GetFrontPage().Content as ObjectContent;
Assert.Equal("http://example.com/Auth/Login.ashx", ((ApiFrontpageModel)responseData.Value).Auth.AuthUri);
}
示例2: ShouldNotEncodeBracesInMessagesUri
public void ShouldNotEncodeBracesInMessagesUri()
{
Mock<IVirtualPathUtility> virtualPathMock = new Mock<IVirtualPathUtility>();
virtualPathMock.Setup(vp => vp.ToAbsolute(It.IsAny<string>())).Returns<string>(s=>s);
Mock<IApplicationSettings> appSettingsMock = new Mock<IApplicationSettings>();
ApiFrontPageController controller = new ApiFrontPageController(virtualPathMock.Object, appSettingsMock.Object);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://example.com:1067/api");
requestMessage.Properties[HttpPropertyKeys.HttpConfigurationKey] = new HttpConfiguration();
requestMessage.SetIsLocal(false);
controller.Request = requestMessage;
var responseData = controller.GetFrontPage().Content as ObjectContent;
Assert.Equal("http://example.com/api/v1/messages/{room}/{format}", ((ApiFrontpageModel)responseData.Value).MessagesUri);
}
示例3: IsLocal_Call_To_HttpRequestMessageExtension_Method
public void IsLocal_Call_To_HttpRequestMessageExtension_Method()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
request.SetIsLocal(new Lazy<bool>(() => true));
HttpRequestMessageWrapper wrapper = new HttpRequestMessageWrapper("/", request);
// Act
bool isLocal = wrapper.IsLocal;
// Assert
Assert.True(isLocal);
}
示例4: WriteStreamedErrorResponseAsync_Aborts_When_Formatter_Write_Faults
public void WriteStreamedErrorResponseAsync_Aborts_When_Formatter_Write_Faults()
{
// Arrange
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
tcs.TrySetException(new NotSupportedException("Expected error"));
Mock<JsonMediaTypeFormatter> formatterMock = new Mock<JsonMediaTypeFormatter>() { CallBase = true };
formatterMock.Setup(m => m.WriteToStreamAsync(It.IsAny<Type>(),
It.IsAny<object>(),
It.IsAny<Stream>(),
It.IsAny<HttpContent>(),
It.IsAny<TransportContext>())).Returns(tcs.Task);
MemoryStream memoryStream = new MemoryStream();
Mock<HttpRequestBase> requestBaseMock = new Mock<HttpRequestBase>();
requestBaseMock.Setup(m => m.Abort()).Verifiable();
HttpRequestBase requestBase = requestBaseMock.Object;
HttpResponseBase responseBase = CreateMockHttpResponseBaseForResponse(memoryStream).Object;
HttpContextBase contextBase = CreateStubContext(requestBase, responseBase);
HttpRequestMessage request = new HttpRequestMessage();
request.SetIsLocal(new Lazy<bool>(() => true));
HttpResponseMessage response = new HttpResponseMessage() { RequestMessage = request };
response.Content = new ObjectContent<string>("hello", formatterMock.Object);
// Act
Task task = HttpControllerHandler.WriteStreamedResponseContentAsync(contextBase, response.Content);
task.Wait();
// Assert
requestBaseMock.Verify();
}
示例5: NotSupportedException
public void ConvertResponse_Returns_InternalServerError_And_No_Content_When_No_Content_Negotiator_For_Error_Response()
{
// Arrange
Mock<JsonMediaTypeFormatter> formatterMock = new Mock<JsonMediaTypeFormatter>() { CallBase = true };
// This formatter throws on any write attempt
formatterMock.Setup(m => m.WriteToStreamAsync(It.IsAny<Type>(),
It.IsAny<object>(),
It.IsAny<Stream>(),
It.IsAny<HttpContent>(),
It.IsAny<TransportContext>())).Throws(new NotSupportedException("Expected error"));
// Create a local config to hook to the request to condition
// the formatter selection for the error response
HttpConfiguration config = new HttpConfiguration();
config.Formatters.Clear();
config.Formatters.Add(formatterMock.Object);
config.Services.Replace(typeof(IContentNegotiator), null /*negotiatorMock.Object*/);
MemoryStream memoryStream = new MemoryStream();
Mock<HttpContextBase> contextMock = CreateMockHttpContextBaseForResponse(memoryStream);
HttpResponseBase responseBase = contextMock.Object.Response;
HttpRequestMessage request = new HttpRequestMessage();
request.SetIsLocal(new Lazy<bool>(() => true));
request.SetConfiguration(config);
HttpResponseMessage response = new HttpResponseMessage() { RequestMessage = request };
response.Content = new ObjectContent<string>("hello", formatterMock.Object);
// Act
Task task = HttpControllerHandler.ConvertResponse(contextMock.Object, response, request);
task.Wait();
// Assert
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
Assert.Equal(0, memoryStream.Length);
Assert.Null(responseBase.Headers["Content-Type"]);
}
示例6: ConvertResponse_Returns_User_Response_When_Formatter_Write_Throws_HttpResponseException_With_Content
public void ConvertResponse_Returns_User_Response_When_Formatter_Write_Throws_HttpResponseException_With_Content()
{
// Arrange
HttpResponseMessage errorResponse = new HttpResponseMessage(HttpStatusCode.MethodNotAllowed);
errorResponse.Headers.Add("myHeader", "myValue");
errorResponse.Content = new StringContent("user message", Encoding.UTF8, "application/fake");
Mock<JsonMediaTypeFormatter> formatterMock = new Mock<JsonMediaTypeFormatter>() { CallBase = true };
formatterMock.Setup(m => m.WriteToStreamAsync(It.IsAny<Type>(),
It.IsAny<object>(),
It.IsAny<Stream>(),
It.IsAny<HttpContent>(),
It.IsAny<TransportContext>())).Throws(new HttpResponseException(errorResponse));
MemoryStream memoryStream = new MemoryStream();
Mock<HttpContextBase> contextMock = CreateMockHttpContextBaseForResponse(memoryStream);
HttpResponseBase responseBase = contextMock.Object.Response;
HttpRequestMessage request = new HttpRequestMessage();
request.SetIsLocal(new Lazy<bool>(() => true));
HttpResponseMessage response = new HttpResponseMessage() { RequestMessage = request };
response.Content = new ObjectContent<string>("hello", formatterMock.Object);
// Act
Task task = HttpControllerHandler.ConvertResponse(contextMock.Object, response, request);
task.Wait();
// Assert preparation -- deserialize the response
memoryStream.Seek(0L, SeekOrigin.Begin);
string responseContent = null;
using (var streamReader = new StreamReader(memoryStream))
{
responseContent = streamReader.ReadToEnd();
}
// Assert
Assert.Equal<int>((int)errorResponse.StatusCode, responseBase.StatusCode);
Assert.True(responseBase.Headers["Content-Type"].StartsWith("application/fake"));
Assert.Equal("user message", responseContent);
Assert.Equal("myValue", responseBase.Headers["myHeader"]);
}
示例7: HttpResponseMessage
public void ConvertResponse_Returns_User_Response_When_Formatter_Write_Throws_HttpResponseException_With_No_Content()
{
// Arrange
HttpResponseMessage errorResponse = new HttpResponseMessage(HttpStatusCode.MethodNotAllowed);
errorResponse.Headers.Add("myHeader", "myValue");
Mock<JsonMediaTypeFormatter> formatterMock = new Mock<JsonMediaTypeFormatter>() { CallBase = true };
formatterMock.Setup(m => m.WriteToStreamAsync(It.IsAny<Type>(),
It.IsAny<object>(),
It.IsAny<Stream>(),
It.IsAny<HttpContent>(),
It.IsAny<TransportContext>())).Throws(new HttpResponseException(errorResponse));
MemoryStream memoryStream = new MemoryStream();
Mock<HttpContextBase> contextMock = CreateMockHttpContextBaseForResponse(memoryStream);
HttpResponseBase responseBase = contextMock.Object.Response;
HttpRequestMessage request = new HttpRequestMessage();
request.SetIsLocal(new Lazy<bool>(() => true));
HttpResponseMessage response = new HttpResponseMessage() { RequestMessage = request };
response.Content = new ObjectContent<string>("hello", formatterMock.Object);
// Act
Task task = HttpControllerHandler.ConvertResponse(contextMock.Object, response, request);
task.Wait();
memoryStream.Seek(0L, SeekOrigin.Begin);
// Assert
Assert.Equal<int>((int)errorResponse.StatusCode, responseBase.StatusCode);
Assert.Equal(0, memoryStream.Length);
Assert.Equal("myValue", responseBase.Headers["myHeader"]);
Assert.Null(responseBase.Headers["Content-Type"]);
}
示例8: ConvertResponse_Returns_Error_Response_When_Formatter_Write_Throws_Immediately
public void ConvertResponse_Returns_Error_Response_When_Formatter_Write_Throws_Immediately()
{
// Arrange
Mock<JsonMediaTypeFormatter> formatterMock = new Mock<JsonMediaTypeFormatter>() { CallBase = true };
formatterMock.Setup(m => m.WriteToStreamAsync(It.IsAny<Type>(),
It.IsAny<object>(),
It.IsAny<Stream>(),
It.IsAny<HttpContent>(),
It.IsAny<TransportContext>())).Throws(new NotSupportedException("Expected error"));
MemoryStream memoryStream = new MemoryStream();
Mock<HttpContextBase> contextMock = CreateMockHttpContextBaseForResponse(memoryStream);
HttpResponseBase responseBase = contextMock.Object.Response;
HttpRequestMessage request = new HttpRequestMessage();
request.SetIsLocal(new Lazy<bool>(() => true));
HttpResponseMessage response = new HttpResponseMessage() { RequestMessage = request };
response.Content = new ObjectContent<string>("hello", formatterMock.Object);
// Act
Task task = HttpControllerHandler.ConvertResponse(contextMock.Object, response, request);
task.Wait();
// Assert preparation -- deserialize the HttpError response
HttpError httpError = null;
memoryStream.Seek(0L, SeekOrigin.Begin);
using (StreamContent content = new StreamContent(memoryStream))
{
content.Headers.ContentType = JsonMediaTypeFormatter.DefaultMediaType;
httpError = content.ReadAsAsync<HttpError>().Result;
}
// Assert
Assert.Equal<int>((int)HttpStatusCode.InternalServerError, responseBase.StatusCode);
Assert.True(responseBase.Headers["Content-Type"].StartsWith(JsonMediaTypeFormatter.DefaultMediaType.MediaType));
Assert.Equal("An error has occurred.", httpError["Message"]);
Assert.Equal("The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.", httpError["ExceptionMessage"]);
Assert.Equal(typeof(InvalidOperationException).FullName, httpError["ExceptionType"]);
Assert.True(httpError.ContainsKey("StackTrace"));
HttpError innerError = (httpError["InnerException"] as JObject).ToObject<HttpError>();
Assert.NotNull(innerError);
Assert.Equal(typeof(NotSupportedException).FullName, innerError["ExceptionType"].ToString());
Assert.Equal("Expected error", innerError["ExceptionMessage"]);
Assert.Contains("System.Net.Http.HttpContent.CopyToAsync", innerError["StackTrace"].ToString());
}