Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(tests): mock_common_endpoints refactor #2251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions tests/integration/test_utils/mock_common_endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
use crate::integration::test_utils::{self, MockEndpointBuilder};
use mockito::Mock;

// Endpoints need to be bound, as they need to live long enough for test to finish
use crate::integration::test_utils::MockEndpointBuilder;

pub fn mock_common_upload_endpoints(
behavior: ServerBehavior,
chunk_options: ChunkOptions,
) -> Vec<Mock> {
common_upload_endpoints(behavior, chunk_options)
.map(super::mock_endpoint)
.collect()
}

/// Returns an iterator over builders for the common upload endpoints.
/// These can be used to generate mocks for the upload endpoints.
fn common_upload_endpoints(
behavior: ServerBehavior,
chunk_options: ChunkOptions,
) -> impl Iterator<Item = MockEndpointBuilder> {
let ChunkOptions {
chunk_size,
missing_chunks,
Expand Down Expand Up @@ -42,27 +53,21 @@ pub fn mock_common_upload_endpoints(
);

vec![
test_utils::mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/projects/wat-org/wat-project/releases/", 208)
.with_response_file("releases/get-release.json"),
)
.expect(release_request_count),
test_utils::mock_endpoint(
MockEndpointBuilder::new("GET", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body(chunk_upload_response),
),
test_utils::mock_endpoint(
MockEndpointBuilder::new("POST", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body("[]"),
),
test_utils::mock_endpoint(
MockEndpointBuilder::new("POST", assemble_endpoint, 200).with_response_body(format!(
MockEndpointBuilder::new("POST", "/api/0/projects/wat-org/wat-project/releases/", 208)
.with_response_file("releases/get-release.json")
.expect(release_request_count),
MockEndpointBuilder::new("GET", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body(chunk_upload_response),
MockEndpointBuilder::new("POST", "/api/0/organizations/wat-org/chunk-upload/", 200)
.with_response_body("[]"),
MockEndpointBuilder::new("POST", assemble_endpoint, 200)
.with_response_body(format!(
r#"{{"state":"created","missingChunks":{}}}"#,
serde_json::to_string(&missing_chunks).unwrap()
)),
)
.expect_at_least(1),
))
.expect_at_least(1),
]
.into_iter()
}

pub enum ServerBehavior {
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test_utils/mock_endpoint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ impl MockEndpointBuilder {
self.mock = self.mock.match_header(key, matcher);
self
}

/// Expect the mock endpoint to be hit at least `hits` times.
///
/// This expectation is only checked when the created mock is asserted.
pub fn expect_at_least(mut self, hits: usize) -> Self {
self.mock = self.mock.expect_at_least(hits);
self
}

/// Expect the mock endpoint to be hit exactly `hits` times.
///
/// This expectation is only checked when the created mock is asserted.
pub fn expect(mut self, hits: usize) -> Self {
self.mock = self.mock.expect(hits);
self
}
}

/// Build and return a mock endpoint with the provided configuration. The mock is automatically
Expand Down
Loading