diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-23 15:10:36 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-23 15:10:36 +0300 |
commit | 2c4e24ec6cbb00b849d2bb1c827a5b8163ba6f3c (patch) | |
tree | 4375c49ebd812a351151690443432f85731b6767 | |
parent | d7233de6883fbb83cf445b19cdc95a119caa52b8 (diff) |
Write tests for serving the 404.html page
-rw-r--r-- | domain.go | 20 | ||||
-rw-r--r-- | domain_test.go | 44 | ||||
-rw-r--r-- | domains_test.go | 2 | ||||
-rw-r--r-- | shared/pages/group.404/domain.404/config.json | 7 | ||||
-rw-r--r-- | shared/pages/group.404/domain.404/public/404.html | 1 | ||||
-rw-r--r-- | shared/pages/group.404/group.404.test.io/public/404.html | 1 | ||||
-rw-r--r-- | shared/pages/group.404/project.404.symlink/public/404.html | 1 | ||||
-rw-r--r-- | shared/pages/group.404/project.404/public/404.html | 1 | ||||
-rw-r--r-- | shared/pages/group.404/project.no.404/public/index.html | 1 |
9 files changed, 65 insertions, 13 deletions
@@ -68,10 +68,10 @@ func (d *domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code in return nil } -func (d *domain) resolvePath(projectName, subPath string) (fullPath string, err error) { +func (d *domain) resolvePath(projectName string, subPath ...string) (fullPath string, err error) { publicPath := filepath.Join(d.Group, projectName, "public") - fullPath = filepath.Join(publicPath, subPath) + fullPath = filepath.Join(publicPath, filepath.Join(subPath...)) fullPath, err = filepath.EvalSymlinks(fullPath) if err != nil { return @@ -132,8 +132,8 @@ func (d *domain) checkPath(w http.ResponseWriter, r *http.Request, path string) return } -func (d *domain) tryFile(w http.ResponseWriter, r *http.Request, projectName, subPath string) error { - path, err := d.resolvePath(projectName, subPath) +func (d *domain) tryFile(w http.ResponseWriter, r *http.Request, projectName string, subPath ...string) error { + path, err := d.resolvePath(projectName, subPath...) if err != nil { return err } @@ -149,14 +149,8 @@ func (d *domain) serveFromGroup(w http.ResponseWriter, r *http.Request) { split := strings.SplitN(r.URL.Path, "/", 3) // Try to serve file for http://group.example.com/subpath/... => /group/subpath/... - if len(split) >= 2 { - subPath := "" - if len(split) >= 3 { - subPath = split[2] - } - if d.tryFile(w, r, split[1], subPath) == nil { - return - } + if len(split) >= 2 && d.tryFile(w, r, split[1], split[2:]...) == nil { + return } // Try to serve file for http://group.example.com/... => /group/group.example.com/... @@ -170,7 +164,7 @@ func (d *domain) serveFromGroup(w http.ResponseWriter, r *http.Request) { } // Try serving not found page for http://group.example.com/ => /group/group.example.com/404.html - if r.Host != "" && d.tryFile(w, r, strings.ToLower(r.Host), r.URL.Path) == nil { + if r.Host != "" && d.tryNotFound(w, r, strings.ToLower(r.Host)) == nil { return } diff --git a/domain_test.go b/domain_test.go index 4af2feb5..d1dca06e 100644 --- a/domain_test.go +++ b/domain_test.go @@ -2,6 +2,8 @@ package main import ( "github.com/stretchr/testify/assert" + "net/http" + "net/url" "testing" ) @@ -46,6 +48,48 @@ func TestDomainServeHTTP(t *testing.T) { assert.HTTPError(t, testDomain.ServeHTTP, "GET", "/not-existing-file", nil) } +func testHTTP404(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, str interface{}) bool { + if !assert.HTTPError(t, handler, mode, url, values) { + return false + } + if !assert.HTTPBodyContains(t, handler, mode, url, values, str) { + return false + } + return true +} + +func TestGroup404ServeHTTP(t *testing.T) { + setUpTests() + + testGroup := &domain{ + Group: "group.404", + Project: "", + } + + testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.404/not/existing-file", nil, "Custom 404 project page") + testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.404/", nil, "Custom 404 project page") + testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.no.404/not/existing-file", nil, "Custom 404 group page") + testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/not/existing-file", nil, "Custom 404 group page") + testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/not-existing-file", nil, "Custom 404 group page") + testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/", nil, "Custom 404 group page") + assert.HTTPBodyNotContains(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.404.symlink/not/existing-file", nil, "Custom 404 project page") +} + +func TestDomain404ServeHTTP(t *testing.T) { + setUpTests() + + testDomain := &domain{ + Group: "group.404", + Project: "domain.404", + Config: &domainConfig{ + Domain: "domain.404.com", + }, + } + + testHTTP404(t, testDomain.ServeHTTP, "GET", "http://group.404.test.io/not-existing-file", nil, "Custom 404 group page") + testHTTP404(t, testDomain.ServeHTTP, "GET", "http://group.404.test.io/", nil, "Custom 404 group page") +} + func TestGroupCertificate(t *testing.T) { testGroup := &domain{ Group: "group", diff --git a/domains_test.go b/domains_test.go index 8c3b1e5e..5a2882d6 100644 --- a/domains_test.go +++ b/domains_test.go @@ -29,6 +29,8 @@ func TestReadProjects(t *testing.T) { "group.internal.test.io", "test.domain.com", // from config.json "other.domain.com", + "domain.404.com", + "group.404.test.io", } for _, expected := range domains { diff --git a/shared/pages/group.404/domain.404/config.json b/shared/pages/group.404/domain.404/config.json new file mode 100644 index 00000000..c2adb9f6 --- /dev/null +++ b/shared/pages/group.404/domain.404/config.json @@ -0,0 +1,7 @@ +{ + "Domains": [ + { + "Domain": "domain.404.com" + } + ] +} diff --git a/shared/pages/group.404/domain.404/public/404.html b/shared/pages/group.404/domain.404/public/404.html new file mode 100644 index 00000000..454a3d50 --- /dev/null +++ b/shared/pages/group.404/domain.404/public/404.html @@ -0,0 +1 @@ +Custom 404 group page diff --git a/shared/pages/group.404/group.404.test.io/public/404.html b/shared/pages/group.404/group.404.test.io/public/404.html new file mode 100644 index 00000000..454a3d50 --- /dev/null +++ b/shared/pages/group.404/group.404.test.io/public/404.html @@ -0,0 +1 @@ +Custom 404 group page diff --git a/shared/pages/group.404/project.404.symlink/public/404.html b/shared/pages/group.404/project.404.symlink/public/404.html new file mode 100644 index 00000000..896a32d0 --- /dev/null +++ b/shared/pages/group.404/project.404.symlink/public/404.html @@ -0,0 +1 @@ +Custom 404 project page diff --git a/shared/pages/group.404/project.404/public/404.html b/shared/pages/group.404/project.404/public/404.html new file mode 100644 index 00000000..896a32d0 --- /dev/null +++ b/shared/pages/group.404/project.404/public/404.html @@ -0,0 +1 @@ +Custom 404 project page diff --git a/shared/pages/group.404/project.no.404/public/index.html b/shared/pages/group.404/project.no.404/public/index.html new file mode 100644 index 00000000..896c9cfc --- /dev/null +++ b/shared/pages/group.404/project.no.404/public/index.html @@ -0,0 +1 @@ +Index page |