aboutsummaryrefslogtreecommitdiff
path: root/run_response.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-03-27 21:57:57 +0700
committerShulhan <ms@kilabit.info>2021-03-27 21:57:57 +0700
commiteaffbf8780e370ecec4e9bbc534694b8a815a86a (patch)
tree53d5f36d7059fdcf58824ab36cad103dc0d2fd08 /run_response.go
parenta098c7d1641299e14ddfa88800922c128feff2d0 (diff)
downloadgorankusu-eaffbf8780e370ecec4e9bbc534694b8a815a86a.tar.xz
all: change the Run result to return dump of HTTP request and response
Previously, we display the actual response of the HTTP target only on the user interface. This changes make the Run function to display the actual HTTP request and response that being send and received on HTTP target.
Diffstat (limited to 'run_response.go')
-rw-r--r--run_response.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/run_response.go b/run_response.go
new file mode 100644
index 0000000..de12105
--- /dev/null
+++ b/run_response.go
@@ -0,0 +1,44 @@
+// Copyright 2021, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package trunks
+
+import (
+ "fmt"
+ "net/http"
+ "net/http/httputil"
+)
+
+//
+// RunResponse contains the raw request and response when running HTTP or
+// WebSocket target.
+//
+type RunResponse struct {
+ DumpRequest []byte
+ DumpResponse []byte
+}
+
+//
+// SetHttpResponse dump the HTTP request including body into the DumpRequest
+// field.
+//
+func (rres *RunResponse) SetHttpRequest(req *http.Request) (err error) {
+ rres.DumpRequest, err = httputil.DumpRequest(req, true)
+ if err != nil {
+ return fmt.Errorf("SetHttpRequest: %w", err)
+ }
+ return nil
+}
+
+//
+// SetHttpResponse dump the HTTP response including body into the DumpResponse
+// field.
+//
+func (rres *RunResponse) SetHttpResponse(res *http.Response) (err error) {
+ rres.DumpResponse, err = httputil.DumpResponse(res, true)
+ if err != nil {
+ return fmt.Errorf("SetHttpResponse: %w", err)
+ }
+ return nil
+}