Skip to content
Snippets Groups Projects
Commit 14defe2f authored by Will McCutchen's avatar Will McCutchen
Browse files

Add /ip, /headers, and /user-agent handlers

parent 8dffcd5c
No related branches found
No related tags found
No related merge requests found
......@@ -2,8 +2,6 @@ package main
import (
"encoding/json"
"log"
"net/http"
"net/url"
)
......@@ -50,11 +48,7 @@ func getURL(r *http.Request) string {
func writeResponse(w http.ResponseWriter, r *http.Request, resp *Resp) {
resp.Origin = getOrigin(r)
resp.URL = getURL(r)
body, err := json.Marshal(resp)
if err != nil {
log.Printf("error marshalling %v as JSON: %s", resp, err)
}
body, _ := json.Marshal(resp)
writeJSON(w, body)
}
......
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
......@@ -21,6 +22,21 @@ type Resp struct {
JSON map[string][]string `json:"json,omitempty"`
}
// IPResp is the response for the /ip endpoint
type IPResp struct {
Origin string `json:"origin"`
}
// HeadersResp is the response for the /headers endpoint
type HeadersResp struct {
Headers http.Header `json:"headers"`
}
// UserAgentResp is the response for the /user-agent endpoint
type UserAgentResp struct {
UserAgent string `json:"user-agent"`
}
func index(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseGlob("templates/*.html")
if err != nil {
......@@ -44,10 +60,34 @@ func get(w http.ResponseWriter, r *http.Request) {
writeResponse(w, r, resp)
}
func ip(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(&IPResp{
Origin: getOrigin(r),
})
writeJSON(w, body)
}
func userAgent(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(&UserAgentResp{
UserAgent: r.Header.Get("User-Agent"),
})
writeJSON(w, body)
}
func headers(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(&HeadersResp{
Headers: r.Header,
})
writeJSON(w, body)
}
func app() http.Handler {
h := http.NewServeMux()
h.HandleFunc("/", index)
h.HandleFunc("/get", get)
h.HandleFunc("/ip", ip)
h.HandleFunc("/user-agent", userAgent)
h.HandleFunc("/headers", headers)
return logger(cors(h))
}
......
......@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
......@@ -148,3 +149,63 @@ func TestGet__XForwardedProto(t *testing.T) {
}
}
}
func TestIP(t *testing.T) {
r, _ := http.NewRequest("GET", "/ip", nil)
r.RemoteAddr = "192.168.0.100"
w := httptest.NewRecorder()
app().ServeHTTP(w, r)
var resp *IPResp
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err)
}
if resp.Origin != r.RemoteAddr {
t.Fatalf("%#v != %#v", resp.Origin, r.RemoteAddr)
}
}
func TestUserAgent(t *testing.T) {
r, _ := http.NewRequest("GET", "/user-agent", nil)
r.Header.Set("User-Agent", "test")
w := httptest.NewRecorder()
app().ServeHTTP(w, r)
var resp *UserAgentResp
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err)
}
if resp.UserAgent != "test" {
t.Fatalf("%#v != \"test\"", resp.UserAgent)
}
}
func TestHeaders(t *testing.T) {
r, _ := http.NewRequest("GET", "/headers", nil)
r.Header.Set("User-Agent", "test")
r.Header.Set("Foo-Header", "foo")
r.Header.Add("Bar-Header", "bar1")
r.Header.Add("Bar-Header", "bar2")
w := httptest.NewRecorder()
app().ServeHTTP(w, r)
var resp *HeadersResp
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err)
}
for k, expectedValues := range r.Header {
values, ok := resp.Headers[http.CanonicalHeaderKey(k)]
if !ok {
t.Fatalf("expected header %#v in response", k)
}
if !reflect.DeepEqual(expectedValues, values) {
t.Fatalf("%#v != %#v", values, expectedValues)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment