public async Task SuccessfulRequestReturnsListofStocks() { Environment.SetEnvironmentVariable("API_KEY", "123"); Environment.SetEnvironmentVariable("API_HOST", "www.host"); Environment.SetEnvironmentVariable("API_BASE_ADDRESS", "http://www.xyz.com"); var responseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"quoteResponse\":{\"result\":[{\"symbol\":\"AAPL\",\"bid\":100}] }}") }; var httpClient = new HttpClient(new MockHttpMessageHandler(responseMessage)); var stockClient = new StockQuoteClient(httpClient); var stockList = new List <string>(); stockList.Add("AAPL"); var actual = await stockClient.GetQuotes(stockList); var expectedStock = new Stock(); expectedStock.Symbol = "AAPL"; expectedStock.Price = 100; Assert.AreEqual(1, actual.Count); Assert.AreEqual(expectedStock.Price, actual[0].Price); Assert.AreEqual(expectedStock.Symbol, actual[0].Symbol); }
static void Main(string[] args) { var context = new InstanceContext(new QuoteHandler()); StockQuoteClient client = new StockQuoteClient(context); client.StartSendingQuotes(); Console.ReadLine(); }
public async void WhenServerTimeoutErrorOccurs_TimeoutExceptionThrown() { // Arrange var httpClient = FakeHttpClientFactory.CreateFakeHttpClientForTimeout(); StockQuoteClient client = new StockQuoteClient(httpClient); // Act and Assert var result = await Assert.ThrowsAsync <TimeoutException>(() => client.GetStockQuote("ZVZZT")); }
public async void WhenInternalServerError_UnknownTickerSymbolExceptionThrown() { // Arrange var httpClient = FakeHttpClientFactory.CreateFakeHttpClientForStringContent(HttpStatusCode.InternalServerError, "text/plain", "Internal Server Error"); StockQuoteClient client = new StockQuoteClient(httpClient); // Act and Assert var result = await Assert.ThrowsAsync <ApplicationException>(() => client.GetStockQuote("ZVZZT")); }
public async void WhenTickerSymbolNotFound_UnknownTickerSymbolExceptionThrown() { // Arrange var httpClient = FakeHttpClientFactory.CreateFakeHttpClientForStringContent(HttpStatusCode.NotFound, "text/plain", "Symbol not found"); StockQuoteClient client = new StockQuoteClient(httpClient); // Act and Assert var result = await Assert.ThrowsAsync <UnknownTickerSymbolException>(() => client.GetStockQuote("ZVZZT")); Assert.Equal("ZVZZT", result.TickerSymbol); }
public async void TestStockTickerGoodResult() { // Arrange var httpClient = FakeHttpClientFactory.CreateFakeHttpClientForJsonStringContent(HttpStatusCode.OK, AAPL_QUOTE); StockQuoteClient client = new StockQuoteClient(httpClient); // Act var result = await client.GetStockQuote("AAPL"); // Assert Assert.Equal("AAPL", result.Symbol); }
public async Task BadRequestReturnsNull() { Environment.SetEnvironmentVariable("API_KEY", "123"); Environment.SetEnvironmentVariable("API_HOST", "www.host"); Environment.SetEnvironmentVariable("API_BASE_ADDRESS", "http://www.xyz.com"); var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest); var httpClient = new HttpClient(new MockHttpMessageHandler(responseMessage)); var stockClient = new StockQuoteClient(httpClient); var stockList = new List <string>(); stockList.Add("AAPL"); var result = await stockClient.GetQuotes(stockList); Assert.IsNull(result); }