summaryrefslogtreecommitdiff
path: root/wrap_test.go
diff options
context:
space:
mode:
authorAlexander <a.kiryukhin@vk.team>2024-06-11 14:12:30 +0300
committerAlexander <a.kiryukhin@vk.team>2024-06-11 14:12:30 +0300
commit88a6b1cb628e4e334068e861ac5fb56274d88845 (patch)
tree312f2a3a7182b6e2fc45e87ec71ddde9861c77f8 /wrap_test.go
InitialHEADmaster
Diffstat (limited to 'wrap_test.go')
-rw-r--r--wrap_test.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/wrap_test.go b/wrap_test.go
new file mode 100644
index 0000000..31b831e
--- /dev/null
+++ b/wrap_test.go
@@ -0,0 +1,45 @@
+package muxtool
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+)
+
+func ExampleWrap() {
+ rr := httptest.NewRecorder()
+
+ // Sample request
+ req := reqHello{
+ Name: "NeonXP",
+ }
+ b, _ := json.Marshal(req)
+ request, _ := http.NewRequest(http.MethodPost, "/hello", bytes.NewReader(b))
+
+ // Handler
+ mux := http.NewServeMux()
+ // Handle wrapped `handleHello(context.Context, *reqHello) (*respHello, error)`
+ mux.Handle("POST /hello", Wrap(handleHello))
+
+ mux.ServeHTTP(rr, request)
+
+ fmt.Println(rr.Body.String())
+ // Output: {"message":"Hello, NeonXP!"}
+}
+
+type reqHello struct {
+ Name string `json:"name"`
+}
+
+type respHello struct {
+ Message string `json:"message"`
+}
+
+func handleHello(ctx context.Context, req *reqHello) (*respHello, error) {
+ return &respHello{
+ Message: fmt.Sprintf("Hello, %s!", req.Name),
+ }, nil
+}