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
1f30f965
Commit
1f30f965
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Group GET and CORS tests
parent
b6dd9eca
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
httpbin/handlers_test.go
+124
-126
124 additions, 126 deletions
httpbin/handlers_test.go
with
124 additions
and
126 deletions
httpbin/handlers_test.go
+
124
−
126
View file @
1f30f965
...
...
@@ -85,21 +85,39 @@ func TestUTF8(t *testing.T) {
assertBodyContains
(
t
,
w
,
`Hello world, Καλημέρα κόσμε, コンニチハ`
)
}
func
TestGet__Basic
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/get"
,
nil
)
func
TestGet
(
t
*
testing
.
T
)
{
makeGetRequest
:=
func
(
params
*
url
.
Values
,
headers
*
http
.
Header
,
expectedStatus
int
)
(
*
getResponse
,
*
httptest
.
ResponseRecorder
)
{
urlStr
:=
"/get"
if
params
!=
nil
{
urlStr
=
fmt
.
Sprintf
(
"%s?%s"
,
urlStr
,
params
.
Encode
())
}
r
,
_
:=
http
.
NewRequest
(
"GET"
,
urlStr
,
nil
)
r
.
Host
=
"localhost"
r
.
Header
.
Set
(
"User-Agent"
,
"test"
)
if
headers
!=
nil
{
for
k
,
vs
:=
range
*
headers
{
for
_
,
v
:=
range
vs
{
r
.
Header
.
Set
(
k
,
v
)
}
}
}
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
jsonContentType
)
assertStatusCode
(
t
,
w
,
expectedStatus
)
var
resp
*
getResponse
if
expectedStatus
==
http
.
StatusOK
{
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
}
return
resp
,
w
}
t
.
Run
(
"basic"
,
func
(
t
*
testing
.
T
)
{
resp
,
_
:=
makeGetRequest
(
nil
,
nil
,
http
.
StatusOK
)
if
resp
.
Args
.
Encode
()
!=
""
{
t
.
Fatalf
(
"expected empty args, got %s"
,
resp
.
Args
.
Encode
())
...
...
@@ -123,59 +141,66 @@ func TestGet__Basic(t *testing.T) {
t
.
Fatalf
(
"expected %s = %#v, got %#v"
,
test
.
key
,
test
.
expected
,
resp
.
Headers
.
Get
(
test
.
key
))
}
}
}
})
func
TestGet__WithParams
(
t
*
testing
.
T
)
{
params
:=
url
.
Values
{}
t
.
Run
(
"with_query_params"
,
func
(
t
*
testing
.
T
)
{
params
:=
&
url
.
Values
{}
params
.
Set
(
"foo"
,
"foo"
)
params
.
Add
(
"bar"
,
"bar1"
)
params
.
Add
(
"bar"
,
"bar2"
)
r
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"/get?%s"
,
params
.
Encode
()),
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
jsonContentType
)
var
resp
*
getResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
resp
,
_
:=
makeGetRequest
(
params
,
nil
,
http
.
StatusOK
)
if
resp
.
Args
.
Encode
()
!=
params
.
Encode
()
{
t
.
Fatalf
(
"args mismatch: %s != %s"
,
resp
.
Args
.
Encode
(),
params
.
Encode
())
}
}
})
func
TestGet__O
nly
A
llows
G
ets
(
t
*
testing
.
T
)
{
t
.
Run
(
"o
nly
_a
llows
_g
ets
"
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"POST"
,
"/get"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusMethodNotAllowed
)
assertContentType
(
t
,
w
,
"text/plain; charset=utf-8"
)
})
var
protoTests
=
[]
struct
{
key
string
value
string
}{
{
"X-Forwarded-Proto"
,
"https"
},
{
"X-Forwarded-Protocol"
,
"https"
},
{
"X-Forwarded-Ssl"
,
"on"
},
}
for
_
,
test
:=
range
protoTests
{
t
.
Run
(
test
.
key
,
func
(
t
*
testing
.
T
)
{
headers
:=
&
http
.
Header
{}
headers
.
Set
(
test
.
key
,
test
.
value
)
resp
,
_
:=
makeGetRequest
(
nil
,
headers
,
http
.
StatusOK
)
if
!
strings
.
HasPrefix
(
resp
.
URL
,
"https://"
)
{
t
.
Fatalf
(
"%s=%s should result in https URL"
,
test
.
key
,
test
.
value
)
}
})
}
}
func
TestGet__CORSHeadersWithoutRequestOrigin
(
t
*
testing
.
T
)
{
func
TestCORS
(
t
*
testing
.
T
)
{
t
.
Run
(
"CORS/no_request_origin"
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/get"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertHeader
(
t
,
w
,
"Access-Control-Allow-Origin"
,
"*"
)
}
})
func
TestGet__CORSHeadersW
ith
R
equest
O
rigin
(
t
*
testing
.
T
)
{
t
.
Run
(
"CORS/w
ith
_r
equest
_o
rigin
"
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/get"
,
nil
)
r
.
Header
.
Set
(
"Origin"
,
"origin"
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertHeader
(
t
,
w
,
"Access-Control-Allow-Origin"
,
"origin"
)
}
})
func
TestGet__CORSHeadersWithOptionsVerb
(
t
*
testing
.
T
)
{
t
.
Run
(
"CORS/options_request"
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"OPTIONS"
,
"/get"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
...
...
@@ -193,9 +218,9 @@ func TestGet__CORSHeadersWithOptionsVerb(t *testing.T) {
for
_
,
test
:=
range
headerTests
{
assertHeader
(
t
,
w
,
test
.
key
,
test
.
expected
)
}
}
})
func
TestGet__
CORS
A
llow
H
eaders
(
t
*
testing
.
T
)
{
t
.
Run
(
"
CORS
/a
llow
_h
eaders
"
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"OPTIONS"
,
"/get"
,
nil
)
r
.
Header
.
Set
(
"Access-Control-Request-Headers"
,
"X-Test-Header"
)
w
:=
httptest
.
NewRecorder
()
...
...
@@ -210,34 +235,7 @@ func TestGet__CORSAllowHeaders(t *testing.T) {
for
_
,
test
:=
range
headerTests
{
assertHeader
(
t
,
w
,
test
.
key
,
test
.
expected
)
}
}
func
TestGet__XForwardedProto
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
key
string
value
string
}{
{
"X-Forwarded-Proto"
,
"https"
},
{
"X-Forwarded-Protocol"
,
"https"
},
{
"X-Forwarded-Ssl"
,
"on"
},
}
for
_
,
test
:=
range
tests
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/get"
,
nil
)
r
.
Header
.
Set
(
test
.
key
,
test
.
value
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
var
resp
*
getResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
if
!
strings
.
HasPrefix
(
resp
.
URL
,
"https://"
)
{
t
.
Fatalf
(
"%s=%s should result in https URL"
,
test
.
key
,
test
.
value
)
}
}
})
}
func
TestIP
(
t
*
testing
.
T
)
{
...
...
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