Skip to content

Commit 41f8593

Browse files
authored
Add a test-client test to validate the unauthorized http status code (#79)
1 parent c78005d commit 41f8593

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test-client/src/tester.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ impl Tester {
124124
.await
125125
.context("nonexistent_resource_tests")?;
126126

127+
self.unauthorized_status_test()
128+
.await
129+
.context("unauthorized_status_test")?;
130+
127131
let dwight =
128132
self.create_user_tests().await.context("create_user_tests")?;
129133
let jim = self.create_jim_user().await.context("create_jim_user")?;
@@ -293,6 +297,38 @@ impl Tester {
293297
Ok(())
294298
}
295299

300+
async fn unauthorized_status_test(&self) -> anyhow::Result<()> {
301+
let body = json!({
302+
"userName": "amartin",
303+
"externalId": "[email protected]",
304+
});
305+
306+
// Don't call `self.post()` so that we can maniuplate the headers.
307+
let mut headers = self.headers.clone();
308+
let Some(_old_bearer) = headers.insert(
309+
header::AUTHORIZATION,
310+
header::HeaderValue::from_str("Bearer this-is-invalid")?,
311+
) else {
312+
// The client was constructed without a bearer token so this test is
313+
// pointless.
314+
return Ok(());
315+
};
316+
317+
let result = self
318+
.client
319+
.post(format!("{}/Users", self.url))
320+
.json(&body)
321+
.headers(headers)
322+
.send()
323+
.await?;
324+
325+
if result.status() != StatusCode::UNAUTHORIZED {
326+
bail!("POST to /Users returned status code {}", result.status());
327+
}
328+
329+
Ok(())
330+
}
331+
296332
async fn create_user_tests(&self) -> anyhow::Result<User> {
297333
let body = json!({
298334
"userName": "dschrute",

0 commit comments

Comments
 (0)