Permalink
Browse files

Adding Content-Length to JSONP middleware

  • Loading branch information...
paulbellamy committed Jun 6, 2011
1 parent 1966358 commit 5ca3e83aeb57e6078615fd96690b75f673ad5829
Showing with 21 additions and 3 deletions.
  1. +0 −1 TODO
  2. +4 −1 jsonp.go
  3. +17 −1 jsonp_test.go
View
1 TODO
@@ -1,6 +1,5 @@
* Better cookie handler
* Domain/Expiry
* JSONP should set/update Content-Length header
POTENTIAL MIDDLEWARE
====================
View
@@ -11,7 +11,7 @@ func JSONP(env Env, app App) (Status, Headers, Body) {
if callback != "" {
if matched, err := regexp.MatchString("^[a-zA-Z_$][a-zA-Z_0-9$]*([.]?[a-zA-Z_$][a-zA-Z_0-9$]*)*$", callback); !matched || err != nil {
return 400, Headers{"Content-Type": []string{"text/plain"}}, "Bad Request"
return 400, Headers{"Content-Type": []string{"text/plain"}, "Content-Length": []string{"11"}}, "Bad Request"
}
}
@@ -20,6 +20,9 @@ func JSONP(env Env, app App) (Status, Headers, Body) {
if callback != "" && strings.Contains(headers.Get("Content-Type"), "application/json") {
headers.Set("Content-Type", strings.Replace(headers.Get("Content-Type"), "json", "javascript", -1))
body = Body(fmt.Sprintf("%s(%s)", callback, body))
if headers.Get("Content-Length") != "" {
headers.Set("Content-Length", fmt.Sprintf("%d", len(body)))
}
}
return status, headers, body
View
@@ -8,7 +8,7 @@ import (
)
func jsonServer(env Env) (Status, Headers, Body) {
return 200, Headers{"Content-Type": []string{"application/json"}}, Body("{\"foo\":\"bar\"}")
return 200, Headers{"Content-Type": []string{"application/json"}, "Content-Length": []string{"13"}}, Body("{\"foo\":\"bar\"}")
}
func nonJsonServer(env Env) (Status, Headers, Body) {
@@ -43,6 +43,10 @@ func TestJSONPSuccess(t *testing.T) {
t.Error("Expected Content-Type to equal \"application/javascript\", got:", headers.Get("Content-Type"))
}
if headers.Get("Content-Length") != "28" {
t.Error("Expected Content-Length to equal \"28\", got:", headers.Get("Content-Length"))
}
expected := "parseResponse({\"foo\":\"bar\"})"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
@@ -71,6 +75,10 @@ func TestNonJSONPSuccess(t *testing.T) {
t.Error("Expected Content-Type to equal \"text/html\", got:", headers.Get("Content-Type"))
}
if headers.Get("Content-Length") != "" {
t.Error("Expected Content-Length to equal \"\", got:", headers.Get("Content-Length"))
}
expected := "<h1>Hello World!</h1>"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
@@ -99,6 +107,10 @@ func TestJSONPNoCallback(t *testing.T) {
t.Error("Expected Content-Type to equal \"application/json\", got:", headers.Get("Content-Type"))
}
if headers.Get("Content-Length") != "13" {
t.Error("Expected Content-Length to equal \"13\", got:", headers.Get("Content-Length"))
}
expected := "{\"foo\":\"bar\"}"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
@@ -127,6 +139,10 @@ func TestJSONPInvalidCallback(t *testing.T) {
t.Error("Expected Content-Type to equal \"text/plain\", got:", headers.Get("Content-Type"))
}
if headers.Get("Content-Length") != "11" {
t.Error("Expected Content-Length to equal \"11\", got:", headers.Get("Content-Length"))
}
expected := "Bad Request"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)

0 comments on commit 5ca3e83

Please sign in to comment.