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 1 commit
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.

13 changes: 13 additions & 0 deletions httpbin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -126,6 +127,18 @@ func (h *HTTPBin) IP(w http.ResponseWriter, r *http.Request) {
writeJSON(w, body, http.StatusOK)
}

func (h *HTTPBin) Hostname(w http.ResponseWriter, r *http.Request) {
hostname, err := os.Hostname()
uromahn marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
http.Error(w, "Error:"+err.Error(), http.StatusInternalServerError)
return
}
body, _ := json.Marshal(&hostnameResponse{
Hostname: hostname,
})
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 none-empty hostname: %q", resp.Hostname)
uromahn marked this conversation as resolved.
Show resolved Hide resolved
}
}

func TestUserAgent(t *testing.T) {
r, _ := http.NewRequest("GET", "/user-agent", nil)
r.Header.Set("User-Agent", "test")
Expand Down
5 changes: 5 additions & 0 deletions httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 @@ -132,6 +136,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
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.