public async Task GetPokemonData_InvalidInput() { //mock fake response and initialize test var fakeHandler = new FakeHttpMessageHandler(); fakeHandler.Response = new HttpResponseMessage(HttpStatusCode.NotFound); //fakeHandler.Response.StatusCode = HttpStatusCode.NotFound; fakeHandler.Response.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("Not Found"))); var pokemonName = "fakePokemon"; var endpoint = string.Format("{0}/{1}/{2}", pokeApiBaseEndpoint, pokemonEndpoint, pokemonName); HttpClient client = new HttpClient(fakeHandler); //execute - assert try { var result = await PokeApiUtils.GetPokemonDataAsync(client, endpoint); } catch (HttpException ex) { Assert.AreEqual(HttpStatusCode.NotFound, (HttpStatusCode)ex.GetHttpCode()); return; } }
public async Task <IHttpActionResult> GetPokemonDataAsync(string pokemonName) { //test api call get pokemon var pokeapiEndpoint = ConfigurationManager.AppSettings["PokeApiEndpoint"]; var shakesperareTranslatorEndpoint = ConfigurationManager.AppSettings["ShakespeareTranslatorEndpoint"]; //get pokemon data via PokeApi var pokemonDescription = ""; //get pokemon data try { var pokemon = await PokeApiUtils.GetPokemonDataAsync(Client, string.Format("{0}/pokemon/{1}", pokeapiEndpoint, pokemonName)); if (!string.IsNullOrEmpty(pokemon.species?.Url)) { //get species data var pokemonSpecies = await PokeApiUtils.GetPokemonSpeciesDataAsync(Client, pokemon.species.Url); //filter for english language and group results by version, to take only the most recent values List <string> flavorTexts = pokemonSpecies.flavorTextEntries.Where(t => t.language.Name == "en").GroupBy(g => g.version.Url).LastOrDefault()?.Select(s => s.flavorText).ToList(); pokemonDescription = string.Join("\n", flavorTexts); } } catch (HttpException ex) { return(Content((HttpStatusCode)ex.GetHttpCode(), ex.Message.ToString())); } catch (Exception ex) { return(Content(HttpStatusCode.InternalServerError, ex.Message.ToString())); } //get translation via ShakespeareTranslator var translatedDescription = ""; //get translation try { var translation = await ShakespeareTranslatorUtils.GetTranslationAsync(Client, shakesperareTranslatorEndpoint, pokemonDescription); translatedDescription = translation.contents.translated; } catch (HttpException ex) { return(Content((HttpStatusCode)ex.GetHttpCode(), ex.Message.ToString())); } catch (Exception ex) { return(Content(HttpStatusCode.InternalServerError, ex.Message.ToString())); } var response = new TranslatedPokemonResponseModel(pokemonName, translatedDescription); return(Ok(response)); }
public async Task GetPokemonData_ValidInput() { //mock fake response and initialize test var fakeHandler = new FakeHttpMessageHandler(); fakeHandler.Response.StatusCode = HttpStatusCode.OK; var pokemonName = "pikachu"; var endpoint = string.Format("{0}/{1}/{2}", pokeApiBaseEndpoint, pokemonEndpoint, pokemonName); var samplePokemon = GetSamplePokemon(pokemonName); fakeHandler.Response.Content = new StringContent(JsonConvert.SerializeObject(samplePokemon), Encoding.UTF8, "application/json"); HttpClient client = new HttpClient(fakeHandler); //execute var result = await PokeApiUtils.GetPokemonDataAsync(client, endpoint); //assert Assert.AreEqual(pokemonName, result.name); }