go-paste/image/image_test.go
2023-11-08 16:18:51 -07:00

68 lines
1.3 KiB
Go

package image_test
import (
"bytes"
"encoding/base64"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/matryer/is"
"go.sour.is/paste/v2/image"
)
func TestPostImgur(t *testing.T) {
is := is.New(t)
data := "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=="
dir, err := os.MkdirTemp("", "image")
is.NoErr(err)
im, err := image.New(dir, 0)
is.NoErr(err)
{
body := &bytes.Buffer{}
wr := multipart.NewWriter(body)
w, err := wr.CreateFormFile("image", "image.png")
is.NoErr(err)
fmt.Fprint(w, data)
err = wr.Close()
is.NoErr(err)
t.Log(body.String())
req := httptest.NewRequest("POST", "/3/upload", body)
req.Header.Set("Content-Type", "multipart/form-data; boundary="+wr.Boundary())
res := httptest.NewRecorder()
im.ServeHTTP(res, req)
is.Equal(res.Code, http.StatusCreated)
is.Equal(res.Body.String(), `{"data":{"link":"https://example.com/i/Igg59VTi6JNuXVriXMR3U_lzfAc","deletehash":""},"success":true,"status":200}
`)
}
{
req := httptest.NewRequest("GET", "/Igg59VTi6JNuXVriXMR3U_lzfAc", nil)
res := httptest.NewRecorder()
im.ServeHTTP(res, req)
is.Equal(res.Code, http.StatusOK)
s := base64.StdEncoding.EncodeToString(res.Body.Bytes())
is.Equal(s, data)
}
err = os.RemoveAll(dir)
is.NoErr(err)
}