public async Task <ActionResult> Edit(Contact contact) { Log.Information($"{nameof(ContactController)} - {nameof(Edit)}"); using (var client = new HttpClient()) { client.BaseAddress = new System.Uri(CONTACT_BASE_URI); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpRequestMessage Request = HttpRequestHelper.CreateHttpRequest(HttpMethod.Put, new System.Uri(CONTACT_BASE_URI + "api/Contact/UpdateContact"), contact); HttpResponseMessage Response = await client.SendAsync(Request); if (Response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { Log.Information($"{nameof(ContactController)} - {nameof(Create)}- Update request failed"); ModelState.AddModelError(string.Empty, "Unable to update contact"); return(BadRequest()); } } }
/// <summary> /// Gets the download info using url only /// </summary> /// <param name="url">Source url</param> /// <returns></returns> public static HttpDownloadInfo Get(string url) { using (var response = (HttpWebResponse)HttpRequestHelper.CreateHttpRequest(url, null, true).GetResponse()) { return(GetFromResponse(response, url)); } }
public void When_Trigger() { // Arrange var httpRequest = HttpRequestHelper.CreateHttpRequest( "POST", "http://localhost"); // Act var actionResult = CaseNumberGenerateHttpTrigger.Run( httpRequest, _logger); // Assert actionResult.Should().BeOfType(typeof(OkObjectResult)); var okObjectResult = (OkObjectResult)actionResult; okObjectResult.Value.Should().BeOfType(typeof(int)); }
public async void When_Trigger() { // Arrange var accountDataStore = new Mock <IAccountDataStore>(); var accountAddHttpTrigger = new AccountAddHttpTrigger( accountDataStore.Object); var accountEntityAddOptions = new AccountEntityAddOptions { AccountNumber = _faker.Random.Number(10000, 99999).ToString(), SystemOfRecord = _faker.PickRandom( AccountData.SYSTEMOFRECORD_AUTOSUITE, AccountData.SYSTEMOFRECORD_FISERVE, AccountData.SYSTEMOFRECORD_ISERIES, AccountData.SYSTEMOFRECORD_LEASEMASTER), PhoneNumber = _faker.Person.Phone, }; var httpRequestHeaders = new HeaderDictionary { { "Content-Type", "application/json" } }; var httpRequest = HttpRequestHelper.CreateHttpRequest( "POST", "http://localhost", httpRequestHeaders, accountEntityAddOptions); // Act var actionResult = await accountAddHttpTrigger.Run( httpRequest, _logger); // Assert accountDataStore.Verify(x => x.AddAsync(It.IsAny <AccountData>())); actionResult.Should().BeOfType(typeof(CreatedResult)); var createdResult = (CreatedResult)actionResult; createdResult.Value.Should().BeOfType(typeof(AccountEntity)); }
public async void When_Trigger() { // Arrange var accountDataStore = new Mock <IAccountDataStore>(); accountDataStore .Setup(x => x.FetchByAccountNumberAsync(It.IsAny <string>())) .ReturnsAsync(new AccountData()); accountDataStore .Setup(x => x.UpdateByIdAsync(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <AccountData>())); var accountUpdateHttpTrigger = new AccountUpdateHttpTrigger( accountDataStore.Object); var accountEntityUpdateOptions = new AccountEntityUpdateOptions { PhoneNumber = _faker.Person.Phone, }; var httpRequestHeaders = new HeaderDictionary { { "Content-Type", "application/json" } }; var httpRequest = HttpRequestHelper.CreateHttpRequest( "PUT", "http://localhost", httpRequestHeaders, accountEntityUpdateOptions); var accountNumber = _faker.Random.Number(10000, 99999).ToString(); // Act var actionResult = await accountUpdateHttpTrigger.Run( httpRequest, accountNumber, _logger); // Assert accountDataStore.Verify(x => x.FetchByAccountNumberAsync(It.IsAny <string>())); accountDataStore.Verify(x => x.UpdateByIdAsync(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <AccountData>())); actionResult.Should().BeOfType(typeof(OkObjectResult)); }
/// <summary> /// Gets the newest download informations in case of url expired or remote file was modified /// </summary> /// <returns></returns> public HttpDownloadInfo GetCurrentInformations() { try { var req = HttpRequestHelper.CreateHttpRequest(this.Url, null, true); cd_BeforeSendingRequest(this, new BeforeSendingRequestEventArgs(req)); using (var resp = (HttpWebResponse)req.GetResponse()) { return(HttpDownloadInfo.GetFromResponse(resp, this.Url)); } } catch { return(null); } }
public async void When_Trigger_And_NextPaymentDateIsNull_And_ZipCodeIsValid( DateTime nextPaymentDate, string postalCode, DateTime expectedNextPaymentDate) { // Arrange var rescheduleDateCalculateOptions = new RescheduleDateCalculateOptions { NextPaymentDate = nextPaymentDate, PostalCode = postalCode }; var httpRequestHeaders = new HeaderDictionary { { "Content-Type", "application/json" } }; var httpRequest = HttpRequestHelper.CreateHttpRequest( "POST", "http://localhost", httpRequestHeaders, rescheduleDateCalculateOptions); // Act var actionResult = await RescheduleDateCalculateHttpTrigger.Run( httpRequest, _logger); // Assert var okObjectResult = (OkObjectResult)actionResult; nextPaymentDate = (DateTime)okObjectResult.Value; nextPaymentDate.Date.Should().Be(expectedNextPaymentDate); }
public async void When_Trigger_And_NextPaymentDateIsNull() { // Arrange var nextPaymentDateCalculateOptions = new NextPaymentDateCalculateOptions { NextPaymentDate = null }; var httpRequestHeaders = new HeaderDictionary { { "Content-Type", "application/json" } }; var httpRequest = HttpRequestHelper.CreateHttpRequest( "POST", "http://localhost", httpRequestHeaders, nextPaymentDateCalculateOptions); // Act var actionResult = await NextPaymentDateCalculateHttpTrigger.Run( httpRequest, _logger); // Assert var okObjectResult = (OkObjectResult)actionResult; var nextPaymentDate = (DateTime)okObjectResult.Value; var expectedNextPaymentDate = DateTime.UtcNow.Date.AddDays(2); nextPaymentDate.Date.Should().Be(expectedNextPaymentDate); }
public async void When_Trigger() { // Arrange var nextPaymentDate = _faker.Date.Soon(); var nextPaymentDateCalculateOptions = new NextPaymentDateCalculateOptions { NextPaymentDate = nextPaymentDate }; var httpRequestHeaders = new HeaderDictionary { { "Content-Type", "application/json" } }; var httpRequest = HttpRequestHelper.CreateHttpRequest( "POST", "http://localhost", httpRequestHeaders, nextPaymentDateCalculateOptions); // Act var actionResult = await NextPaymentDateCalculateHttpTrigger.Run( httpRequest, _logger); // Assert actionResult.Should().BeOfType(typeof(OkObjectResult)); var okObjectResult = (OkObjectResult)actionResult; okObjectResult.Value.Should().BeOfType(typeof(DateTime)); }
public async void When_Trigger() { // Arrange var accountDataStore = new Mock <IAccountDataStore>(); accountDataStore .Setup(x => x.FetchByAccountNumberAsync(It.IsAny <string>())) .ReturnsAsync(new AccountData()); var accountFetchHttpTrigger = new AccountFetchHttpTrigger( accountDataStore.Object); var httpRequest = HttpRequestHelper.CreateHttpRequest( "GET", "http://localhost"); var accountNumber = _faker.Random.Number(10000, 99999).ToString(); // Act var actionResult = await accountFetchHttpTrigger.Run( httpRequest, accountNumber, _logger); // Assert accountDataStore.Verify(x => x.FetchByAccountNumberAsync(It.IsAny <string>())); actionResult.Should().BeOfType(typeof(OkObjectResult)); var okObjectResult = (OkObjectResult)actionResult; okObjectResult.Value.Should().BeOfType(typeof(AccountEntity)); }
void DownloadProcedure() { try { var endOfStream = false; var stOffset = Range.Remaining.Start; var chunkEndOffset = stOffset + 1 * 1024 * 1000 - 1; var endOffset = UseChunk ? Math.Min(chunkEndOffset, Range.End) : Range.End; Range.Status = Status = State.SendRequest; Range.LastTry = DateTime.Now; request = Info != null? HttpRequestHelper.CreateHttpRequest(Info, stOffset, endOffset, BeforeSendingRequest) : HttpRequestHelper.CreateHttpRequest(Url, BeforeSendingRequest, true); Range.Status = Status = State.GetResponse; using (var response = Info != null ? HttpRequestHelper.GetRangedResponse(Info, stOffset, endOffset, request, AfterGettingResponse) : request.GetResponse() as HttpWebResponse) { Range.Status = Status = State.GetResponseStream; ResponseReceived.Raise(this, new ResponseReceivedEventArgs(response)); while (Wait) { Thread.Sleep(100); } using (fileStream = FileHelper.CheckFile(Range.FilePath, Range.TotalBytesReceived > 0)) { using (var str = response.GetResponseStream()) { var buffer = new byte[10 * 1024]; var bytesRead = 0; while (true) { if (str != null) { bytesRead = str.Read(buffer, 0, buffer.Length); } if (Info.ContentSize > 0) { bytesRead = (int)Math.Min(Range.Size - Range.TotalBytesReceived, bytesRead); } if (bytesRead <= 0) { endOfStream = true; break; } if (stopFlag) { break; } if (Info != null && Info.ContentSize > 0 && Range.IsDownloaded) { break; } Range.Status = Status = State.Downloading; fileStream.Write(buffer, 0, bytesRead); Range.TotalBytesReceived += bytesRead; ProgressChanged.Raise(this, EventArgs.Empty); } } } } lock (GlobalLock.Locker) { Range.LastTry = DateTime.Now; if ((!stopFlag && endOfStream && Info.ContentSize < 1) || (Info.ContentSize > 0 && Range.IsDownloaded)) { Range.Status = Status = State.Completed; Completed.Raise(this, EventArgs.Empty); } else if (stopFlag) { Range.Status = Status = State.Stopped; Stopped.Raise(this, EventArgs.Empty); } } } catch (Exception ex) { lock (GlobalLock.Locker) { Range.LastTry = DateTime.Now; if (Info != null && Info.ContentSize > 0 && Range.IsDownloaded) { Range.Status = Status = State.Completed; Completed.Raise(this, EventArgs.Empty); } else if (stopFlag) { Range.Status = Status = State.Stopped; Stopped.Raise(this, EventArgs.Empty); } else { Range.Status = Status = State.Failed; Failed.Raise(this, new ErrorEventArgs(ex)); } } } finally { if (request != null) { request.Abort(); } if (!Range.IsIdle) { Range.Status = Status = State.Failed; } } }