test against test server

This commit is contained in:
Robin Appelman 2020-11-26 21:52:23 +01:00
commit 7860e8df34
7 changed files with 198 additions and 75 deletions

View file

@ -1,10 +1,11 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt;
use reqwest::{Client, IntoUrl, Url, StatusCode};
use reqwest::{Client, IntoUrl, Url, StatusCode, multipart};
use thiserror::Error;
use steamid_ng::SteamID;
use std::borrow::Cow;
use std::str::FromStr;
#[derive(Debug, Error)]
pub enum Error {
@ -20,6 +21,8 @@ pub enum Error {
HashMisMatch,
#[error("Unknown server error")]
ServerError(u16),
#[error("Invalid response: {0}")]
InvalidResponse(String),
}
#[derive(Clone, Debug, Deserialize)]
@ -427,7 +430,32 @@ impl ApiClient {
_ => Ok(())
}
}
}
#[cfg(test)]
mod tests;
pub async fn upload_demo(&self, file_name: String, body: Vec<u8>, red: String, blue: String, key: String) -> Result<u32, Error> {
let form = multipart::Form::new()
.text("red", red)
.text("blue", blue)
.text("name", file_name)
.text("key", key);
let file = multipart::Part::bytes(body)
.file_name("demo.dem")
.mime_str("text/plain")?;
let form = form.part("demo", file);
let resp = self.client.post(self.base_url.join("/upload").unwrap())
.multipart(form)
.send()
.await?
.text()
.await?;
if resp == "Invalid key" {
return Err(Error::InvalidApiKey)
}
let tail = resp.split('/').last().unwrap_or_default();
u32::from_str(tail).map_err(|_| Error::InvalidResponse(resp))
}
}

View file

@ -1,59 +0,0 @@
use crate::{ApiClient, ListParams, ListOrder, Error};
use steamid_ng::SteamID;
#[tokio::test]
async fn test_list_demos() {
let client = ApiClient::default();
let demos = client.list(ListParams::default().with_order(ListOrder::Ascending), 1).await.unwrap();
assert_eq!(demos[0].id, 9);
assert_eq!(demos[0].uploader.id(), 1);
assert!(demos[0].uploader.user().is_none());
assert_eq!(demos[0].uploader.resolve(&client).await.unwrap().steam_id, SteamID::from(76561198024494988));
}
#[tokio::test]
async fn test_get_demo() {
let client = ApiClient::default();
let demo = client.get(9).await.unwrap();
assert_eq!(demo.id, 9);
assert_eq!(demo.uploader.id(), 1);
assert!(demo.uploader.user().is_some());
assert_eq!(demo.uploader.user().unwrap().steam_id, SteamID::from(76561198024494988));
assert_eq!(demo.uploader.resolve(&client).await.unwrap().steam_id, SteamID::from(76561198024494988));
assert_eq!(demo.players[0].player_id, 623);
assert_eq!(demo.players[0].user.id, 346);
}
#[tokio::test]
async fn test_get_chat() {
let client = ApiClient::default();
let chat = client.get_chat(447678).await.unwrap();
assert_eq!(chat.len(), 10);
assert_eq!(chat[0].user, "wiitabix");
assert_eq!(chat[0].time, 5);
assert_eq!(chat[0].message, "gl hf :)))))");
}
#[tokio::test]
async fn test_get_players() {
let client = ApiClient::default();
let demos = client.list(ListParams::default().with_order(ListOrder::Ascending), 1).await.unwrap();
assert_eq!(demos[0].players.len(), 0);
assert_eq!(demos[0].get_players(&client).await.unwrap().len(), 12);
}
#[tokio::test]
async fn test_set_url_invalid_key() {
let client = ApiClient::default();
let res = client.set_url(9, "test", "test", "http://example.com/test", [0; 16], "wrong").await;
assert!(matches!(res.unwrap_err(), Error::InvalidApiKey));
}