Skip to content

Commit f1f2de9

Browse files
authored
feat: add /dump/request endpoint (#109)
1 parent 3a21fd7 commit f1f2de9

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

httpbin/handlers.go

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"net/http"
10+
"net/http/httputil"
1011
"net/url"
1112
"sort"
1213
"strconv"
@@ -1013,6 +1014,20 @@ func (h *HTTPBin) Base64(w http.ResponseWriter, r *http.Request) {
10131014
writeResponse(w, http.StatusOK, "text/plain", result)
10141015
}
10151016

1017+
// DumpRequest - returns the given request in its HTTP/1.x wire representation.
1018+
// The returned representation is an approximation only;
1019+
// some details of the initial request are lost while parsing it into
1020+
// an http.Request. In particular, the order and case of header field
1021+
// names are lost.
1022+
func (h *HTTPBin) DumpRequest(w http.ResponseWriter, r *http.Request) {
1023+
dump, err := httputil.DumpRequest(r, true)
1024+
if err != nil {
1025+
http.Error(w, err.Error(), http.StatusInternalServerError)
1026+
return
1027+
}
1028+
w.Write(dump)
1029+
}
1030+
10161031
// JSON - returns a sample json
10171032
func (h *HTTPBin) JSON(w http.ResponseWriter, r *http.Request) {
10181033
w.Header().Set("Content-Type", jsonContentType)

httpbin/handlers_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -2840,6 +2840,19 @@ func TestBase64(t *testing.T) {
28402840
}
28412841
}
28422842

2843+
func TestDumpRequest(t *testing.T) {
2844+
t.Parallel()
2845+
r, _ := http.NewRequest("GET", "/dump/request?foo=bar", nil)
2846+
r.Host = "test-host"
2847+
r.Header.Set("x-test-header2", "Test-Value2")
2848+
r.Header.Set("x-test-header1", "Test-Value1")
2849+
w := httptest.NewRecorder()
2850+
app.ServeHTTP(w, r)
2851+
2852+
assertContentType(t, w, "text/plain; charset=utf-8")
2853+
assertBodyEquals(t, w, "GET /dump/request?foo=bar HTTP/1.1\r\nHost: test-host\r\nX-Test-Header1: Test-Value1\r\nX-Test-Header2: Test-Value2\r\n\r\n")
2854+
}
2855+
28432856
func TestJSON(t *testing.T) {
28442857
t.Parallel()
28452858
r, _ := http.NewRequest("GET", "/json", nil)

httpbin/httpbin.go

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ func (h *HTTPBin) Handler() http.Handler {
145145
mux.HandleFunc("/uuid", h.UUID)
146146
mux.HandleFunc("/base64/", h.Base64)
147147

148+
mux.HandleFunc("/dump/request", h.DumpRequest)
149+
148150
// existing httpbin endpoints that we do not support
149151
mux.HandleFunc("/brotli", notImplementedHandler)
150152

httpbin/static/index.html

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)