public async Task TestShakespeareApi() { const string content = "You gave Mr. Tim a hearty meal, but unfortunately what he ate made him die."; JsonResult shakespeareResult = await ShakespeareClient.getShakespeareTranslated(content); Assert.IsNotNull(shakespeareResult); Assert.AreEqual(shakespeareResult.Value.ToString(), "Thee did giveth mr. Tim a hearty meal, but unfortunately what he did doth englut did maketh him kicketh the bucket."); }
public static async Task <JsonResult> getTranslatedDescription(string pokemonName) { PokemonSpecies p; try { p = await DataFetcher.GetNamedApiObject <PokemonSpecies>(pokemonName); } catch (HttpRequestException) { return(new JsonResult(ErrorMessage.pokeMonNotFoundOrUnAvailable) { StatusCode = StatusCodes.Status404NotFound }); } catch (PokemonParseException ex) { return(new JsonResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError }); } try { string description = Array.Find(p.FlavorTexts, element => element.Language.Name == "en").FlavorText; JsonResult shakespeareResult = await ShakespeareClient.getShakespeareTranslated(description); if (shakespeareResult.StatusCode == StatusCodes.Status429TooManyRequests) { return(new JsonResult(ErrorMessage.tooManyRequest) { StatusCode = StatusCodes.Status429TooManyRequests }); } PokemonDetails pDetails = new PokemonDetails() { Name = p.Name, Description = shakespeareResult.Value.ToString() }; return(new JsonResult(pDetails) { StatusCode = StatusCodes.Status200OK // Status code here }); } catch (Exception ex) { return(new JsonResult(ex.Message) { StatusCode = StatusCodes.Status500InternalServerError // Status code here }); } }
public async Task TestShakespeareApiWhenReachingRateLimiter() { const int count = 15; string content = "What he ate made him die."; for (int i = 0; i < count; i++) { await ShakespeareClient.getShakespeareTranslated(content); } JsonResult shakespeareResult = await ShakespeareClient.getShakespeareTranslated(content); Assert.AreEqual(shakespeareResult.StatusCode, StatusCodes.Status429TooManyRequests); }