mirror of
https://github.com/h2non/imaginary.git
synced 2025-12-13 20:37:04 +01:00
* Replace Dep by Go Modules Code refactoring (switch, gofmt, ...) Update libvips to 8.8.1 and go to 1.12.9 Use Debian Buster on Dockerfile * Add GOLANG_VERSION to build args Currently all tests done by TravisCI run on the same Go version regardless of the matrix * Remove Go 1.10 from Travis matrix (Go modules require 1.11+) * Update README.md Prerequisites * Ignore Hash.Write() errors
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
const fixtureFile = "testdata/large.jpg"
|
|
|
|
func TestSourceBodyMatch(t *testing.T) {
|
|
u, _ := url.Parse("http://foo")
|
|
req := &http.Request{Method: http.MethodPost, URL: u}
|
|
source := NewBodyImageSource(&SourceConfig{})
|
|
|
|
if !source.Matches(req) {
|
|
t.Error("Cannot match the request")
|
|
}
|
|
}
|
|
|
|
func TestBodyImageSource(t *testing.T) {
|
|
var body []byte
|
|
var err error
|
|
|
|
source := NewBodyImageSource(&SourceConfig{})
|
|
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
if !source.Matches(r) {
|
|
t.Fatal("Cannot match the request")
|
|
}
|
|
|
|
body, err = source.GetImage(r)
|
|
if err != nil {
|
|
t.Fatalf("Error while reading the body: %s", err)
|
|
}
|
|
_, _ = w.Write(body)
|
|
}
|
|
|
|
file, _ := os.Open(fixtureFile)
|
|
r, _ := http.NewRequest(http.MethodPost, "http://foo/bar", file)
|
|
w := httptest.NewRecorder()
|
|
fakeHandler(w, r)
|
|
|
|
buf, _ := ioutil.ReadFile(fixtureFile)
|
|
if len(body) != len(buf) {
|
|
t.Error("Invalid response body")
|
|
}
|
|
}
|
|
|
|
func testReadBody(t *testing.T) {
|
|
var body []byte
|
|
var err error
|
|
|
|
source := NewBodyImageSource(&SourceConfig{})
|
|
fakeHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
if !source.Matches(r) {
|
|
t.Fatal("Cannot match the request")
|
|
}
|
|
|
|
body, err = source.GetImage(r)
|
|
if err != nil {
|
|
t.Fatalf("Error while reading the body: %s", err)
|
|
}
|
|
_, _ = w.Write(body)
|
|
}
|
|
|
|
file, _ := os.Open(fixtureFile)
|
|
r, _ := http.NewRequest(http.MethodPost, "http://foo/bar", file)
|
|
w := httptest.NewRecorder()
|
|
fakeHandler(w, r)
|
|
|
|
buf, _ := ioutil.ReadFile(fixtureFile)
|
|
if len(body) != len(buf) {
|
|
t.Error("Invalid response body")
|
|
}
|
|
}
|