public Task <double> Handle(GetBitcoinConversionQuery request, CancellationToken cancellationToken) { var conversionInfo = new BitcoinConversionInfo { Amount = request.Value, Abbreviation = request.Abbreviation }; return(_bitcoinPriceService.ConvertToBitcoin(conversionInfo, cancellationToken)); }
public async Task <double> ConvertToBitcoin(BitcoinConversionInfo bitcoinConversionInfo, CancellationToken cancellationToken) { if (bitcoinConversionInfo.Abbreviation == null) { throw new ArgumentException($"{nameof(bitcoinConversionInfo.Abbreviation)} is required for method" + $"{nameof(ConvertToBitcoin)}"); } var path = // ReSharper disable once StringLiteralTypo $"tobtc?currency={bitcoinConversionInfo.Abbreviation}" + $"&value={bitcoinConversionInfo.Amount}"; return (await GetResponseFromService <double>(path, cancellationToken)); }
public async Task ConvertToBitcoin_UnsuccessfulHttpRequest_ThrowsBitcoinPriceServiceException() { var mockHttp = new MockHttpMessageHandler(); mockHttp.When("https://blockchain.info/tobtc") .Respond(HttpStatusCode.InternalServerError); var service = new BitcoinPriceService(_configuration.Object, mockHttp.ToHttpClient()); var conversionInfo = new BitcoinConversionInfo { Abbreviation = "GBP", Amount = 1323 }; await Assert .ThrowsAsync <BitcoinPriceService.BitcoinPriceServiceException>(async() => await service.ConvertToBitcoin(conversionInfo, CancellationToken.None)); }
public async Task ConvertToBitcoin_AbbreviationNull_ThrowsArgumentException() { var mockHttp = new MockHttpMessageHandler(); mockHttp.When("https://blockchain.info/tobtc") .Respond("application/json", "200"); var service = new BitcoinPriceService(_configuration.Object, mockHttp.ToHttpClient()); var conversionInfo = new BitcoinConversionInfo { Abbreviation = null, Amount = 1323 }; await Assert .ThrowsAsync <ArgumentException>(async() => await service.ConvertToBitcoin(conversionInfo, CancellationToken.None)); }
public async Task ConvertToBitcoin_ReturnsCorrectResponse() { var mockHttp = new MockHttpMessageHandler(); mockHttp.When("https://blockchain.info/tobtc") .Respond("application/json", "200"); var service = new BitcoinPriceService(_configuration.Object, mockHttp.ToHttpClient()); var conversionInfo = new BitcoinConversionInfo { Abbreviation = "GBP", Amount = 1323 }; var value = await service.ConvertToBitcoin(conversionInfo, CancellationToken.None); Assert.Equal(200, value); }