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

add missing Accept header in AggregatorClient::get_config #358

Merged
merged 1 commit into from
Jul 31, 2023
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
1 change: 1 addition & 0 deletions src/clients/aggregator_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl AggregatorClient {
client
.get(base_url)
.with_header(KnownHeaderName::Authorization, format!("Bearer {token}"))
.with_header(KnownHeaderName::Accept, CONTENT_TYPE)
.success_or_client_error()
.await?
.response_json()
Expand Down
124 changes: 117 additions & 7 deletions tests/aggregator_client.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,139 @@
use divviup_api::api_mocks::aggregator_api;
use divviup_api::{
api_mocks::aggregator_api::{self, BAD_BEARER_TOKEN},
clients::AggregatorClient,
};
use test_support::{assert_eq, test, *};
use trillium::Handler;

#[test(harness = set_up)]
async fn get_task_ids(app: DivviupApi) -> TestResult {
#[test(harness = with_client_logs)]
async fn get_task_ids(app: DivviupApi, client_logs: ClientLogs) -> TestResult {
let aggregator = fixtures::aggregator(&app, None).await;
let client = aggregator.client(app.config().client.clone());
let task_ids = client.get_task_ids().await?;
assert_eq!(task_ids.len(), 25); // two pages of 10 plus a final page of 5

let logs = client_logs.logs();
assert!(logs.iter().all(|log| {
log.request_headers
.get_str(KnownHeaderName::Accept)
.unwrap()
== "application/vnd.janus.aggregator+json;version=0.1"
}));

assert!(logs.iter().all(|log| {
log.request_headers
.get_str(KnownHeaderName::Authorization)
.unwrap()
== &format!("Bearer {}", aggregator.bearer_token)
}));

let queries = logs
.iter()
.map(|log| log.url.query().clone())
.collect::<Vec<_>>();
assert_eq!(
&queries,
&[
None,
Some("pagination_token=second"),
Some("pagination_token=last")
]
);

Ok(())
}

#[test(harness = set_up)]
async fn get_task_metrics(app: DivviupApi) -> TestResult {
#[test(harness = with_client_logs)]
async fn get_task_metrics(app: DivviupApi, client_logs: ClientLogs) -> TestResult {
let aggregator = fixtures::aggregator(&app, None).await;
let client = aggregator.client(app.config().client.clone());
assert!(client.get_task_metrics("fake-task-id").await.is_ok());

let log = client_logs.last();
assert_eq!(
log.request_headers
.get_str(KnownHeaderName::Accept)
.unwrap(),
"application/vnd.janus.aggregator+json;version=0.1"
);
assert_eq!(
log.request_headers
.get_str(KnownHeaderName::Authorization)
.unwrap(),
&format!("Bearer {}", aggregator.bearer_token)
);

assert_eq!(
log.url.as_ref(),
&format!("{}tasks/fake-task-id/metrics", aggregator.api_url.as_ref())
);

Ok(())
}

#[test(harness = set_up)]
async fn delete_task(app: DivviupApi) -> TestResult {
#[test(harness = with_client_logs)]
async fn delete_task(app: DivviupApi, client_logs: ClientLogs) -> TestResult {
let aggregator = fixtures::aggregator(&app, None).await;
let client = aggregator.client(app.config().client.clone());
assert!(client.delete_task("fake-task-id").await.is_ok());

let log = client_logs.last();
assert_eq!(
log.request_headers
.get_str(KnownHeaderName::Accept)
.unwrap(),
"application/vnd.janus.aggregator+json;version=0.1"
);
assert_eq!(
log.request_headers
.get_str(KnownHeaderName::Authorization)
.unwrap(),
&format!("Bearer {}", aggregator.bearer_token)
);

assert_eq!(
log.url.as_ref(),
&format!("{}tasks/fake-task-id", aggregator.api_url.as_ref())
);

Ok(())
}

#[test(harness = with_client_logs)]
async fn get_config(app: DivviupApi, client_logs: ClientLogs) -> TestResult {
AggregatorClient::get_config(
app.config().client.clone(),
"https://aggregator.api.url".parse().unwrap(),
"token",
)
.await?;
let log = client_logs.last();
assert_eq!(
log.request_headers
.get_str(KnownHeaderName::Accept)
.unwrap(),
"application/vnd.janus.aggregator+json;version=0.1"
);
assert_eq!(
log.request_headers
.get_str(KnownHeaderName::Authorization)
.unwrap(),
"Bearer token"
);

assert_eq!(log.url.as_ref(), "https://aggregator.api.url/");
Ok(())
}

#[test(harness = set_up)]
async fn get_config_bad_token(app: DivviupApi) -> TestResult {
assert!(AggregatorClient::get_config(
app.config().client.clone(),
"https://aggregator.api.url".parse().unwrap(),
BAD_BEARER_TOKEN,
)
.await
.is_err());
Ok(())
}

Expand Down
Loading