-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug in
inspect
implementation and add a test (#422)
- Loading branch information
Showing
6 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use { | ||
crate::{ | ||
common::{Test, TestResult}, | ||
viceroy_test, | ||
}, | ||
hyper::{Request, StatusCode}, | ||
}; | ||
|
||
viceroy_test!(upstream_sync, |is_component| { | ||
// Set up the test harness: | ||
let test = Test::using_fixture("inspect.wasm").adapt_component(is_component); | ||
|
||
// And send a request to exercise the hostcall: | ||
let resp = test | ||
.against(Request::post("/").body("Hello, Viceroy!").unwrap()) | ||
.await?; | ||
|
||
assert_eq!(resp.status(), StatusCode::OK); | ||
assert_eq!( | ||
resp.into_body().read_into_string().await?, | ||
"inspect result: waf_response=200, tags=[], decision_ms=0ms, verdict=Allow" | ||
); | ||
|
||
Ok(()) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use fastly::experimental::{inspect, InspectConfig, InspectError, InspectVerdict}; | ||
use fastly::handle::{BodyHandle, RequestHandle}; | ||
use fastly::http::{HeaderName, HeaderValue, Method, StatusCode}; | ||
use fastly::{Error, Request, Response}; | ||
|
||
#[fastly::main] | ||
fn main(mut req: Request) -> Result<Response, Error> { | ||
let (req, body) = req.into_handles(); | ||
let body = body.unwrap_or_else(BodyHandle::new); | ||
|
||
let inspectconf = InspectConfig::new(&req, &body) | ||
.corp("junichi-lab") | ||
.workspace("lab"); | ||
|
||
let resp = match inspect(inspectconf) { | ||
Ok(x) => { | ||
let body = format!( | ||
"inspect result: waf_response={}, tags={:?}, decision_ms={}ms, verdict={:?}", | ||
x.waf_response(), | ||
x.tags(), | ||
x.decision_ms().as_millis(), | ||
x.verdict() | ||
); | ||
|
||
Response::from_status(StatusCode::OK) | ||
.with_body_text_plain(&body) | ||
} | ||
Err(e) => { | ||
let body = format!("Error: {e:?}"); | ||
|
||
Response::from_status(StatusCode::BAD_REQUEST) | ||
.with_body_text_plain(&body) | ||
}, | ||
}; | ||
|
||
Ok(resp) | ||
} |