Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add /headers endpoint to httpbin #388

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions e2e/tests/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,10 @@ func TestProxyUpstream(t *testing.T) {
t.Skip("HTTPBIN_PROTOCOL not set to http")
}

res := string(newClient(t, httpbin).GET("/header/via/").ExpectStatus(http.StatusNotFound).Body)
_, viaHeader, ok := strings.Cut(res, "=")
if !ok {
t.Fatalf("unexpected response: %q", res)
}

l := len("1.1 ")
filter := func(s string) string {
i := strings.LastIndex(s, "-")
if i < l {
t.Errorf("unexpected via header: %q", s)
return ""
}
return s[l:i]
}

viaHeader := newClient(t, httpbin).GET("/headers/").Header["Via"]
var success bool
for _, via := range strings.Split(viaHeader, ", ") {
if forwarder.UpstreamProxyServiceName == filter(via) {
for _, via := range viaHeader {
if strings.Contains(via, forwarder.UpstreamProxyServiceName) {
success = true
}
}
Expand Down
9 changes: 9 additions & 0 deletions utils/httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Handler() http.Handler {
m.HandleFunc("/ws/echo", wsEcho)
m.HandleFunc("/ws.html", wsHTML)
m.HandleFunc("/header/", headerHandler)
m.HandleFunc("/headers/", headersHandler)
return m
}

Expand Down Expand Up @@ -149,3 +150,11 @@ func headerHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("invalid header: " + k + "=" + vv))
}
}

func headersHandler(w http.ResponseWriter, r *http.Request) {
for k, vv := range r.Header {
for _, v := range vv {
w.Header().Add(k, v)
}
}
}