public void Can_respond_to_non_range_requests_with_200_OK_response() { var mockRequest = new HttpRequestMock(); var mockResponse = new HttpResponseMock(); string customText = "1234567890"; byte[] customTextBytes = customText.ToUtf8Bytes(); var ms = new MemoryStream(); ms.Write(customTextBytes, 0, customTextBytes.Length); var httpResult = new HttpResult(ms, "audio/mpeg"); bool reponseWasAutoHandled = mockResponse.WriteToResponse(mockRequest, httpResult); Assert.That(reponseWasAutoHandled, Is.True); string writtenString = mockResponse.GetOutputStreamAsString(); Assert.That(writtenString, Is.EqualTo(customText)); Assert.That(mockResponse.Headers["Content-Range"], Is.Null); Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes")); Assert.That(mockResponse.StatusCode, Is.EqualTo(200)); }
public void ShouldThrowWebException() { //Arrange var filePath = "apisConfig.json"; var config = new Config(); config.DefaultMethod = "Post"; var api = new ApiConfig(); config.Apis = new[] { api }; api.Url = "http://localhost/api/products/{idProduct}/something"; var responseCfg = new HttpResponseMock(); api.Responses = new[] { responseCfg }; responseCfg.Active = true; responseCfg.WebExceptionMessage = "WebException Message"; ConfigIO.WriteConfig(config, filePath); var requester = new FakeHttpRequester(filePath); var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost/api/products/2/something"); webRequest.Method = "POST"; //Act var exception = Assert.ThrowsException <WebException>(() => { requester.GetResponse(webRequest); }); //Assert Assert.AreEqual(responseCfg.WebExceptionMessage, exception.Message); }
public void Can_seek_from_middle_to_middle() { var mockRequest = new HttpRequestMock(); mockRequest.Headers.Add("Range", "bytes=3-5"); var mockResponse = new HttpResponseMock(); string customText = "1234567890"; byte[] customTextBytes = customText.ToUtf8Bytes(); var ms = new MemoryStream(); ms.Write(customTextBytes, 0, customTextBytes.Length); var httpResult = new HttpResult(ms, "audio/mpeg"); bool reponseWasAutoHandled = mockResponse.WriteToResponse(mockRequest, httpResult); Assert.That(reponseWasAutoHandled, Is.True); string writtenString = mockResponse.GetOutputStreamAsString(); Assert.That(writtenString, Is.EqualTo("456")); Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 3-5/10")); Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString())); Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes")); Assert.That(mockResponse.StatusCode, Is.EqualTo(206)); }
public void ShoulReturnDataFromFile() { //Arrange var filePath = "apisConfig.json"; var config = new Config(); config.DefaultMethod = "PUT"; var api = new ApiConfig(); config.Apis = new[] { api }; api.Url = "http://localhost/api/products/{idProduct}/something"; var responseCfg = new HttpResponseMock(); api.Responses = new[] { responseCfg }; responseCfg.Active = true; responseCfg.File = "Files/DownloadFile.txt"; ConfigIO.WriteConfig(config, filePath); var requester = new FakeHttpRequester(filePath); var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost/api/products/2/something"); webRequest.Method = "PUT"; //Act var webResponse = requester.GetResponse(webRequest); //Assert using (var streamReader = new StreamReader(webResponse.GetResponseStream())) { var content = streamReader.ReadToEnd(); Assert.AreEqual(File.ReadAllText("Files/DownloadFile.txt"), content); } }
public void Can_use_fileStream() { byte[] fileBytes = uploadedTextFile.ReadFully(); string fileText = Encoding.ASCII.GetString(fileBytes); "File content size {0}".Print(fileBytes.Length); "File content is {0}".Print(fileText); var mockRequest = new HttpRequestMock(); var mockResponse = new HttpResponseMock(); mockRequest.Headers.Add("Range", "bytes=6-8"); var httpResult = new HttpResult(uploadedTextFile, "audio/mpeg"); bool reponseWasAutoHandled = mockResponse.WriteToResponse(mockRequest, httpResult); Assert.That(reponseWasAutoHandled, Is.True); string writtenString = mockResponse.GetOutputStreamAsString(); Assert.That(writtenString, Is.EqualTo(fileText.Substring(6, 3))); Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 6-8/33")); Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString())); Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes")); Assert.That(mockResponse.StatusCode, Is.EqualTo(206)); }
public void Can_send_ResponseStream_test_with_Custom_Header() { var mockResponse = new HttpResponseMock(); var customText = "<h1>Custom Stream</h1>"; var customTextBytes = customText.ToUtf8Bytes(); var ms = new MemoryStream(); ms.Write(customTextBytes, 0, customTextBytes.Length); var httpResult = new HttpResult(ms, ContentType.Html) { Headers = { { "X-Custom", "Header" } } }; var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, ContentType.Html); Assert.That(reponseWasAutoHandled, Is.True); var writtenString = mockResponse.GetOutputStreamAsString(); Assert.That(writtenString, Is.EqualTo(customText)); Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header")); }
public void Can_seek_from_beginning_to_further_than_end() { // Not sure if this would ever occur in real streaming scenarios, but it does occur // when some crawlers use range headers to specify a max size to return. // e.g. Facebook crawler always sends range header of 'bytes=0-524287'. var mockRequest = new HttpRequestMock(); var mockResponse = new HttpResponseMock(); mockRequest.Headers[HttpHeaders.Range] = "bytes=0-524287"; string customText = "1234567890"; byte[] customTextBytes = customText.ToUtf8Bytes(); var ms = new MemoryStream(); ms.Write(customTextBytes, 0, customTextBytes.Length); var httpResult = new HttpResult(ms, "audio/mpeg"); bool reponseWasAutoHandled = mockResponse.WriteToResponse(mockRequest, httpResult); Assert.That(reponseWasAutoHandled, Is.True); string writtenString = mockResponse.GetOutputStreamAsString(); Assert.That(writtenString, Is.EqualTo(customText)); Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 0-9/10")); Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString())); Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes")); Assert.That(mockResponse.StatusCode, Is.EqualTo(206)); }
public void SetContentType_HttpResponseTestStruct_TextHtml() { var response = new HttpResponseMock(); response.SetContentType(TestStruct); var act = response.ContentType; var exp = TestStruct.ToString(); Assert.AreEqual(exp, act); }
public void SetUp() { authenticationConfig = AuthenticationConfig.Builder.RedirectUri("test://example.com").Build(); keycloakConfig = new KeycloakConfig(kcConfig); credentialManagerMock = new CredentialManagerMock(); IHttpResponse httpResponse = (HttpResponseMock)HttpResponseMock.newResponse().withStatusCode(200); var requestToBeExecuted = new HttpRequestToBeExecutedMock(Task.FromResult(httpResponse), null); IHttpRequest httpRequestMock = (HttpRequestMock)HttpRequestMock.newRequest().withGetResult(requestToBeExecuted); httpModuleMock = (HttpServiceModuleMock)HttpServiceModuleMock.newMock().withNewRequest(httpRequestMock); authenticatorToTest = new OIDCAuthenticator(authenticationConfig, keycloakConfig, credentialManagerMock, httpModuleMock, logger); }
public void GetContentType_HttpResponse_TextHtml() { var response = new HttpResponseMock() { ContentType = "text/html" }; var act = response.GetContentType(); var exp = TextHtml; Assert.AreEqual(exp, act); }
public void Can_handle_null_HttpResult_StatusDescription() { var mockResponse = new HttpResponseMock(); var httpResult = new HttpResult(); httpResult.StatusDescription = null; mockResponse.WriteToResponse(httpResult, ContentType.Html); Assert.IsNotNull(mockResponse.StatusDescription); }
public void GetNextFile_ShouldThrowInvalidExceptionWhenFilesIsEmpty() { //Arrange var response = new HttpResponseMock(); response.Files = new List <string>(); //Act var ex = Assert.ThrowsException <InvalidOperationException>(() => { response.GetNextFile(); }); }
public void WriteResponse() { var url = "test_url"; var result = new RedirectResult(url); var responseMock = new HttpResponseMock(null); result.WriteResponse(responseMock); Assert.Equals(responseMock.lastRedirect, url); Assert.IsTrue(!responseMock.lastRedirectIsPermanent); result = new RedirectResult(url, true); responseMock = new HttpResponseMock(null); result.WriteResponse(responseMock); Assert.Equals(responseMock.lastRedirect, url); Assert.IsTrue(responseMock.lastRedirectIsPermanent); }
public void CreateResponseStream_ShouldThrowExceptionWhenFileNotExists() { //Arrange var responseMock = new HttpResponseMock(); responseMock.File = "FakeFile"; //Act var ex = Assert.ThrowsException <FileLoadException>(() => { FakeHttpRequester.CreateResponseStream(null, responseMock); }); //Assert Assert.AreEqual("File FakeFile not exists", ex.Message); }
public void Can_send_ResponseText_test_with_StatusDescription() { var mockResponse = new HttpResponseMock(); var customStatus = "Custom Status Description"; var httpResult = new HttpResult(System.Net.HttpStatusCode.Accepted, customStatus); var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, ContentType.Html); Assert.That(reponseWasAutoHandled, Is.True); var statusDesc = mockResponse.StatusDescription; Assert.That(mockResponse.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.Accepted)); Assert.That(statusDesc, Is.EqualTo(customStatus)); }
public void Test_response_with_CompressedResult() { EndpointHost.Config = new EndpointHostConfig( "ServiceName", new ServiceManager(GetType().Assembly)); var assembly = typeof(CompressionTests).Assembly; EndpointHost.ConfigureHost( new TestAppHost(new Container(), assembly), "Name", new ServiceManager(assembly)); var mockResponse = new HttpResponseMock(); var simpleDto = new TestCompress(1, "name"); var simpleDtoXml = DataContractSerializer.Instance.Parse(simpleDto); const string expectedXml = "<TestCompress xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.ddnglobal.com/types/\"><Id>1</Id><Name>name</Name></TestCompress>"; Assert.That(simpleDtoXml, Is.EqualTo(expectedXml)); var simpleDtoZip = simpleDtoXml.Deflate(); Assert.That(simpleDtoZip.Length, Is.GreaterThan(0)); var compressedResult = new CompressedResult(simpleDtoZip); var reponseWasAutoHandled = mockResponse.WriteToResponse( compressedResult, CompressionTypes.Deflate); Assert.That(reponseWasAutoHandled, Is.True); //var bytesToWriteToResponseStream = new byte[simpleDtoZip.Length - 4]; //Array.Copy(simpleDtoZip, CompressedResult.Adler32ChecksumLength, bytesToWriteToResponseStream, 0, bytesToWriteToResponseStream.Length); var bytesToWriteToResponseStream = simpleDtoZip; var writtenBytes = mockResponse.GetOutputStreamAsBytes(); Assert.That(writtenBytes, Is.EqualTo(bytesToWriteToResponseStream)); Assert.That(mockResponse.ContentType, Is.EqualTo(MimeTypes.Xml)); Assert.That(mockResponse.Headers[HttpHeaders.ContentEncoding], Is.EqualTo(CompressionTypes.Deflate)); Log.Debug("Content-length: " + writtenBytes.Length); Log.Debug(BitConverter.ToString(writtenBytes)); }
public void GetNextFile_ShouldReturnNextFileAndReset() { //Arrange var response = new HttpResponseMock(); response.Files = new List <string> { "1", "2", "3" }; //Act/Assert Assert.AreEqual("1", response.GetNextFile()); Assert.AreEqual("2", response.GetNextFile()); Assert.AreEqual("3", response.GetNextFile()); Assert.AreEqual("1", response.GetNextFile()); }
public void Test_response_with_html_result() { var mockResponse = new HttpResponseMock(); const string url = "http://www.servicestack.net"; var htmlResult = Html.RedirectTo(url); var reponseWasAutoHandled = mockResponse.WriteToResponse(htmlResult, "text/xml"); Assert.That(reponseWasAutoHandled, Is.True); var expectedOutput = string.Format( "<html><head><meta http-equiv=\"refresh\" content=\"0;url={0}\"></head></html>", url); var writtenString = mockResponse.GetOutputStreamAsString(); Assert.That(writtenString, Is.EqualTo(expectedOutput)); Assert.That(mockResponse.Headers["Location"], Is.EqualTo(url)); }
public void Can_send_ResponseText_test_with_Custom_Header() { var mockResponse = new HttpResponseMock(); var customText = "<h1>Custom Text</h1>"; var httpResult = new HttpResult(customText, ContentType.Html) { Headers = { { "X-Custom", "Header" } } }; var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, ContentType.Html); Assert.That(reponseWasAutoHandled, Is.True); var writtenString = mockResponse.GetOutputStreamAsString(); Assert.That(writtenString, Is.EqualTo(customText)); Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header")); }
private void ThrowIfError <TResponse>(HttpResponseMock httpRes) { if (httpRes.StatusCode >= 400) { var webEx = new WebServiceException("WebServiceException, StatusCode: " + httpRes.StatusCode) { StatusCode = httpRes.StatusCode, StatusDescription = httpRes.StatusDescription, }; try { var deserializer = EndpointHost.AppHost.ContentTypeFilters.GetStreamDeserializer(httpReq.ResponseContentType); webEx.ResponseDto = deserializer(typeof(TResponse), httpRes.OutputStream); } catch (Exception ex) { Console.WriteLine(ex); } throw webEx; } }
public void ShouldThrowCustomException() { //Arrange var filePath = "apisConfig.json"; var config = new Config(); config.DefaultMethod = "PUT"; var api = new ApiConfig(); config.Apis = new[] { api }; api.Url = "http://localhost/api/products/{idProduct}/something"; var responseCfg = new HttpResponseMock(); api.Responses = new[] { responseCfg }; responseCfg.Active = true; responseCfg.CustomApiException = new ApiException { FullTypeName = "FakeApiTest.CustomWebException, FakeApiTest", ConstructorArgs = new object[] { "Custom message" } }; ConfigIO.WriteConfig(config, filePath); var requester = new FakeHttpRequester(filePath); var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost/api/products/2/something"); webRequest.Method = "PUT"; //Act var exception = Assert.ThrowsException <CustomWebException>(() => { requester.GetResponse(webRequest); }); //Assert Assert.AreEqual(responseCfg.CustomApiException.ConstructorArgs.First(), exception.Message); }
public void Can_send_ResponseText_test_with_StatusDescription() { var mockRequest = new MockHttpRequest { ContentType = MimeTypes.Json }; var mockRequestContext = new HttpRequestContext(mockRequest, null, new object()); var mockResponse = new HttpResponseMock(); var customStatus = "Custom Status Description"; var httpResult = new HttpResult(System.Net.HttpStatusCode.Accepted, customStatus) { RequestContext = mockRequestContext }; var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html); Assert.That(reponseWasAutoHandled, Is.True); var statusDesc = mockResponse.StatusDescription; Assert.That(mockResponse.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.Accepted)); Assert.That(statusDesc, Is.EqualTo(customStatus)); }
public FetchHttpContext(string path, string queryString, string method) { ParentContext = HttpManager.CurrentContext; FetchRequest = new FetchHttpRequest(this, path, queryString, method); FetchResponse = new HttpResponseMock(this); }
public static HttpResponseMockAssertions Should(this HttpResponseMock response) { return(new HttpResponseMockAssertions(response)); }
public void VerifyAll() { ConnectionInfoMock.VerifyAll(); HttpRequestMock.VerifyAll(); HttpResponseMock.VerifyAll(); }
public void ShouldMockPropertiesFromApiResponse() { //Arrange var filePath = "apisConfig.json"; var config = new Config(); var api = new ApiConfig(); config.Apis = new[] { api }; api.Url = "http://localhost/api/products/{idProduct}/something"; var responseCfg = new HttpResponseMock(); api.Responses = new[] { responseCfg }; responseCfg.Active = true; responseCfg.Delay = 100; responseCfg.ContentLength = 2565121024; responseCfg.ContentType = "defaultContentType"; responseCfg.HttpCode = (int)HttpStatusCode.Ambiguous; responseCfg.IsFromCache = true; responseCfg.IsMutuallyAuthenticated = true; responseCfg.Method = "PUT"; responseCfg.Response = "defaultResponse"; responseCfg.ResponseUri = "http://localhost/api/myApi"; responseCfg.StatusDescription = "defaultStatusDescription"; responseCfg.Cookies = new[] { new Cookie("ck1", "ck1Value"), new Cookie("ck2", "ck2Value") }; responseCfg.Headers = new[] { new HttpHeader { Name = "header1", Value = "valueHeader1" }, new HttpHeader { Name = "header2", Value = "valueHeader2" }, }; ConfigIO.WriteConfig(config, filePath); var requester = new FakeHttpRequester(filePath); var webRequest = (HttpWebRequest)WebRequest.Create("http://localhost/api/products/2/something"); webRequest.Method = "Put"; //Act var webResponse = requester.GetResponse(webRequest); //Assert Assert.AreEqual(responseCfg.ContentType, webResponse.ContentType); Assert.AreEqual(responseCfg.Method, webResponse.Method); Assert.AreEqual(responseCfg.ResponseUri, webResponse.ResponseUri.AbsoluteUri); Assert.AreEqual(responseCfg.StatusDescription, webResponse.StatusDescription); Assert.AreEqual(responseCfg.ContentLength, webResponse.ContentLength); Assert.AreEqual(responseCfg.HttpCode, (int)webResponse.StatusCode); Assert.AreEqual(responseCfg.IsFromCache, webResponse.IsFromCache); Assert.AreEqual(responseCfg.IsMutuallyAuthenticated, webResponse.IsMutuallyAuthenticated); using (var streamReader = new StreamReader(webResponse.GetResponseStream())) { var content = streamReader.ReadToEnd(); Assert.AreEqual(responseCfg.Response, content); } for (int i = 0; i < responseCfg.Cookies.Count(); i++) { var cookieCfg = responseCfg.Cookies.ElementAt(i); var cookieResponse = webResponse.Cookies[i]; Assert.AreEqual(cookieCfg.Name, cookieResponse.Name); Assert.AreEqual(cookieCfg.Value, cookieResponse.Value); } for (int i = 0; i < responseCfg.Headers.Count(); i++) { var headerCfg = responseCfg.Headers.ElementAt(i); var headerResponseKey = webResponse.Headers.GetKey(i); var headerResponseValue = webResponse.Headers.GetValues(headerResponseKey).SingleOrDefault(); Assert.AreEqual(headerCfg.Name, headerResponseKey); Assert.AreEqual(headerCfg.Value, headerResponseValue); } }
public AddTodoItemAdapterResponse(HttpResponseMock httpResponseMock) { _httpResponseMock = httpResponseMock; }