diff --git a/Cargo.toml b/Cargo.toml index 0d05dac..df76850 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,6 @@ anyhow = "1.0" governor = "0.4" # TODO: Relies on a yet-unreleased interface. Switch to an actual crate release once available die-exit = { git = "https://github.com/Xavientois/die.git", rev = "31d3801f4e21654b0b28430987b1e21fc7728676" } + +[dev-dependencies] +httpmock = "0.6" diff --git a/src/main.rs b/src/main.rs index 6679bbc..8ec53b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,3 +123,32 @@ async fn main() -> anyhow::Result<()> { return Ok(()); } + +#[cfg(test)] +mod tests { + use httpmock::MockServer; + use serde_json::json; + + #[test] + fn create_repo_success_test() { + // Arrange + let server = MockServer::start(); + let mock = server.mock(|when, then| { + when.method("POST") + .path("/user/repos") + .header("Authorization", "token TOKEN") + .header("Content-Type", "application/json"); + then.status(201) + .json_body(json!({ "html_url": "http://example.com" })); + }); + let client = GithubClient::new("TOKEN", &server.base_url()); + + // Act + let result = client.create_repo("myRepo"); + + // Assert + mock.assert(); + assert_eq!(result.is_ok(), true); + assert_eq!(result.unwrap(), "http://example.com"); + } +} \ No newline at end of file