public async Task TestHttpClientMockedMapper() { //setup SetupMocks(); var ticker = "AAPL"; var path = $"/stock/{ticker}/book"; var mockResponse = GetIexResponse(); HttpResponseMessage mockHttpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mockResponse), Encoding.UTF8, "application/json") }; var client = GetMockClient(new MockClientConfigurator { MessagesToReturn = new Dictionary <string, HttpResponseMessage> { { path, mockHttpResponse } } }); var retriever = new StockRetriever(client, _mockMapper.Object); //execute var result = await retriever.GetStockAsync(ticker); //validate Assert.NotNull(result); _mockMapper.Verify(m => m.MapIexResponseToStock(It.IsAny <IexResponse>()), Times.Once); }
public async Task TestCaching() { //setup var ticker = "AAPL"; var path = $"/stock/{ticker}/book"; var mockResponse = GetIexResponse(); HttpResponseMessage mockHttpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mockResponse), Encoding.UTF8, "application/json") }; var client = GetMockClient(new MockClientConfigurator { MessagesToReturn = new Dictionary <string, HttpResponseMessage> { { path, mockHttpResponse } } }); var retriever = new StockRetriever(client, new Mapper()); //execute var result = await retriever.GetStockAsync("AAPL"); await Task.Delay(1100); var result2 = await retriever.GetStockAsync("AAPL"); //validate Assert.NotNull(result); Assert.NotNull(result2); Assert.Equal(result.LastUpdated, result2.LastUpdated); }
public async Task TestDefaultClient() { //setup var ticker = "AAPL"; var retriever = new StockRetriever(); //execute var result = await retriever.GetStockAsync(ticker); //validate Assert.NotNull(result); }
public async Task TestHttpCollections() { //setup SetupMocks(); var ticker = "AAPL"; var path = $"/stock/{ticker}/book"; var ticker2 = "MSFT"; var path2 = $"/stock/{ticker2}/book"; var mockResponse = GetIexResponse(); HttpResponseMessage mockHttpResponse = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mockResponse), Encoding.UTF8, "application/json") }; mockResponse.quote.symbol = "MSFT"; HttpResponseMessage mockHttpResponse2 = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mockResponse), Encoding.UTF8, "application/json") }; var client = GetMockClient(new MockClientConfigurator { MessagesToReturn = new Dictionary <string, HttpResponseMessage> { { path, mockHttpResponse }, { path2, mockHttpResponse2 } } }); var retriever = new StockRetriever(client, _mockMapper.Object); //execute var result = await retriever.GetStocksAsync(new string[] { "AAPL", "MSFT" }); //validate Assert.NotNull(result); Assert.Equal(ticker, result.ToList()[0].Ticker); Assert.Equal(ticker2, result.ToList()[1].Ticker); _mockMapper.Verify(m => m.MapIexResponseToStock(It.IsAny <IexResponse>()), Times.Exactly(2)); }
public static async Task <HttpResponseMessage> RunHttp( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info($"RetrieveStockData processed at {DateTime.Now}"); string priceValue = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "max", StringComparison.OrdinalIgnoreCase) == 0) .Value; bool validMaxPrice = double.TryParse(priceValue, out var maxPrice); StockRetriever retrieve = new StockRetriever(); var results = await retrieve.RetrieveAllStockInformationAsync(); return(new HttpResponseMessage(HttpStatusCode.OK) { RequestMessage = req, Content = new StringContent( JsonConvert.SerializeObject(!validMaxPrice ? results : results.Where(x => x.Price <= maxPrice && x.Price > 0)), Encoding.UTF8, "application/json") }); }