Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
14defe2f
Commit
14defe2f
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Add /ip, /headers, and /user-agent handlers
parent
8dffcd5c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
helpers.go
+1
-7
1 addition, 7 deletions
helpers.go
main.go
+40
-0
40 additions, 0 deletions
main.go
main_test.go
+61
-0
61 additions, 0 deletions
main_test.go
with
102 additions
and
7 deletions
helpers.go
+
1
−
7
View file @
14defe2f
...
...
@@ -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
)
}
...
...
This diff is collapsed.
Click to expand it.
main.go
+
40
−
0
View file @
14defe2f
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
))
}
...
...
This diff is collapsed.
Click to expand it.
main_test.go
+
61
−
0
View file @
14defe2f
...
...
@@ -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
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment