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

adding method to return the hostname where httpbin is running #66

Closed
wants to merge 4 commits into from
Closed
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
26 changes: 13 additions & 13 deletions httpbin/assets/assets.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions httpbin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ func (h *HTTPBin) IP(w http.ResponseWriter, r *http.Request) {
writeJSON(w, body, http.StatusOK)
}

// Hostname returns the name of the host this program is running on
func (h *HTTPBin) Hostname(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(&hostnameResponse{
Hostname: h.HostnameStr,
})
writeJSON(w, body, http.StatusOK)
}

// UserAgent echoes the incoming User-Agent header
func (h *HTTPBin) UserAgent(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(&userAgentResponse{
Expand Down
18 changes: 18 additions & 0 deletions httpbin/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,24 @@ func TestIP(t *testing.T) {
}
}

func TestHostname(t *testing.T) {
// this cannot be really tested, except to verify that the call returns some hostname and not a HTTP error
r, _ := http.NewRequest("GET", "/hostname", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)

var resp *hostnameResponse
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err)
}

if len(resp.Hostname) == 0 {
t.Fatalf("expected non-empty hostname: %q", resp.Hostname)
}
}

func TestUserAgent(t *testing.T) {
r, _ := http.NewRequest("GET", "/user-agent", nil)
r.Header.Set("User-Agent", "test")
Expand Down
15 changes: 15 additions & 0 deletions httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpbin
import (
"net/http"
"net/url"
"os"
"time"
)

Expand All @@ -23,6 +24,10 @@ type ipResponse struct {
Origin string `json:"origin"`
}

type hostnameResponse struct {
Hostname string `json:"hostname"`
}

type userAgentResponse struct {
UserAgent string `json:"user-agent"`
}
Expand Down Expand Up @@ -99,6 +104,9 @@ type HTTPBin struct {

// Default parameter values
DefaultParams DefaultParams

// Name of the host we are running on
HostnameStr string
}

// DefaultParams defines default parameter values
Expand Down Expand Up @@ -132,6 +140,7 @@ func (h *HTTPBin) Handler() http.Handler {
mux.HandleFunc("/put", methods(h.RequestWithBody, "PUT"))

mux.HandleFunc("/ip", h.IP)
mux.HandleFunc("/hostname", h.Hostname)
mux.HandleFunc("/user-agent", h.UserAgent)
mux.HandleFunc("/headers", h.Headers)
mux.HandleFunc("/response-headers", h.ResponseHeaders)
Expand Down Expand Up @@ -224,6 +233,12 @@ func New(opts ...OptionFunc) *HTTPBin {
for _, opt := range opts {
opt(h)
}
// set the hostname string in the struct
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
h.HostnameStr = hostname
return h
}

Expand Down
1 change: 1 addition & 0 deletions static/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.