Skip to content

Commit

Permalink
Add support for rfc7231 methods to http monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioalvap committed Dec 10, 2024
1 parent da6822b commit d4d0de4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions heartbeat/docs/monitors/monitor-http.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ Example configuration:

Under `check.request`, specify these options:

*`method`*:: The HTTP method to use. Valid values are `"HEAD"`, `"GET"`, `"POST"` and
`"OPTIONS"`.
*`method`*:: The HTTP method to use. Valid values are `"HEAD"`, `"GET"`, `"POST"`, `"PUT"`, `"DELETE"`, `"CONNECT"`,
`"TRACE"` and `"OPTIONS"`.
*`headers`*:: A dictionary of additional HTTP headers to send. By default heartbeat
will set the 'User-Agent' header to identify itself.
*`body`*:: Optional request body content.
Expand Down
3 changes: 2 additions & 1 deletion heartbeat/monitors/active/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http

import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -137,7 +138,7 @@ func (r *responseConfig) Validate() error {
// Validate validates of the requestParameters object is valid or not
func (r *requestParameters) Validate() error {
switch strings.ToUpper(r.Method) {
case "HEAD", "GET", "POST", "OPTIONS":
case http.MethodOptions, http.MethodHead, http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodConnect, http.MethodTrace:
default:
return fmt.Errorf("HTTP method '%v' not supported", r.Method)
}
Expand Down
32 changes: 22 additions & 10 deletions heartbeat/tests/system/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class BaseTest(TestCase):
def setUpClass(self):
self.beat_name = "heartbeat"
self.beat_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../"))
os.path.join(os.path.dirname(__file__), "../../")
)
super(BaseTest, self).setUpClass()

def start_server(self, content, status_code, **kwargs):
class HTTPHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(status_code)
self.send_header('Content-Type', 'application/json')
self.send_header("Content-Type", "application/json")
self.end_headers()
if "write_delay" in kwargs:
sleep(float(kwargs["write_delay"]))
Expand All @@ -30,16 +31,19 @@ def do_GET(self):
class HTTPHandlerEnabledOPTIONS(HTTPHandler):
def do_OPTIONS(self):
self.send_response(status_code)
self.send_header('Access-Control-Allow-Credentials', 'true')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'HEAD, GET, POST, OPTIONS')
self.send_header("Access-Control-Allow-Credentials", "true")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header(
"Access-Control-Allow-Methods",
"HEAD, GET, POST, OPTIONS, PUT, DELETE, CONNECT, TRACE",
)
self.end_headers()

# initialize http server based on if it needs to support OPTIONS method
server = http.server.HTTPServer(('localhost', 0), HTTPHandler)
server = http.server.HTTPServer(("localhost", 0), HTTPHandler)
# setup enable_options_method as False if it's not set
if kwargs.get("enable_options_method", False):
server = http.server.HTTPServer(('localhost', 0), HTTPHandlerEnabledOPTIONS)
server = http.server.HTTPServer(("localhost", 0), HTTPHandlerEnabledOPTIONS)

thread = threading.Thread(target=server.serve_forever)
thread.start()
Expand All @@ -54,7 +58,11 @@ def http_cfg(id, url):
schedule: "@every 1s"
timeout: 3s
urls: ["{url}"]
"""[1:-1].format(id=id, url=url)
"""[
1:-1
].format(
id=id, url=url
)

@staticmethod
def tcp_cfg(*hosts):
Expand All @@ -64,13 +72,17 @@ def tcp_cfg(*hosts):
schedule: "@every 1s"
timeout: 3s
hosts: [{host_str}]
"""[1:-1].format(host_str=host_str)
"""[
1:-1
].format(
host_str=host_str
)

def last_output_line(self):
return self.read_output()[-1]

def write_dyn_config(self, filename, cfg):
with open(self.monitors_dir() + filename, 'w') as f:
with open(self.monitors_dir() + filename, "w") as f:
f.write(cfg)

def monitors_dir(self):
Expand Down

0 comments on commit d4d0de4

Please sign in to comment.