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
bd122ea6
Unverified
Commit
bd122ea6
authored
2 years ago
by
Will McCutchen
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Extract inline vars from the status handler (#93)
parent
7d0621ff
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
httpbin/handlers.go
+47
-46
47 additions, 46 deletions
httpbin/handlers.go
with
47 additions
and
46 deletions
httpbin/handlers.go
+
47
−
46
View file @
bd122ea6
...
...
@@ -14,14 +14,6 @@ import (
"github.com/mccutchen/go-httpbin/v2/httpbin/digest"
)
var
acceptedMediaTypes
=
[]
string
{
"image/webp"
,
"image/svg+xml"
,
"image/jpeg"
,
"image/png"
,
"image/"
,
}
func
notImplementedHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
http
.
Error
(
w
,
"Not implemented"
,
http
.
StatusNotImplemented
)
}
...
...
@@ -151,36 +143,29 @@ func (h *HTTPBin) Headers(w http.ResponseWriter, r *http.Request) {
writeJSON
(
w
,
body
,
http
.
StatusOK
)
}
// Status responds with the specified status code. TODO: support random choice
// from multiple, optionally weighted status codes.
func
(
h
*
HTTPBin
)
Status
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
if
len
(
parts
)
!=
3
{
http
.
Error
(
w
,
"Not found"
,
http
.
StatusNotFound
)
return
}
code
,
err
:=
strconv
.
Atoi
(
parts
[
2
])
if
err
!=
nil
{
http
.
Error
(
w
,
"Invalid status"
,
http
.
StatusBadRequest
)
return
}
type
statusCase
struct
{
headers
map
[
string
]
string
body
[]
byte
}
type
statusCase
struct
{
headers
map
[
string
]
string
body
[]
byte
}
redirectHeaders
:=
&
statusCase
{
var
(
statusRedirectHeaders
=
&
statusCase
{
headers
:
map
[
string
]
string
{
"Location"
:
"/redirect/1"
,
},
}
notAcceptableBody
,
_
:=
jsonMarshalNoEscape
(
map
[
string
]
interface
{}{
"message"
:
"Client did not request a supported media type"
,
"accept"
:
acceptedMediaTypes
,
})
http300body
:=
[]
byte
(
`<!doctype html>
statusNotAcceptableBody
=
[]
byte
(
`{
"message": "Client did not request a supported media type",
"accept": [
"image/webp",
"image/svg+xml",
"image/jpeg",
"image/png",
"image/"
]
}
`
)
statusHTTP300body
=
[]
byte
(
`<!doctype html>
<head>
<title>Multiple Choices</title>
</head>
...
...
@@ -192,7 +177,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
</body>
</html>`
)
http
308
b
ody
:
=
[]
byte
(
`<!doctype html>
statusHTTP
308
B
ody
=
[]
byte
(
`<!doctype html>
<head>
<title>Permanent Redirect</title>
</head>
...
...
@@ -200,20 +185,20 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
</body>
</html>`
)
specialCases
:
=
map
[
int
]
*
statusCase
{
s
tatusS
pecialCases
=
map
[
int
]
*
statusCase
{
300
:
{
body
:
http
300body
,
body
:
statusHTTP
300body
,
headers
:
map
[
string
]
string
{
"Location"
:
"/image/jpeg"
,
},
},
301
:
r
edirectHeaders
,
302
:
r
edirectHeaders
,
303
:
r
edirectHeaders
,
305
:
r
edirectHeaders
,
307
:
r
edirectHeaders
,
301
:
statusR
edirectHeaders
,
302
:
statusR
edirectHeaders
,
303
:
statusR
edirectHeaders
,
305
:
statusR
edirectHeaders
,
307
:
statusR
edirectHeaders
,
308
:
{
body
:
http
308
b
ody
,
body
:
statusHTTP
308
B
ody
,
headers
:
map
[
string
]
string
{
"Location"
:
"/image/jpeg"
,
},
...
...
@@ -230,7 +215,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
},
},
406
:
{
body
:
n
otAcceptableBody
,
body
:
statusN
otAcceptableBody
,
headers
:
map
[
string
]
string
{
"Content-Type"
:
jsonContentType
,
},
...
...
@@ -247,8 +232,23 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
},
},
}
)
if
specialCase
,
ok
:=
specialCases
[
code
];
ok
{
// Status responds with the specified status code. TODO: support random choice
// from multiple, optionally weighted status codes.
func
(
h
*
HTTPBin
)
Status
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
if
len
(
parts
)
!=
3
{
http
.
Error
(
w
,
"Not found"
,
http
.
StatusNotFound
)
return
}
code
,
err
:=
strconv
.
Atoi
(
parts
[
2
])
if
err
!=
nil
{
http
.
Error
(
w
,
"Invalid status"
,
http
.
StatusBadRequest
)
return
}
if
specialCase
,
ok
:=
statusSpecialCases
[
code
];
ok
{
for
key
,
val
:=
range
specialCase
.
headers
{
w
.
Header
()
.
Set
(
key
,
val
)
}
...
...
@@ -256,9 +256,10 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
if
specialCase
.
body
!=
nil
{
w
.
Write
(
specialCase
.
body
)
}
}
else
{
w
.
WriteHeader
(
code
)
return
}
w
.
WriteHeader
(
code
)
}
// Unstable - returns 500, sometimes
...
...
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