2013-11-27 11:02:21 -08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2013-12-05 08:31:19 -08:00
|
|
|
"reflect"
|
2013-11-27 11:02:21 -08:00
|
|
|
"testing"
|
2013-12-05 08:31:19 -08:00
|
|
|
|
|
|
|
"github.com/willnorris/go-imageproxy/data"
|
2013-11-27 11:02:21 -08:00
|
|
|
)
|
|
|
|
|
2013-12-05 08:31:19 -08:00
|
|
|
var emptyOptions = new(data.Options)
|
|
|
|
|
2013-11-27 11:02:21 -08:00
|
|
|
func TestNewRequest(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
URL string
|
|
|
|
RemoteURL string
|
2013-12-05 08:31:19 -08:00
|
|
|
Options *data.Options
|
2013-11-27 11:02:21 -08:00
|
|
|
ExpectError bool
|
|
|
|
}{
|
|
|
|
// invalid URLs
|
|
|
|
{
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://localhost/", "", nil, true,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://localhost/1/", "", nil, true,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://localhost//example.com/foo", "", nil, true,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://localhost//ftp://example.com/foo", "", nil, true,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
2013-12-06 11:01:34 -08:00
|
|
|
|
|
|
|
// invalid options. These won't return errors, but will not fully parse the options
|
2013-11-27 11:02:21 -08:00
|
|
|
{
|
2013-12-06 11:01:34 -08:00
|
|
|
"http://localhost/s/http://example.com/",
|
|
|
|
"http://example.com/", emptyOptions, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
2013-12-06 11:01:34 -08:00
|
|
|
"http://localhost/1xs/http://example.com/",
|
|
|
|
"http://example.com/", &data.Options{Width: 1}, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// valid URLs
|
2013-12-05 08:31:19 -08:00
|
|
|
{
|
|
|
|
"http://localhost/http://example.com/foo",
|
|
|
|
"http://example.com/foo", nil, false,
|
|
|
|
},
|
2013-11-27 11:02:21 -08:00
|
|
|
{
|
|
|
|
"http://localhost//http://example.com/foo",
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://example.com/foo", emptyOptions, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost//https://example.com/foo",
|
2013-12-05 08:31:19 -08:00
|
|
|
"https://example.com/foo", emptyOptions, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost//http://example.com/foo?bar",
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://example.com/foo?bar", emptyOptions, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// size variations
|
|
|
|
{
|
|
|
|
"http://localhost/x/http://example.com/",
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://example.com/", emptyOptions, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost/0/http://example.com/",
|
2013-12-05 08:31:19 -08:00
|
|
|
"http://example.com/", emptyOptions, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost/1x/http://example.com/",
|
2013-12-06 11:01:34 -08:00
|
|
|
"http://example.com/", &data.Options{1, 0, false}, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost/x1/http://example.com/",
|
2013-12-06 11:01:34 -08:00
|
|
|
"http://example.com/", &data.Options{0, 1, false}, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost/1x2/http://example.com/",
|
2013-12-06 11:01:34 -08:00
|
|
|
"http://example.com/", &data.Options{1, 2, false}, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost/,fit/http://example.com/",
|
|
|
|
"http://example.com/", &data.Options{0, 0, true}, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://localhost/1x2,fit/http://example.com/",
|
|
|
|
"http://example.com/", &data.Options{1, 2, true}, false,
|
2013-11-27 11:02:21 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
req, err := http.NewRequest("GET", tt.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%d. Error parsing request: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := NewRequest(req)
|
|
|
|
if tt.ExpectError {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%d. Expected parsing error", i)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
t.Errorf("%d. Error parsing request: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if got := r.URL.String(); tt.RemoteURL != got {
|
|
|
|
t.Errorf("%d. Request URL = %v, want %v", i, got, tt.RemoteURL)
|
|
|
|
}
|
2013-12-05 08:31:19 -08:00
|
|
|
if !reflect.DeepEqual(tt.Options, r.Options) {
|
|
|
|
t.Errorf("%d. Request Options = %v, want %v", i, r.Options, tt.Options)
|
2013-11-27 11:02:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|