diff --git a/proxy/proxy.go b/proxy/proxy.go index 08788dc..1b3f34a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -29,19 +29,26 @@ func (e URLError) Error() string { // NewRequest parses an http.Request into an image request. func NewRequest(r *http.Request) (*data.Request, error) { - path := strings.SplitN(r.URL.Path, "/", 3) - if len(path) != 3 { - return nil, URLError{"too few path segments", r.URL} - } - var err error req := new(data.Request) - req.URL, err = url.Parse(path[2]) - if err != nil { - return nil, URLError{ - fmt.Sprintf("unable to parse remote URL: %v", err), - r.URL, + path := r.URL.Path[1:] // strip leading slash + req.URL, err = url.Parse(path) + if err != nil || !req.URL.IsAbs() { + // first segment is likely options + parts := strings.SplitN(path, "/", 2) + if len(parts) != 2 { + return nil, URLError{"too few path segments", r.URL} + } + + req.URL, err = url.Parse(parts[1]) + if err != nil { + return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL} + } + + req.Options, err = data.ParseOptions(parts[0]) + if err != nil { + return nil, URLError{err.Error(), r.URL} } } @@ -55,11 +62,6 @@ func NewRequest(r *http.Request) (*data.Request, error) { // query string is always part of the remote URL req.URL.RawQuery = r.URL.RawQuery - req.Options, err = data.ParseOptions(path[1]) - if err != nil { - return nil, URLError{err.Error(), r.URL} - } - return req, nil } diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index 05a8575..d879379 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -2,71 +2,79 @@ package proxy import ( "net/http" + "reflect" "testing" + + "github.com/willnorris/go-imageproxy/data" ) +var emptyOptions = new(data.Options) + func TestNewRequest(t *testing.T) { tests := []struct { URL string RemoteURL string - Width int - Height int + Options *data.Options ExpectError bool }{ // invalid URLs { - "http://localhost/", "", 0, 0, true, + "http://localhost/", "", nil, true, }, { - "http://localhost/1/", "", 0, 0, true, + "http://localhost/1/", "", nil, true, }, { - "http://localhost//example.com/foo", "", 0, 0, true, + "http://localhost//example.com/foo", "", nil, true, }, { - "http://localhost//ftp://example.com/foo", "", 0, 0, true, + "http://localhost//ftp://example.com/foo", "", nil, true, }, { - "http://localhost/s/http://example.com/", "", 0, 0, true, + "http://localhost/s/http://example.com/", "", nil, true, }, { - "http://localhost/1xs/http://example.com/", "", 0, 0, true, + "http://localhost/1xs/http://example.com/", "", nil, true, }, // valid URLs + { + "http://localhost/http://example.com/foo", + "http://example.com/foo", nil, false, + }, { "http://localhost//http://example.com/foo", - "http://example.com/foo", 0, 0, false, + "http://example.com/foo", emptyOptions, false, }, { "http://localhost//https://example.com/foo", - "https://example.com/foo", 0, 0, false, + "https://example.com/foo", emptyOptions, false, }, { "http://localhost//http://example.com/foo?bar", - "http://example.com/foo?bar", 0, 0, false, + "http://example.com/foo?bar", emptyOptions, false, }, // size variations { "http://localhost/x/http://example.com/", - "http://example.com/", 0, 0, false, + "http://example.com/", emptyOptions, false, }, { "http://localhost/0/http://example.com/", - "http://example.com/", 0, 0, false, + "http://example.com/", emptyOptions, false, }, { "http://localhost/1x/http://example.com/", - "http://example.com/", 1, 0, false, + "http://example.com/", &data.Options{1, 0}, false, }, { "http://localhost/x1/http://example.com/", - "http://example.com/", 0, 1, false, + "http://example.com/", &data.Options{0, 1}, false, }, { "http://localhost/1x2/http://example.com/", - "http://example.com/", 1, 2, false, + "http://example.com/", &data.Options{1, 2}, false, }, } @@ -91,11 +99,8 @@ func TestNewRequest(t *testing.T) { if got := r.URL.String(); tt.RemoteURL != got { t.Errorf("%d. Request URL = %v, want %v", i, got, tt.RemoteURL) } - if tt.Height != r.Height { - t.Errorf("%d. Request Height = %v, want %v", i, r.Height, tt.Height) - } - if tt.Width != r.Width { - t.Errorf("%d. Request Width = %v, want %v", i, r.Width, tt.Width) + if !reflect.DeepEqual(tt.Options, r.Options) { + t.Errorf("%d. Request Options = %v, want %v", i, r.Options, tt.Options) } } }