Skip to content

Commit

Permalink
ref(tests): mock_common_endpoints refactor
Browse files Browse the repository at this point in the history
Separate the logic for configuring the common upload mocks and the logic for actually creating the mocks
  • Loading branch information
szokeasaurusrex committed Nov 14, 2024
1 parent 431ed82 commit 67ca9e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
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

0 comments on commit 67ca9e7

Please sign in to comment.