Skip to content

Commit ea791a1

Browse files
committed
Fixing binary file transmission. Resolves #15
1 parent 07c74e6 commit ea791a1

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

mango.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (this *Stack) HandlerFunc(app App) http.HandlerFunc {
133133
}
134134
}
135135
w.WriteHeader(int(status))
136-
fmt.Fprintf(w, string(body))
136+
w.Write([]byte(body))
137137
}
138138
}
139139

static/binary_file.png

195 Bytes
Loading

static_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package mango
22

33
import (
4+
"bytes"
45
"http"
6+
"io/ioutil"
57
"testing"
68
"runtime"
79
)
@@ -68,6 +70,34 @@ func TestStaticFail(t *testing.T) {
6870
}
6971
}
7072

73+
func TestStaticBinaryFile(t *testing.T) {
74+
// Compile the stack
75+
staticStack := new(Stack)
76+
staticStack.Middleware(Static("./static"))
77+
staticApp := staticStack.Compile(staticTestServer)
78+
79+
// Request against it
80+
request, err := http.NewRequest("GET", "http://localhost:3000/binary_file.png", nil)
81+
status, _, body := staticApp(Env{"mango.request": &Request{request}})
82+
83+
if err != nil {
84+
t.Error(err)
85+
}
86+
87+
if status != 200 {
88+
t.Error("Expected status to equal 200, got:", status)
89+
}
90+
91+
expected, err := ioutil.ReadFile("./static/binary_file.png")
92+
if err != nil {
93+
t.Error(err)
94+
}
95+
96+
if bytes.Compare([]byte(body), []byte(expected)) != 0 {
97+
t.Error("Expected body to equal ./static/binary_file.png")
98+
}
99+
}
100+
71101
func BenchmarkStatic(b *testing.B) {
72102
b.StopTimer()
73103

0 commit comments

Comments
 (0)