Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 211 lines (163 sloc) 5.529 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
package mango

import (
"http"
"testing"
"runtime"
)

func jsonServer(env Env) (Status, Headers, Body) {
return 200, Headers{"Content-Type": []string{"application/json"}, "Content-Length": []string{"13"}}, Body("{\"foo\":\"bar\"}")
}

func nonJsonServer(env Env) (Status, Headers, Body) {
return 200, Headers{"Content-Type": []string{"text/html"}}, Body("<h1>Hello World!</h1>")
}

func init() {
runtime.GOMAXPROCS(4)
}

func TestJSONPSuccess(t *testing.T) {
// Compile the stack
jsonpStack := new(Stack)
jsonpStack.Middleware(JSONP)
jsonpApp := jsonpStack.Compile(jsonServer)

// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/?callback=parseResponse", nil)
status, headers, body := jsonpApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}

if headers.Get("Content-Type") != "application/javascript" {
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)
}
}

func TestNonJSONPSuccess(t *testing.T) {
// Compile the stack
nonJsonpStack := new(Stack)
nonJsonpStack.Middleware(JSONP)
nonJsonpApp := nonJsonpStack.Compile(nonJsonServer)

// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/?callback=parseResponse", nil)
status, headers, body := nonJsonpApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}

if headers.Get("Content-Type") != "text/html" {
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)
}
}

func TestJSONPNoCallback(t *testing.T) {
// Compile the stack
jsonpStack := new(Stack)
jsonpStack.Middleware(JSONP)
jsonpApp := jsonpStack.Compile(jsonServer)

// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/", nil)
status, headers, body := jsonpApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}

if headers.Get("Content-Type") != "application/json" {
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)
}
}

func TestJSONPInvalidCallback(t *testing.T) {
// Compile the stack
jsonpStack := new(Stack)
jsonpStack.Middleware(JSONP)
jsonpApp := jsonpStack.Compile(jsonServer)

// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/?callback=invalid(callback)", nil)
status, headers, body := jsonpApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 400 {
t.Error("Expected status to equal 400, got:", status)
}

if headers.Get("Content-Type") != "text/plain" {
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)
}
}

func BenchmarkJSONP(b *testing.B) {
b.StopTimer()

jsonpStack := new(Stack)
jsonpStack.Middleware(JSONP)
jsonpApp := jsonpStack.Compile(jsonServer)

request, _ := http.NewRequest("GET", "http://localhost:3000/?callback=parseResponse", nil)

b.StartTimer()
for i := 0; i < b.N; i++ {
jsonpApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}

func BenchmarkNonJSONP(b *testing.B) {
b.StopTimer()

nonJsonpStack := new(Stack)
nonJsonpStack.Middleware(JSONP)
nonJsonpApp := nonJsonpStack.Compile(nonJsonServer)

request, _ := http.NewRequest("GET", "http://localhost:3000/?callback=parseResponse", nil)

b.StartTimer()
for i := 0; i < b.N; i++ {
nonJsonpApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}

func BenchmarkJSONPNoCallback(b *testing.B) {
b.StopTimer()

jsonpStack := new(Stack)
jsonpStack.Middleware(JSONP)
jsonpApp := jsonpStack.Compile(jsonServer)

request, _ := http.NewRequest("GET", "http://localhost:3000/", nil)

b.StartTimer()
for i := 0; i < b.N; i++ {
jsonpApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}

func BenchmarkJSONPInvalidCallback(b *testing.B) {
b.StopTimer()

jsonpStack := new(Stack)
jsonpStack.Middleware(JSONP)
jsonpApp := jsonpStack.Compile(jsonServer)

request, _ := http.NewRequest("GET", "http://localhost:3000/?callback=invalid(callback)", nil)

b.StartTimer()
for i := 0; i < b.N; i++ {
jsonpApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}
Something went wrong with that request. Please try again.