public void Get_Current_Weather_Success() { var payload = new { cod = 200, coord = new { lon = 9.89, lat = 53.47 } }; //Arrange _mockHttpMessageHandler.Protected(). Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonSerializer.Serialize(payload)) }); _mockHttpClient = new Mock <HttpClient>(_mockHttpMessageHandler.Object); _client = new OpenWeatherMapClient(_mockHttpClient.Object, _mockConfig); var response = _client.GetCurrentWeatherByCity(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()); Assert.IsNotNull(response); Assert.IsNotNull(response.Result); Assert.IsInstanceOfType(response.Result, typeof(CurrentWeatherResponse)); Assert.AreEqual(payload.cod, response.Result.Code); Assert.AreEqual(payload.coord.lat, response.Result.Coordinates.Lat); Assert.AreEqual(payload.coord.lon, response.Result.Coordinates.Lon); }
public Worker( IHttpClientFactory factory, IWeatherForecastClient weatherForecastClient, ILogger <Worker> logger) { _regularHttpClient = factory.CreateClient("weather-api-client"); _weatherForecastClient = weatherForecastClient; _logger = logger; }
public async Task Test_GetWeatherForecastsAsync_Returns_Weather() { Db.Recreate(); var now = DateTime.UtcNow; using (WeatherDbContext ctx = Db.GetAdminContext()) { ctx.Set <WeatherForecastEntity>() .AddRange(new WeatherForecastEntity { Id = Guid.NewGuid(), Date = now, Summary = "sum-1", TemperatureC = 1 }, new WeatherForecastEntity { Id = Guid.NewGuid(), Date = now.AddDays(-1), Summary = "sum-2", TemperatureC = 1 }); await ctx.SaveChangesAsync(); } IWeatherForecastClient client = Integration.CreateClient(); WeatherForecastGetModel[] models = await client.GetWeatherForecasts(CancellationToken.None); Assert.AreEqual(2, models.Length); Assert.AreEqual("sum-1", models[0].Summary); Assert.AreEqual("sum-2", models[1].Summary); }
public UnitTest1() { var httpClientFactory = Substitute.For <IHttpClientFactory>(); httpClientFactory .CreateClient(nameof(WeatherForecastClient)) .Returns(client); _weatherForecastClient = new WeatherForecastClient(httpClientFactory); }
public void Get_Forecast_Weather_Success() { var payload = new { cod = "200", city = new { name = "Hamburg Hausbruch", coord = new { lat = 53.4703, lon = 9.8933 }, country = "DE", timezone = 7200, sunrise = 1590116956, sunset = 1590175516 } }; //Arrange _mockHttpMessageHandler.Protected(). Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonSerializer.Serialize(payload)) }); _mockHttpClient = new Mock <HttpClient>(_mockHttpMessageHandler.Object); _client = new OpenWeatherMapClient(_mockHttpClient.Object, _mockConfig); var response = _client.GetForecastByZipcode(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()); Assert.IsNotNull(response); Assert.IsNotNull(response.Result); Assert.IsInstanceOfType(response.Result, typeof(WeatherForecastResponse)); Assert.AreEqual(payload.cod, response.Result.Code); Assert.AreEqual(payload.city.name, response.Result.CityData.Name); Assert.AreEqual(payload.city.coord.lon, response.Result.CityData.Coordinates.Lon); Assert.AreEqual(payload.city.coord.lat, response.Result.CityData.Coordinates.Lat); Assert.AreEqual(payload.city.country, response.Result.CityData.Country); Assert.AreEqual(payload.city.timezone, response.Result.CityData.Timezone); Assert.AreEqual(payload.city.sunrise, response.Result.CityData.Sunrise); Assert.AreEqual(payload.city.sunset, response.Result.CityData.Sunset); }
public async Task Test_GetCurrentWeatherAsync_Returns_Weather() { const string city = "Mountain View"; Integration.WeatherApi.Given(Request.Create() .WithPath("/data/2.5/weather") .WithParam("appid", Integration.MockWeatherApiKey) .WithParam("q", city)) .RespondWith(Response.Create() .WithSuccess() .WithBodyFromFile("Controllers\\mockWeatherResponse.json")); IWeatherForecastClient client = Integration.CreateClient(); WeatherGetModel weather = await client.GetCurrentWeatherIn(city, CancellationToken.None); Assert.AreEqual(weather.Name, "Mountain View"); }
public void Get_Current_Weather_Fail() { //Arrange _mockHttpMessageHandler.Protected(). Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest }); _mockHttpClient = new Mock <HttpClient>(_mockHttpMessageHandler.Object); _client = new OpenWeatherMapClient(_mockHttpClient.Object, _mockConfig); var response = _client.GetCurrentWeatherByCity(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()); Assert.IsNull(response.Result); }
public GetForecastByZipcodeHandler(IForecastService forecastService, IWeatherForecastClient weatherClient) { _forecastService = forecastService; _weatherClient = weatherClient; }
public WeatherForecastController(IWeatherForecastClient weatherForecastClient) { WeatherForecastClient = weatherForecastClient; }
public WeatherService(IWeatherForecastClient weatherForecastClient, ILogger <WeatherService> logger) { _weatherForecastClient = weatherForecastClient; _logger = logger; }
public WeatherForecastService(IWeatherForecastClient weatherForecastClient) { _weatherForecastClient = weatherForecastClient; }
private void Initialize(IWebClientServiceSettings settings, HttpClient httpClient) { WeatherForecastClient = new WeatherForecastClient(settings, httpClient); }
public WeatherForecastRepository(IWeatherForecastClient weatherForecastClient) { _weatherForecastClient = weatherForecastClient; }
public WeatherForecastController(ILogger <WeatherForecastController> logger, IWeatherForecastClient weatherForecastClient) { _logger = logger; _weatherForecastClient = weatherForecastClient ?? throw new ArgumentNullException(nameof(weatherForecastClient)); }
public ConsumerController(ILogger <ConsumerController> logger, IWeatherForecastClient weatherForecastClient) { _logger = logger; _weatherForecastClient = weatherForecastClient; }
public static Task <IReadOnlyList <WeatherForecast>?> GetForTodayAsync(this IWeatherForecastClient client, CancellationToken cancellationToken = default) => client.GetAsync(1, cancellationToken);
public WeatherForecastProxyController(IWeatherForecastClient weatherForecastClient) { _weatherForecastClient = weatherForecastClient; }