/
example_test.go
50 lines (42 loc) · 1.49 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.
package appsec_test
import (
"io"
"io/ioutil"
"net/http"
"github.com/labstack/echo/v4"
"gopkg.in/DataDog/dd-trace-go.v1/appsec"
echotrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/labstack/echo.v4"
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)
func customBodyParser(body io.ReadCloser) string {
b, err := ioutil.ReadAll(body)
if err != nil {
return "Ill formed http body"
}
return string(b)
}
// Monitor HTTP request parsed body
func ExampleMonitorParsedHTTPBody() {
mux := httptrace.NewServeMux()
mux.HandleFunc("/body", func(w http.ResponseWriter, r *http.Request) {
// Use the SDK to monitor the request's parsed body
appsec.MonitorParsedHTTPBody(r.Context(), customBodyParser(r.Body))
w.Write([]byte("Body monitored using AppSec SDK\n"))
})
http.ListenAndServe(":8080", mux)
}
// Monitor HTTP request parsed body with a framework customized context type
func ExampleMonitorParsedHTTPBodyCustomContext() {
r := echo.New()
r.Use(echotrace.Middleware())
r.POST("/body", func(c echo.Context) error {
// Use the SDK to monitor the request's parsed body
appsec.MonitorParsedHTTPBody(c.Request().Context(), customBodyParser(c.Request().Body))
return c.String(http.StatusOK, "Body monitored using AppSec SDK")
})
r.Start(":8080")
}