fix: normalize pokemon lookup names

This commit is contained in:
2026-06-15 12:25:17 +02:00
parent cf11e20f90
commit 23eaea36c1
2 changed files with 32 additions and 2 deletions

View File

@@ -25,7 +25,11 @@ impl PokeApiClient {
#[instrument(skip(self), fields(pokemon.name = %name))]
pub async fn get_pokemon_info(&self, name: &str) -> Result<PokemonInfo, AppError> {
validate_pokemon_name(name)?;
let url = format!("{}/pokemon-species/{}", self.base_url, name);
let url = format!(
"{}/pokemon-species/{}",
self.base_url,
name.to_ascii_lowercase()
);
let response = self
.http
.get(url)
@@ -176,6 +180,32 @@ mod tests {
assert!(pokemon.is_legendary);
}
#[tokio::test]
async fn normalizes_pokemon_names_to_pokeapi_identifiers() {
let server = MockServer::start().await;
Mock::given(matchers::method("GET"))
.and(matchers::path("/pokemon-species/mewtwo"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"name": "mewtwo",
"is_legendary": true,
"habitat": { "name": "rare" },
"flavor_text_entries": [
{ "flavor_text": "Created by science.", "language": { "name": "en" } }
]
})))
.expect(1)
.mount(&server)
.await;
let client = client_for(&server);
let pokemon = client
.get_pokemon_info("Mewtwo")
.await
.expect("mixed-case pokemon names should resolve");
assert_eq!(pokemon.name, "mewtwo");
}
#[tokio::test]
async fn uses_unknown_habitat_when_pokeapi_has_no_habitat() {
let server = MockServer::start().await;