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
1462a07c
Commit
1462a07c
authored
7 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Add /redirect-to
parent
c49dd449
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
httpbin/handlers.go
+26
-0
26 additions, 0 deletions
httpbin/handlers.go
httpbin/handlers_test.go
+45
-0
45 additions, 0 deletions
httpbin/handlers_test.go
httpbin/httpbin.go
+2
-0
2 additions, 0 deletions
httpbin/httpbin.go
with
73 additions
and
0 deletions
httpbin/handlers.go
+
26
−
0
View file @
1462a07c
...
...
@@ -293,6 +293,32 @@ func (h *HTTPBin) AbsoluteRedirect(w http.ResponseWriter, r *http.Request) {
doRedirect
(
w
,
r
,
false
)
}
// RedirectTo responds with a redirect to a specific URL with an optional
// status code, which defaults to 302
func
(
h
*
HTTPBin
)
RedirectTo
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
q
:=
r
.
URL
.
Query
()
url
:=
q
.
Get
(
"url"
)
if
url
==
""
{
http
.
Error
(
w
,
"Missing URL"
,
http
.
StatusBadRequest
)
return
}
var
err
error
statusCode
:=
http
.
StatusFound
rawStatusCode
:=
q
.
Get
(
"status_code"
)
if
rawStatusCode
!=
""
{
statusCode
,
err
=
strconv
.
Atoi
(
q
.
Get
(
"status_code"
))
if
err
!=
nil
||
statusCode
<
300
||
statusCode
>
399
{
http
.
Error
(
w
,
"Invalid status code"
,
http
.
StatusBadRequest
)
return
}
}
w
.
Header
()
.
Set
(
"Location"
,
url
)
w
.
WriteHeader
(
statusCode
)
}
// Cookies responds with the cookies in the incoming request
func
(
h
*
HTTPBin
)
Cookies
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
resp
:=
cookiesResponse
{}
...
...
This diff is collapsed.
Click to expand it.
httpbin/handlers_test.go
+
45
−
0
View file @
1462a07c
...
...
@@ -823,6 +823,51 @@ func TestRedirects(t *testing.T) {
}
}
func
TestRedirectTo
(
t
*
testing
.
T
)
{
var
okTests
=
[]
struct
{
url
string
expectedLocation
string
expectedStatus
int
}{
{
"/redirect-to?url=http://www.example.com/"
,
"http://www.example.com/"
,
http
.
StatusFound
},
{
"/redirect-to?url=http://www.example.com/&status_code=307"
,
"http://www.example.com/"
,
http
.
StatusTemporaryRedirect
},
{
"/redirect-to?url=/get"
,
"/get"
,
http
.
StatusFound
},
{
"/redirect-to?url=/get&status_code=307"
,
"/get"
,
http
.
StatusTemporaryRedirect
},
{
"/redirect-to?url=foo"
,
"foo"
,
http
.
StatusFound
},
}
for
_
,
test
:=
range
okTests
{
t
.
Run
(
"ok"
+
test
.
url
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
test
.
url
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
test
.
expectedStatus
)
assertHeader
(
t
,
w
,
"Location"
,
test
.
expectedLocation
)
})
}
var
badTests
=
[]
struct
{
url
string
expectedStatus
int
}{
{
"/redirect-to"
,
http
.
StatusBadRequest
},
{
"/redirect-to?status_code=302"
,
http
.
StatusBadRequest
},
{
"/redirect-to?url=foo&status_code=418"
,
http
.
StatusBadRequest
},
}
for
_
,
test
:=
range
badTests
{
t
.
Run
(
"bad"
+
test
.
url
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
test
.
url
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
test
.
expectedStatus
)
})
}
}
func
TestCookies
(
t
*
testing
.
T
)
{
testCookies
:=
func
(
t
*
testing
.
T
,
cookies
cookiesResponse
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/cookies"
,
nil
)
...
...
This diff is collapsed.
Click to expand it.
httpbin/httpbin.go
+
2
−
0
View file @
1462a07c
...
...
@@ -102,9 +102,11 @@ func (h *HTTPBin) Handler() http.Handler {
mux
.
HandleFunc
(
"/response-headers"
,
h
.
ResponseHeaders
)
mux
.
HandleFunc
(
"/status/"
,
h
.
Status
)
mux
.
HandleFunc
(
"/redirect/"
,
h
.
Redirect
)
mux
.
HandleFunc
(
"/relative-redirect/"
,
h
.
RelativeRedirect
)
mux
.
HandleFunc
(
"/absolute-redirect/"
,
h
.
AbsoluteRedirect
)
mux
.
HandleFunc
(
"/redirect-to"
,
h
.
RedirectTo
)
mux
.
HandleFunc
(
"/cookies"
,
h
.
Cookies
)
mux
.
HandleFunc
(
"/cookies/set"
,
h
.
SetCookies
)
...
...
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