Introduction
For the purposes of testing a Sentinel policy, some functions need to return expected values. Mocking the http get or post facilitates this and allows for testing in a controlled manner.
Prerequisites (if applicable)
- Please refer to Mocking on how to run Sentinel to mock data
Use Case
The following executable code can be found here in the Sentinel Playground
policy.sentinel
import "http"
req = http.request("https://wat.com").
with_header("thing", "foo").
with_body("why")
print(req)
print("******")
resp = http.post(req)
print(resp.body)
main = true
mock-http.sentinel
func request(url) {
return builder({
"url": url,
"headers": {},
"body": "",
})
}
func with_header(req) {
return func(name, value) {
req.headers[name] = value
return builder(req)
}
}
func with_body(req) {
return func(body) {
req["body"] = body
return builder(req)
}
}
func builder(req) {
return {
"url": req.url,
"headers": req.headers,
"body": req.body,
"with_header": with_header(req),
"with_body": with_body(req),
}
}
func post(req) {
return {
"status_code": 200,
"body": "mock post data",
}
}