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
a174dfd0
Commit
a174dfd0
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Discreet response types; break apart main.go
parent
664f89cb
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
handlers.go
+89
-0
89 additions, 0 deletions
handlers.go
handlers_test.go
+9
-9
9 additions, 9 deletions
handlers_test.go
helpers.go
+3
-9
3 additions, 9 deletions
helpers.go
main.go
+0
-105
0 additions, 105 deletions
main.go
types.go
+38
-0
38 additions, 0 deletions
types.go
with
139 additions
and
123 deletions
handlers.go
0 → 100644
+
89
−
0
View file @
a174dfd0
package
main
import
(
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
)
// Index must be wrapped by the withTemplates middleware before it can be used
func
index
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
t
*
template
.
Template
)
{
t
=
t
.
Lookup
(
"index.html"
)
if
t
==
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error looking up index.html"
),
http
.
StatusInternalServerError
)
return
}
t
.
Execute
(
w
,
nil
)
}
// FormsPost must be wrapped by withTemplates middleware before it can be used
func
formsPost
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
t
*
template
.
Template
)
{
t
=
t
.
Lookup
(
"forms-post.html"
)
if
t
==
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error looking up index.html"
),
http
.
StatusInternalServerError
)
return
}
t
.
Execute
(
w
,
nil
)
}
func
get
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
args
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing query params: %s"
,
err
),
http
.
StatusBadRequest
)
return
}
resp
:=
&
getResponse
{
Args
:
args
,
Headers
:
r
.
Header
,
Origin
:
getOrigin
(
r
),
URL
:
getURL
(
r
),
}
body
,
_
:=
json
.
Marshal
(
resp
)
writeJSON
(
w
,
body
,
http
.
StatusOK
)
}
func
post
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
args
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing query params: %s"
,
err
),
http
.
StatusBadRequest
)
return
}
resp
:=
&
bodyResponse
{
Args
:
args
,
Headers
:
r
.
Header
,
Origin
:
getOrigin
(
r
),
URL
:
getURL
(
r
),
}
err
=
parseBody
(
w
,
r
,
resp
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing request body: %s"
,
err
),
http
.
StatusBadRequest
)
}
body
,
_
:=
json
.
Marshal
(
resp
)
writeJSON
(
w
,
body
,
http
.
StatusOK
)
}
func
ip
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
body
,
_
:=
json
.
Marshal
(
&
ipResponse
{
Origin
:
getOrigin
(
r
),
})
writeJSON
(
w
,
body
,
http
.
StatusOK
)
}
func
userAgent
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
body
,
_
:=
json
.
Marshal
(
&
userAgentResponse
{
UserAgent
:
r
.
Header
.
Get
(
"User-Agent"
),
})
writeJSON
(
w
,
body
,
http
.
StatusOK
)
}
func
headers
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
body
,
_
:=
json
.
Marshal
(
&
headersResponse
{
Headers
:
r
.
Header
,
})
writeJSON
(
w
,
body
,
http
.
StatusOK
)
}
This diff is collapsed.
Click to expand it.
main
_test.go
→
handlers
_test.go
+
9
−
9
View file @
a174dfd0
...
@@ -42,7 +42,7 @@ func TestGet__Basic(t *testing.T) {
...
@@ -42,7 +42,7 @@ func TestGet__Basic(t *testing.T) {
t
.
Fatalf
(
"expected status code 200, got %d"
,
w
.
Code
)
t
.
Fatalf
(
"expected status code 200, got %d"
,
w
.
Code
)
}
}
var
resp
*
Resp
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
@@ -160,7 +160,7 @@ func TestGet__XForwardedProto(t *testing.T) {
...
@@ -160,7 +160,7 @@ func TestGet__XForwardedProto(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
Resp
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
@@ -178,7 +178,7 @@ func TestIP(t *testing.T) {
...
@@ -178,7 +178,7 @@ func TestIP(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
IP
Resp
var
resp
*
ip
Resp
onse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
@@ -195,7 +195,7 @@ func TestUserAgent(t *testing.T) {
...
@@ -195,7 +195,7 @@ func TestUserAgent(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
U
serAgentResp
var
resp
*
u
serAgentResp
onse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
@@ -215,7 +215,7 @@ func TestHeaders(t *testing.T) {
...
@@ -215,7 +215,7 @@ func TestHeaders(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
H
eadersResp
var
resp
*
h
eadersResp
onse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
@@ -237,7 +237,7 @@ func TestPost__EmptyBody(t *testing.T) {
...
@@ -237,7 +237,7 @@ func TestPost__EmptyBody(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
Resp
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
@@ -262,7 +262,7 @@ func TestPost__FormEncodedBody(t *testing.T) {
...
@@ -262,7 +262,7 @@ func TestPost__FormEncodedBody(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
Resp
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %#v from JSON: %s"
,
w
.
Body
.
String
(),
err
)
t
.
Fatalf
(
"failed to unmarshal body %#v from JSON: %s"
,
w
.
Body
.
String
(),
err
)
...
@@ -295,7 +295,7 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) {
...
@@ -295,7 +295,7 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
Resp
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
@@ -332,7 +332,7 @@ func TestPost__JSON(t *testing.T) {
...
@@ -332,7 +332,7 @@ func TestPost__JSON(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
app
()
.
ServeHTTP
(
w
,
r
)
app
()
.
ServeHTTP
(
w
,
r
)
var
resp
*
Resp
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
...
...
This diff is collapsed.
Click to expand it.
helpers.go
+
3
−
9
View file @
a174dfd0
...
@@ -47,14 +47,8 @@ func getURL(r *http.Request) string {
...
@@ -47,14 +47,8 @@ func getURL(r *http.Request) string {
return
u
.
String
()
return
u
.
String
()
}
}
func
writeResponse
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
Resp
)
{
func
writeJSON
(
w
http
.
ResponseWriter
,
body
[]
byte
,
status
int
)
{
resp
.
Origin
=
getOrigin
(
r
)
w
.
WriteHeader
(
status
)
resp
.
URL
=
getURL
(
r
)
body
,
_
:=
json
.
Marshal
(
resp
)
writeJSON
(
w
,
body
)
}
func
writeJSON
(
w
http
.
ResponseWriter
,
body
[]
byte
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; encoding=utf-8"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; encoding=utf-8"
)
w
.
Write
(
body
)
w
.
Write
(
body
)
}
}
...
@@ -62,7 +56,7 @@ func writeJSON(w http.ResponseWriter, body []byte) {
...
@@ -62,7 +56,7 @@ func writeJSON(w http.ResponseWriter, body []byte) {
// parseBody handles parsing a request body into our standard API response,
// parseBody handles parsing a request body into our standard API response,
// taking care to only consume the request body once based on the Content-Type
// taking care to only consume the request body once based on the Content-Type
// of the request. The given Resp will be updated.
// of the request. The given Resp will be updated.
func
parseBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
Resp
)
error
{
func
parseBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
bodyResponse
)
error
{
if
r
.
Body
==
nil
{
if
r
.
Body
==
nil
{
return
nil
return
nil
}
}
...
...
This diff is collapsed.
Click to expand it.
main.go
+
0
−
105
View file @
a174dfd0
package
main
package
main
import
(
import
(
"encoding/json"
"fmt"
"html/template"
"log"
"log"
"net/http"
"net/http"
"net/url"
)
)
// Resp is the standard JSON response from httpbin
type
Resp
struct
{
Args
url
.
Values
`json:"args"`
Headers
http
.
Header
`json:"headers"`
Origin
string
`json:"origin"`
URL
string
`json:"url"`
Data
[]
byte
`json:"data,omitempty"`
Files
map
[
string
][]
string
`json:"files,omitempty"`
Form
map
[
string
][]
string
`json:"form,omitempty"`
JSON
interface
{}
`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"`
}
// Max size of a request body we'll handle
// Max size of a request body we'll handle
const
maxMemory
=
1024
*
1024
*
5
+
1
const
maxMemory
=
1024
*
1024
*
5
+
1
// Index must be wrapped by the withTemplates middleware before it can be used
func
index
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
t
*
template
.
Template
)
{
t
=
t
.
Lookup
(
"index.html"
)
if
t
==
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error looking up index.html"
),
http
.
StatusInternalServerError
)
return
}
t
.
Execute
(
w
,
nil
)
}
// FormsPost must be wrapped by withTemplates middleware before it can be used
func
formsPost
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
t
*
template
.
Template
)
{
t
=
t
.
Lookup
(
"forms-post.html"
)
if
t
==
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error looking up index.html"
),
http
.
StatusInternalServerError
)
return
}
t
.
Execute
(
w
,
nil
)
}
func
get
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
args
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing query params: %s"
,
err
),
http
.
StatusBadRequest
)
return
}
resp
:=
&
Resp
{
Args
:
args
,
Headers
:
r
.
Header
,
}
writeResponse
(
w
,
r
,
resp
)
}
func
post
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
args
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing query params: %s"
,
err
),
http
.
StatusBadRequest
)
return
}
resp
:=
&
Resp
{
Args
:
args
,
Headers
:
r
.
Header
,
}
err
=
parseBody
(
w
,
r
,
resp
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing request body: %s"
,
err
),
http
.
StatusBadRequest
)
}
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
{
func
app
()
http
.
Handler
{
h
:=
http
.
NewServeMux
()
h
:=
http
.
NewServeMux
()
templateWrapper
:=
withTemplates
(
"templates/*.html"
)
templateWrapper
:=
withTemplates
(
"templates/*.html"
)
...
...
This diff is collapsed.
Click to expand it.
types.go
0 → 100644
+
38
−
0
View file @
a174dfd0
package
main
import
(
"net/http"
"net/url"
)
type
headersResponse
struct
{
Headers
http
.
Header
`json:"headers"`
}
type
ipResponse
struct
{
Origin
string
`json:"origin"`
}
type
userAgentResponse
struct
{
UserAgent
string
`json:"user-agent"`
}
type
getResponse
struct
{
Args
url
.
Values
`json:"args"`
Headers
http
.
Header
`json:"headers"`
Origin
string
`json:"origin"`
URL
string
`json:"url"`
}
// A generic response for any incoming request that might contain a body
type
bodyResponse
struct
{
Args
url
.
Values
`json:"args"`
Headers
http
.
Header
`json:"headers"`
Origin
string
`json:"origin"`
URL
string
`json:"url"`
Data
[]
byte
`json:"data"`
Files
map
[
string
][]
string
`json:"files"`
Form
map
[
string
][]
string
`json:"form"`
JSON
interface
{}
`json:"json"`
}
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