-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagine_it_test.go
121 lines (102 loc) · 2.75 KB
/
imagine_it_test.go
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
package imagine_test
import (
"fmt"
"image"
"image/color"
"image/png"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/risico/imagine"
)
func TestUploadHandler(t *testing.T) {
var err error
storage := imagine.NewInMemoryStorage(imagine.MemoryStoreParams{})
assert.NoError(t, err)
i, err := imagine.New(imagine.Params{
Storage: storage,
Cache: imagine.NewInMemoryStorage(imagine.MemoryStoreParams{}),
})
assert.NoError(t, err)
// Set up a pipe to avoid buffering
pr, pw := io.Pipe()
// This writer is going to transform
// what we pass to it to multipart form data
// and write it to our io.Pipe
writer := multipart.NewWriter(pw)
go func() {
defer writer.Close()
// We create the form data field 'fileupload'
// which returns another writer to write the actual file
var part io.Writer
part, err = writer.CreateFormFile("file", "randomimage.png")
if err != nil {
t.Error(err)
}
img := createImage()
// Encode() takes an io.Writer.
// We pass the multipart field
// 'fileupload' that we defined
// earlier which, in turn, writes
// to our io.Pipe
err = png.Encode(part, img)
if err != nil {
t.Error(err)
}
}()
request := httptest.NewRequest("POST", "/", pr)
request.Header.Add("Content-Type", writer.FormDataContentType())
response := httptest.NewRecorder()
handler := i.UploadHandlerFunc()
handler.ServeHTTP(response, request)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.NotEmpty(t, string(body))
}
func TestGetImagineHandler(t *testing.T) {
t.SkipNow()
storage, err := imagine.NewLocalStorage(imagine.LocalStoreParams{
Path: "/tmp/imagine",
})
assert.NoError(t, err)
i, err := imagine.New(imagine.Params{
Storage: storage,
Cache: imagine.NewInMemoryStorage(imagine.MemoryStoreParams{
TTL: 1 * time.Hour,
}),
})
assert.NoError(t, err)
ts := httptest.NewUnstartedServer(i.GetHandlerFunc())
ts.Start()
resp, err := ts.Client().Get(fmt.Sprintf("%s/imagine/someiage.png", ts.URL))
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func createImage() image.Image {
width := 200
height := 100
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
// Colors are defined by Red, Green, Blue, Alpha uint8 values.
cyan := color.RGBA{100, 200, 200, 0xff}
// Set color for each pixel.
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
switch {
case x < width/2 && y < height/2: // upper left quadrant
img.Set(x, y, cyan)
case x >= width/2 && y >= height/2: // lower right quadrant
img.Set(x, y, color.White)
default:
// Use zero value.
}
}
}
return img
}