Skip to content

Commit

Permalink
chore: update all dpes, and fix async mock test error
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Apr 3, 2024
1 parent 980372e commit 3378754
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "amp-client"
description = "The Amphitheatre API client for Rust"
version = "0.7.7"
version = "0.8.0"
edition = "2021"
license = "Apache-2.0"
homepage = "https://amphitheatre.app"
Expand All @@ -10,13 +10,13 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.7.8" }
futures = "0.3"
amp-common = { git = "https://github.com/amphitheatre-app/common", tag = "v0.8.0" }
futures = "0.3.30"
reqwest-eventsource = "0.6.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
tokio = { version = "1.33.0", features = ["full"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = { version = "1.0.115", features = ["raw_value"] }
tokio = { version = "1.37.0", features = ["full"] }

[dev-dependencies]
assert_matches = "1.5"
mockito = "1.1.0"
assert_matches = "1.5.0"
mockito = "1.4.0"
4 changes: 2 additions & 2 deletions tests/actors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use amp_common::sync::{EventKinds, Path, Synchronization};
use futures::StreamExt;
use reqwest_eventsource::Event;

use crate::common::setup_mock_for;
use crate::common::{setup_async_mock_for, setup_mock_for};
mod common;

#[test]
Expand Down Expand Up @@ -52,7 +52,7 @@ fn get_actor_test() {

#[tokio::test]
async fn get_actor_logs() {
let setup = setup_mock_for("/actors/1/hello/logs", "actors/get-actor-logs-success", "GET");
let setup = setup_async_mock_for("/actors/1/hello/logs", "actors/get-actor-logs-success", "GET").await;
let client = setup.0;
let pid = "1";
let name = "hello";
Expand Down
56 changes: 39 additions & 17 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,60 @@ use std::fs;
use amp_client::client::Client;
use mockito::{Server, ServerGuard};

/// Creates a mockserver and a client (changing the url of the client
/// to that of the mockserver to capture the requests).
/// Creates a mock server and a client (changing the url of the client
/// to that of the mock server to capture the requests).
///
/// It builds a response struct for the mock server using the fixture.
///
/// # Arguments
///
/// `fixture`: the path to the fixture inside the `api` directory
/// `path`: the path in the server (i.e. `/me`)
/// `method`: the HTTP method we are going to use (GET, POST, DELETE, ...)
pub fn setup_mock_for(path: &str, fixture: &str, method: &str) -> (Client, ServerGuard) {
let path = format!("/v1{}", path);
let fixture = format!("./tests/fixtures/v1/api/{}.http", fixture);

let content = fs::read_to_string(fixture.as_str()).expect("Something went wrong: Couldn't read the file");

let lines = content.lines();
let status = &content[9..12];
let body = lines.last();
let (status, body) = parse_fixture(fixture);

let mut server = Server::new();
server
.mock(method, path.as_str())
.with_header("x-ratelimit-limit", "2")
.with_header("x-ratelimit-remaining", "2")
.with_header("x-ratelimit-after", "never")
.with_status(status.parse().unwrap())
.with_body(body.unwrap())
.with_status(status)
.with_body(body)
.create();

let base_url = format!("{}/v1", server.url());
let client = Client::new(&base_url, Some("some-token".to_string()));

(client, server)
}

#[allow(dead_code)]
pub async fn setup_async_mock_for(path: &str, fixture: &str, method: &str) -> (Client, ServerGuard) {
let path = format!("/v1{}", path);
let (status, body) = parse_fixture(fixture);

let mut server = Server::new_async().await;
server
.mock(method, path.as_str())
.with_header("x-ratelimit-limit", "2")
.with_header("x-ratelimit-remaining", "2")
.with_header("x-ratelimit-after", "never")
.with_status(status)
.with_body(body)
.create_async()
.await;

let base_url = format!("{}/v1", server.url());
let client = Client::new(&base_url, Some("some-token".to_string()));

(client, server)
}

fn parse_fixture(fixture: &str) -> (usize, String) {
let fixture = format!("./tests/fixtures/v1/api/{}.http", fixture);

let content = fs::read_to_string(fixture.as_str()).expect("Something went wrong: Couldn't read the file");

let lines = content.lines();
let status = &content[9..12];
let body = lines.last().unwrap();

(status.parse().unwrap(), body.to_string())
}

0 comments on commit 3378754

Please sign in to comment.