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
a4426b4d
Commit
a4426b4d
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Fix content type for JSON responses
parent
93f147d6
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
httpbin/handlers_test.go
+51
-21
51 additions, 21 deletions
httpbin/handlers_test.go
httpbin/helpers.go
+1
-1
1 addition, 1 deletion
httpbin/helpers.go
with
52 additions
and
22 deletions
httpbin/handlers_test.go
+
51
−
21
View file @
a4426b4d
...
...
@@ -19,14 +19,31 @@ var app = NewHTTPBin(&Options{
var
handler
=
app
.
Handler
()
func
assertStatusCode
(
t
*
testing
.
T
,
w
*
httptest
.
ResponseRecorder
,
code
int
)
{
if
w
.
Code
!=
code
{
t
.
Fatalf
(
"expected status code %d, got %d"
,
code
,
w
.
Code
)
}
}
func
assertContentType
(
t
*
testing
.
T
,
w
*
httptest
.
ResponseRecorder
,
contentType
string
)
{
if
w
.
Header
()
.
Get
(
"Content-Type"
)
!=
contentType
{
t
.
Fatalf
(
"expected content type %s, got %s"
,
contentType
,
w
.
Header
()
.
Get
(
"Content-Type"
))
}
}
func
assertBodyContains
(
t
*
testing
.
T
,
w
*
httptest
.
ResponseRecorder
,
needle
string
)
{
if
!
strings
.
Contains
(
w
.
Body
.
String
(),
needle
)
{
t
.
Fatalf
(
"expected string %v in body"
,
needle
)
}
}
func
TestIndex
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
if
!
strings
.
Contains
(
w
.
Body
.
String
(),
"go-httpbin"
)
{
t
.
Fatalf
(
"expected go-httpbin in index body"
)
}
assertContentType
(
t
,
w
,
"text/html; charset=utf-8"
)
assertBodyContains
(
t
,
w
,
"go-httpbin"
)
}
func
TestFormsPost
(
t
*
testing
.
T
)
{
...
...
@@ -34,9 +51,8 @@ func TestFormsPost(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
if
!
strings
.
Contains
(
w
.
Body
.
String
(),
`<form method="post" action="/post">`
)
{
t
.
Fatalf
(
"expected <form> in body"
)
}
assertContentType
(
t
,
w
,
"text/html; charset=utf-8"
)
assertBodyContains
(
t
,
w
,
`<form method="post" action="/post">`
)
}
func
TestUTF8
(
t
*
testing
.
T
)
{
...
...
@@ -44,12 +60,8 @@ func TestUTF8(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Header
()
.
Get
(
"Content-Type"
)
!=
"text/html; charset=utf-8"
{
t
.
Fatalf
(
"expected 'text/html; charset=utf-8' content type"
)
}
if
!
strings
.
Contains
(
w
.
Body
.
String
(),
`Hello world, Καλημέρα κόσμε, コンニチハ`
)
{
t
.
Fatalf
(
"expected utf8 text in body"
)
}
assertContentType
(
t
,
w
,
"text/html; charset=utf-8"
)
assertBodyContains
(
t
,
w
,
`Hello world, Καλημέρα κόσμε, コンニチハ`
)
}
func
TestGet__Basic
(
t
*
testing
.
T
)
{
...
...
@@ -59,9 +71,8 @@ func TestGet__Basic(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Code
!=
200
{
t
.
Fatalf
(
"expected status code 200, got %d"
,
w
.
Code
)
}
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
...
...
@@ -98,9 +109,8 @@ func TestGet__OnlyAllowsGets(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Code
!=
http
.
StatusMethodNotAllowed
{
t
.
Fatalf
(
"expected HTTP 405, got %d"
,
w
.
Code
)
}
assertStatusCode
(
t
,
w
,
http
.
StatusMethodNotAllowed
)
assertContentType
(
t
,
w
,
"text/plain; charset=utf-8"
)
}
func
TestGet__CORSHeadersWithoutRequestOrigin
(
t
*
testing
.
T
)
{
...
...
@@ -199,6 +209,9 @@ func TestIP(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
ipResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
...
...
@@ -216,6 +229,9 @@ func TestUserAgent(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
userAgentResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
...
...
@@ -236,6 +252,9 @@ func TestHeaders(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
headersResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
...
...
@@ -258,6 +277,9 @@ func TestPost__EmptyBody(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
...
...
@@ -283,6 +305,9 @@ func TestPost__FormEncodedBody(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
...
...
@@ -316,6 +341,9 @@ func TestPost__FormEncodedBodyNoContentType(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
...
...
@@ -353,6 +381,9 @@ func TestPost__JSON(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
...
...
@@ -390,7 +421,6 @@ func TestPost__BodyTooBig(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Code
!=
http
.
StatusBadRequest
{
t
.
Fatalf
(
"expected code %d, got %d"
,
http
.
StatusBadRequest
,
w
.
Code
)
}
assertStatusCode
(
t
,
w
,
http
.
StatusBadRequest
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
}
This diff is collapsed.
Click to expand it.
httpbin/helpers.go
+
1
−
1
View file @
a4426b4d
...
...
@@ -48,8 +48,8 @@ func getURL(r *http.Request) string {
}
func
writeJSON
(
w
http
.
ResponseWriter
,
body
[]
byte
,
status
int
)
{
w
.
WriteHeader
(
status
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; encoding=utf-8"
)
w
.
WriteHeader
(
status
)
w
.
Write
(
body
)
}
...
...
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