Skip to content

Commit

Permalink
refactor: Fixing code smell
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh committed Feb 11, 2021
1 parent 5fab58d commit 865be61
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions pkg/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,27 @@ func Handler() http.Handler {
})
}

func dumpRequest(r *http.Request) string {
var headers bytes.Buffer
for key, value := range r.Header {
headers.WriteString(fmt.Sprintf("%s: %s\n", key, strings.Join(value, ",")))
func getBufferContent(content map[string][]string) bytes.Buffer {
var buffer bytes.Buffer

for key, values := range content {
buffer.WriteString(fmt.Sprintf("%s: %s\n", key, strings.Join(values, ",")))
}

var params bytes.Buffer
for key, value := range r.URL.Query() {
headers.WriteString(fmt.Sprintf("%s: %s\n", key, strings.Join(value, ",")))
return buffer
}

func dumpRequest(r *http.Request) string {
parts := map[string]bytes.Buffer{
"Headers": getBufferContent(r.Header),
"Params": getBufferContent(r.URL.Query()),
}

var form bytes.Buffer
if err := r.ParseForm(); err != nil {
form.WriteString(err.Error())
} else {
for key, value := range r.PostForm {
form.WriteString(fmt.Sprintf("%s: %s\n", key, strings.Join(value, ",")))
}
parts["Form"] = getBufferContent(r.PostForm)
}

body, err := request.ReadBodyRequest(r)
Expand All @@ -56,19 +59,14 @@ func dumpRequest(r *http.Request) string {
r.URL.Path,
}

if headers.Len() != 0 {
outputPattern.WriteString("Headers\n%s\n")
outputData = append(outputData, headers.String())
}

if params.Len() != 0 {
outputPattern.WriteString("Params\n%s\n")
outputData = append(outputData, params.String())
}
for key, value := range parts {
if value.Len() == 0 {
continue
}

if form.Len() != 0 {
outputPattern.WriteString("Form\n%s\n")
outputData = append(outputData, form.String())
outputPattern.WriteString(key)
outputPattern.WriteString("\n%s\n")
outputData = append(outputData, value.String())
}

if len(body) != 0 {
Expand Down

0 comments on commit 865be61

Please sign in to comment.