2014-12-13 00:48:41 +01:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2017-03-01 19:24:08 +01:00
|
|
|
"fmt"
|
2015-01-31 20:43:06 +01:00
|
|
|
"net/http"
|
2014-12-13 00:48:41 +01:00
|
|
|
"net/url"
|
2017-03-01 19:24:08 +01:00
|
|
|
"reflect"
|
2014-12-13 00:48:41 +01:00
|
|
|
"testing"
|
2015-12-15 23:35:23 +01:00
|
|
|
|
|
|
|
"github.com/docker/distribution/reference"
|
2014-12-13 00:48:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type urlBuilderTestCase struct {
|
2015-01-31 20:43:06 +01:00
|
|
|
description string
|
|
|
|
expectedPath string
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr error
|
2015-01-31 20:43:06 +01:00
|
|
|
build func() (string, error)
|
2014-12-13 00:48:41 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 20:43:06 +01:00
|
|
|
func makeURLBuilderTestCases(urlBuilder *URLBuilder) []urlBuilderTestCase {
|
2017-01-14 02:06:03 +01:00
|
|
|
fooBarRef, _ := reference.WithName("foo/bar")
|
2015-01-31 20:43:06 +01:00
|
|
|
return []urlBuilderTestCase{
|
2014-12-13 00:48:41 +01:00
|
|
|
{
|
2015-01-31 20:43:06 +01:00
|
|
|
description: "test base url",
|
|
|
|
expectedPath: "/v2/",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2015-01-31 20:43:06 +01:00
|
|
|
build: urlBuilder.BuildBaseURL,
|
2014-12-13 00:48:41 +01:00
|
|
|
},
|
|
|
|
{
|
2015-01-31 20:43:06 +01:00
|
|
|
description: "test tags url",
|
|
|
|
expectedPath: "/v2/foo/bar/tags/list",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2014-12-13 00:48:41 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildTagsURL(fooBarRef)
|
2014-12-13 00:48:41 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-03-01 19:24:08 +01:00
|
|
|
description: "test manifest url tagged ref",
|
2015-01-31 20:43:06 +01:00
|
|
|
expectedPath: "/v2/foo/bar/manifests/tag",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2014-12-13 00:48:41 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-16 01:43:13 +01:00
|
|
|
ref, _ := reference.WithTag(fooBarRef, "tag")
|
|
|
|
return urlBuilder.BuildManifestURL(ref)
|
2014-12-13 00:48:41 +01:00
|
|
|
},
|
|
|
|
},
|
2017-03-01 19:24:08 +01:00
|
|
|
{
|
|
|
|
description: "test manifest url bare ref",
|
|
|
|
expectedPath: "",
|
|
|
|
expectedErr: fmt.Errorf("reference must have a tag or digest"),
|
|
|
|
build: func() (string, error) {
|
|
|
|
return urlBuilder.BuildManifestURL(fooBarRef)
|
|
|
|
},
|
|
|
|
},
|
2014-12-13 00:48:41 +01:00
|
|
|
{
|
2015-01-31 20:43:06 +01:00
|
|
|
description: "build blob url",
|
2015-12-16 02:18:13 +01:00
|
|
|
expectedPath: "/v2/foo/bar/blobs/sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2014-12-13 00:48:41 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-16 01:43:13 +01:00
|
|
|
ref, _ := reference.WithDigest(fooBarRef, "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5")
|
|
|
|
return urlBuilder.BuildBlobURL(ref)
|
2014-12-13 00:48:41 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 20:43:06 +01:00
|
|
|
description: "build blob upload url",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2014-12-13 00:48:41 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadURL(fooBarRef)
|
2014-12-13 00:48:41 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 20:43:06 +01:00
|
|
|
description: "build blob upload url with digest and size",
|
2015-12-16 02:18:13 +01:00
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/?digest=sha256%3A3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5&size=10000",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2014-12-13 00:48:41 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadURL(fooBarRef, url.Values{
|
2014-12-13 00:48:41 +01:00
|
|
|
"size": []string{"10000"},
|
2015-12-16 02:18:13 +01:00
|
|
|
"digest": []string{"sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"},
|
2014-12-13 00:48:41 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 20:43:06 +01:00
|
|
|
description: "build blob upload chunk url",
|
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2014-12-13 00:48:41 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadChunkURL(fooBarRef, "uuid-part")
|
2014-12-13 00:48:41 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-31 20:43:06 +01:00
|
|
|
description: "build blob upload chunk url with digest and size",
|
2015-12-16 02:18:13 +01:00
|
|
|
expectedPath: "/v2/foo/bar/blobs/uploads/uuid-part?digest=sha256%3A3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5&size=10000",
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr: nil,
|
2014-12-13 00:48:41 +01:00
|
|
|
build: func() (string, error) {
|
2015-12-15 23:35:23 +01:00
|
|
|
return urlBuilder.BuildBlobUploadChunkURL(fooBarRef, "uuid-part", url.Values{
|
2014-12-13 00:48:41 +01:00
|
|
|
"size": []string{"10000"},
|
2015-12-16 02:18:13 +01:00
|
|
|
"digest": []string{"sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"},
|
2014-12-13 00:48:41 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
2015-01-31 20:43:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestURLBuilder tests the various url building functions, ensuring they are
|
|
|
|
// returning the expected values.
|
|
|
|
func TestURLBuilder(t *testing.T) {
|
|
|
|
roots := []string{
|
|
|
|
"http://example.com",
|
|
|
|
"https://example.com",
|
|
|
|
"http://localhost:5000",
|
|
|
|
"https://localhost:5443",
|
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest := func(relative bool) {
|
|
|
|
for _, root := range roots {
|
|
|
|
urlBuilder, err := NewURLBuilderFromString(root, relative)
|
2015-01-31 20:43:06 +01:00
|
|
|
if err != nil {
|
2016-02-23 02:49:23 +01:00
|
|
|
t.Fatalf("unexpected error creating urlbuilder: %v", err)
|
2015-01-31 20:43:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
for _, testCase := range makeURLBuilderTestCases(urlBuilder) {
|
|
|
|
url, err := testCase.build()
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr := testCase.expectedErr
|
|
|
|
if !reflect.DeepEqual(expectedErr, err) {
|
|
|
|
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
|
|
|
|
}
|
|
|
|
if expectedErr != nil {
|
|
|
|
continue
|
2016-02-23 02:49:23 +01:00
|
|
|
}
|
2017-03-01 19:24:08 +01:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
expectedURL := testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = root + expectedURL
|
|
|
|
}
|
2015-01-31 20:43:06 +01:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
if url != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
|
|
|
|
}
|
2015-01-31 20:43:06 +01:00
|
|
|
}
|
2014-12-13 00:48:41 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest(true)
|
|
|
|
doTest(false)
|
2015-01-31 20:43:06 +01:00
|
|
|
}
|
2014-12-13 00:48:41 +01:00
|
|
|
|
2015-02-24 23:59:01 +01:00
|
|
|
func TestURLBuilderWithPrefix(t *testing.T) {
|
|
|
|
roots := []string{
|
|
|
|
"http://example.com/prefix/",
|
|
|
|
"https://example.com/prefix/",
|
|
|
|
"http://localhost:5000/prefix/",
|
|
|
|
"https://localhost:5443/prefix/",
|
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest := func(relative bool) {
|
|
|
|
for _, root := range roots {
|
|
|
|
urlBuilder, err := NewURLBuilderFromString(root, relative)
|
2015-02-24 23:59:01 +01:00
|
|
|
if err != nil {
|
2016-02-23 02:49:23 +01:00
|
|
|
t.Fatalf("unexpected error creating urlbuilder: %v", err)
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
for _, testCase := range makeURLBuilderTestCases(urlBuilder) {
|
|
|
|
url, err := testCase.build()
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr := testCase.expectedErr
|
|
|
|
if !reflect.DeepEqual(expectedErr, err) {
|
|
|
|
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
|
|
|
|
}
|
|
|
|
if expectedErr != nil {
|
|
|
|
continue
|
2016-02-23 02:49:23 +01:00
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
expectedURL := testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = root[0:len(root)-1] + expectedURL
|
|
|
|
}
|
|
|
|
if url != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
|
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest(true)
|
|
|
|
doTest(false)
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 20:43:06 +01:00
|
|
|
type builderFromRequestTestCase struct {
|
|
|
|
request *http.Request
|
|
|
|
base string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderFromRequest(t *testing.T) {
|
|
|
|
u, err := url.Parse("http://example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testRequests := []struct {
|
2016-10-13 17:59:49 +02:00
|
|
|
name string
|
2015-09-18 20:03:15 +02:00
|
|
|
request *http.Request
|
|
|
|
base string
|
|
|
|
configHost url.URL
|
2015-01-31 20:43:06 +01:00
|
|
|
}{
|
|
|
|
{
|
2016-10-13 17:59:49 +02:00
|
|
|
name: "no forwarded header",
|
2015-01-31 20:43:06 +01:00
|
|
|
request: &http.Request{URL: u, Host: u.Host},
|
|
|
|
base: "http://example.com",
|
|
|
|
},
|
2015-10-15 10:56:28 +02:00
|
|
|
{
|
2016-10-13 17:59:49 +02:00
|
|
|
name: "https protocol forwarded with a non-standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Custom-Forwarded-Proto": []string{"https"},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
|
|
|
base: "http://example.com",
|
2015-10-15 10:56:28 +02:00
|
|
|
},
|
2015-01-31 20:43:06 +01:00
|
|
|
{
|
2016-10-13 17:59:49 +02:00
|
|
|
name: "forwarded protocol is the same",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Proto": []string{"https"},
|
|
|
|
}},
|
|
|
|
base: "https://example.com",
|
2015-01-31 20:43:06 +01:00
|
|
|
},
|
2015-04-24 23:04:48 +02:00
|
|
|
{
|
2016-10-13 17:59:49 +02:00
|
|
|
name: "forwarded host with a non-standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Host": []string{"first.example.com"},
|
|
|
|
}},
|
|
|
|
base: "http://first.example.com",
|
2015-04-24 23:04:48 +02:00
|
|
|
},
|
|
|
|
{
|
2016-10-13 17:59:49 +02:00
|
|
|
name: "forwarded multiple hosts a with non-standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Host": []string{"first.example.com, proxy1.example.com"},
|
|
|
|
}},
|
|
|
|
base: "http://first.example.com",
|
2015-04-24 23:04:48 +02:00
|
|
|
},
|
2015-09-18 20:03:15 +02:00
|
|
|
{
|
2016-10-13 17:59:49 +02:00
|
|
|
name: "host configured in config file takes priority",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Host": []string{"first.example.com, proxy1.example.com"},
|
|
|
|
}},
|
|
|
|
base: "https://third.example.com:5000",
|
2015-09-18 20:03:15 +02:00
|
|
|
configHost: url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "third.example.com:5000",
|
|
|
|
},
|
|
|
|
},
|
2016-10-13 17:59:49 +02:00
|
|
|
{
|
|
|
|
name: "forwarded host and port with just one non-standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Host": []string{"first.example.com:443"},
|
|
|
|
}},
|
|
|
|
base: "http://first.example.com:443",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "forwarded port with a non-standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Host": []string{"example.com:5000"},
|
2016-10-13 17:59:49 +02:00
|
|
|
"X-Forwarded-Port": []string{"5000"},
|
|
|
|
}},
|
|
|
|
base: "http://example.com:5000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "forwarded multiple ports with a non-standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Port": []string{"443 , 5001"},
|
|
|
|
}},
|
2017-03-21 00:13:33 +01:00
|
|
|
base: "http://example.com",
|
2016-10-13 17:59:49 +02:00
|
|
|
},
|
2017-03-21 00:10:36 +01:00
|
|
|
{
|
|
|
|
name: "forwarded standard port with non-standard headers",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Proto": []string{"https"},
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Host": []string{"example.com"},
|
2017-03-21 00:10:36 +01:00
|
|
|
"X-Forwarded-Port": []string{"443"},
|
|
|
|
}},
|
|
|
|
base: "https://example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "forwarded standard port with non-standard headers and explicit port",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host + ":443", Header: http.Header{
|
|
|
|
"X-Forwarded-Proto": []string{"https"},
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Host": []string{u.Host + ":443"},
|
2017-03-21 00:10:36 +01:00
|
|
|
"X-Forwarded-Port": []string{"443"},
|
|
|
|
}},
|
|
|
|
base: "https://example.com:443",
|
|
|
|
},
|
2016-10-13 17:59:49 +02:00
|
|
|
{
|
|
|
|
name: "several non-standard headers",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Proto": []string{"https"},
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Host": []string{" first.example.com:12345 "},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
2017-03-21 00:13:33 +01:00
|
|
|
base: "https://first.example.com:12345",
|
2016-10-13 17:59:49 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "forwarded host with port supplied takes priority",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Host": []string{"first.example.com:5000"},
|
|
|
|
"X-Forwarded-Port": []string{"80"},
|
|
|
|
}},
|
|
|
|
base: "http://first.example.com:5000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "malformed forwarded port",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Host": []string{"first.example.com"},
|
|
|
|
"X-Forwarded-Port": []string{"abcd"},
|
|
|
|
}},
|
|
|
|
base: "http://first.example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "forwarded protocol and addr using standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"Forwarded": []string{`proto=https;host="192.168.22.30:80"`},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
|
|
|
base: "https://192.168.22.30:80",
|
|
|
|
},
|
|
|
|
{
|
2017-03-21 00:13:33 +01:00
|
|
|
name: "forwarded host takes priority over for",
|
2016-10-13 17:59:49 +02:00
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"Forwarded": []string{`host="reg.example.com:5000";for="192.168.22.30"`},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
2017-03-21 00:13:33 +01:00
|
|
|
base: "http://reg.example.com:5000",
|
2016-10-13 17:59:49 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "forwarded host and protocol using standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"Forwarded": []string{`host=reg.example.com;proto=https`},
|
|
|
|
}},
|
|
|
|
base: "https://reg.example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "process just the first standard forwarded header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"Forwarded": []string{`host="reg.example.com:88";proto=http`, `host=reg.example.com;proto=https`},
|
|
|
|
}},
|
|
|
|
base: "http://reg.example.com:88",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "process just the first list element of standard header",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"Forwarded": []string{`host="reg.example.com:443";proto=https, host="reg.example.com:80";proto=http`},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
|
|
|
base: "https://reg.example.com:443",
|
|
|
|
},
|
|
|
|
{
|
2017-03-21 00:13:33 +01:00
|
|
|
name: "IPv6 address use host",
|
2016-10-13 17:59:49 +02:00
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"Forwarded": []string{`for="2607:f0d0:1002:51::4";host="[2607:f0d0:1002:51::4]:5001"`},
|
|
|
|
"X-Forwarded-Port": []string{"5002"},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
|
|
|
base: "http://[2607:f0d0:1002:51::4]:5001",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "IPv6 address with port",
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"Forwarded": []string{`host="[2607:f0d0:1002:51::4]:4000"`},
|
2016-10-13 17:59:49 +02:00
|
|
|
"X-Forwarded-Port": []string{"5001"},
|
|
|
|
}},
|
|
|
|
base: "http://[2607:f0d0:1002:51::4]:4000",
|
|
|
|
},
|
|
|
|
{
|
2017-03-21 00:13:33 +01:00
|
|
|
name: "non-standard and standard forward headers",
|
2016-10-13 17:59:49 +02:00
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Proto": []string{`https`},
|
|
|
|
"X-Forwarded-Host": []string{`first.example.com`},
|
|
|
|
"X-Forwarded-Port": []string{``},
|
|
|
|
"Forwarded": []string{`host=first.example.com; proto=https`},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
2017-03-21 00:13:33 +01:00
|
|
|
base: "https://first.example.com",
|
2016-10-13 17:59:49 +02:00
|
|
|
},
|
|
|
|
{
|
2017-03-21 00:13:33 +01:00
|
|
|
name: "standard header takes precedence over non-standard headers",
|
2016-10-13 17:59:49 +02:00
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Proto": []string{`http`},
|
|
|
|
"Forwarded": []string{`host=second.example.com; proto=https`},
|
|
|
|
"X-Forwarded-Host": []string{`first.example.com`},
|
|
|
|
"X-Forwarded-Port": []string{`4000`},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
2017-03-21 00:13:33 +01:00
|
|
|
base: "https://second.example.com",
|
2016-10-13 17:59:49 +02:00
|
|
|
},
|
|
|
|
{
|
2017-03-21 00:13:33 +01:00
|
|
|
name: "incomplete standard header uses default",
|
2016-10-13 17:59:49 +02:00
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
|
|
|
"X-Forwarded-Proto": []string{`https`},
|
2017-03-21 00:13:33 +01:00
|
|
|
"Forwarded": []string{`for=127.0.0.1`},
|
2016-10-13 17:59:49 +02:00
|
|
|
"X-Forwarded-Host": []string{`first.example.com`},
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Port": []string{`4000`},
|
2016-10-13 17:59:49 +02:00
|
|
|
}},
|
2017-03-21 00:13:33 +01:00
|
|
|
base: "http://" + u.Host,
|
2016-10-13 17:59:49 +02:00
|
|
|
},
|
|
|
|
{
|
2017-03-21 00:13:33 +01:00
|
|
|
name: "standard with just proto",
|
2016-10-13 17:59:49 +02:00
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: http.Header{
|
2017-03-21 00:13:33 +01:00
|
|
|
"X-Forwarded-Proto": []string{`https`},
|
|
|
|
"Forwarded": []string{`proto=https`},
|
2016-10-13 17:59:49 +02:00
|
|
|
"X-Forwarded-Host": []string{`first.example.com`},
|
|
|
|
"X-Forwarded-Port": []string{`4000`},
|
|
|
|
}},
|
2017-03-21 00:13:33 +01:00
|
|
|
base: "https://" + u.Host,
|
2016-10-13 17:59:49 +02:00
|
|
|
},
|
2015-01-31 20:43:06 +01:00
|
|
|
}
|
2016-10-13 17:59:49 +02:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest := func(relative bool) {
|
|
|
|
for _, tr := range testRequests {
|
|
|
|
var builder *URLBuilder
|
|
|
|
if tr.configHost.Scheme != "" && tr.configHost.Host != "" {
|
|
|
|
builder = NewURLBuilder(&tr.configHost, relative)
|
|
|
|
} else {
|
|
|
|
builder = NewURLBuilderFromRequest(tr.request, relative)
|
2015-01-31 20:43:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
for _, testCase := range makeURLBuilderTestCases(builder) {
|
|
|
|
buildURL, err := testCase.build()
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr := testCase.expectedErr
|
|
|
|
if !reflect.DeepEqual(expectedErr, err) {
|
|
|
|
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
|
|
|
|
}
|
|
|
|
if expectedErr != nil {
|
|
|
|
continue
|
2015-10-15 10:56:28 +02:00
|
|
|
}
|
2015-01-31 20:43:06 +01:00
|
|
|
|
2017-03-21 00:13:33 +01:00
|
|
|
expectedURL := testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = tr.base + expectedURL
|
2016-02-23 02:49:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if buildURL != expectedURL {
|
2016-10-13 17:59:49 +02:00
|
|
|
t.Errorf("[relative=%t, request=%q, case=%q]: %q != %q", relative, tr.name, testCase.description, buildURL, expectedURL)
|
2016-02-23 02:49:23 +01:00
|
|
|
}
|
2015-01-31 20:43:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 17:59:49 +02:00
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
doTest(true)
|
|
|
|
doTest(false)
|
2014-12-13 00:48:41 +01:00
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
|
|
|
func TestBuilderFromRequestWithPrefix(t *testing.T) {
|
|
|
|
u, err := url.Parse("http://example.com/prefix/v2/")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forwardedProtoHeader := make(http.Header, 1)
|
|
|
|
forwardedProtoHeader.Set("X-Forwarded-Proto", "https")
|
|
|
|
|
|
|
|
testRequests := []struct {
|
2015-09-18 20:03:15 +02:00
|
|
|
request *http.Request
|
|
|
|
base string
|
|
|
|
configHost url.URL
|
2015-02-24 23:59:01 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host},
|
|
|
|
base: "http://example.com/prefix/",
|
|
|
|
},
|
2015-10-15 10:56:28 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "http://example.com/prefix/",
|
|
|
|
},
|
2015-02-24 23:59:01 +01:00
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "https://example.com/prefix/",
|
|
|
|
},
|
2015-09-18 20:03:15 +02:00
|
|
|
{
|
|
|
|
request: &http.Request{URL: u, Host: u.Host, Header: forwardedProtoHeader},
|
|
|
|
base: "https://subdomain.example.com/prefix/",
|
|
|
|
configHost: url.URL{
|
|
|
|
Scheme: "https",
|
2015-10-25 18:01:45 +01:00
|
|
|
Host: "subdomain.example.com",
|
|
|
|
Path: "/prefix/",
|
2015-09-18 20:03:15 +02:00
|
|
|
},
|
|
|
|
},
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:49:23 +01:00
|
|
|
var relative bool
|
2015-02-24 23:59:01 +01:00
|
|
|
for _, tr := range testRequests {
|
2015-09-18 20:03:15 +02:00
|
|
|
var builder *URLBuilder
|
|
|
|
if tr.configHost.Scheme != "" && tr.configHost.Host != "" {
|
2016-02-23 02:49:23 +01:00
|
|
|
builder = NewURLBuilder(&tr.configHost, false)
|
2015-09-18 20:03:15 +02:00
|
|
|
} else {
|
2016-02-23 02:49:23 +01:00
|
|
|
builder = NewURLBuilderFromRequest(tr.request, false)
|
2015-09-18 20:03:15 +02:00
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
|
|
|
for _, testCase := range makeURLBuilderTestCases(builder) {
|
2015-10-15 10:56:28 +02:00
|
|
|
buildURL, err := testCase.build()
|
2017-03-01 19:24:08 +01:00
|
|
|
expectedErr := testCase.expectedErr
|
|
|
|
if !reflect.DeepEqual(expectedErr, err) {
|
|
|
|
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
|
|
|
|
}
|
|
|
|
if expectedErr != nil {
|
|
|
|
continue
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
2016-02-23 02:49:23 +01:00
|
|
|
|
2015-10-15 10:56:28 +02:00
|
|
|
var expectedURL string
|
|
|
|
proto, ok := tr.request.Header["X-Forwarded-Proto"]
|
|
|
|
if !ok {
|
2016-02-23 02:49:23 +01:00
|
|
|
expectedURL = testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = tr.base[0:len(tr.base)-1] + expectedURL
|
|
|
|
}
|
2015-10-15 10:56:28 +02:00
|
|
|
} else {
|
|
|
|
urlBase, err := url.Parse(tr.base)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
urlBase.Scheme = proto[0]
|
2016-02-23 02:49:23 +01:00
|
|
|
expectedURL = testCase.expectedPath
|
|
|
|
if !relative {
|
|
|
|
expectedURL = urlBase.String()[0:len(urlBase.String())-1] + expectedURL
|
|
|
|
}
|
|
|
|
|
2015-10-15 10:56:28 +02:00
|
|
|
}
|
2015-02-24 23:59:01 +01:00
|
|
|
|
2015-10-15 10:56:28 +02:00
|
|
|
if buildURL != expectedURL {
|
|
|
|
t.Fatalf("%s: %q != %q", testCase.description, buildURL, expectedURL)
|
2015-02-24 23:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|