Commit 49b0cdef authored by Kamil Trzcinski's avatar Kamil Trzcinski

Merge commit '3bbb1be7' into artifacts-metadata

* commit '3bbb1be7':
  Version 0.6.0
  Gotta commit those moved files
  Develomentmode ended up the wrong way around
  Move test helpers into seperate package
  Prevent package testing from going into the build
parents 16154719 3bbb1be7
......@@ -2,6 +2,12 @@
Formerly known as 'gitlab-git-http-server'.
0.6.0
Overhauled the source code organization; no user-facing changes
(intended). The application code is now split into Go 'packages'
(modules).
0.5.4
Fix /api/v3/projects routing bug introduced in 0.5.2-0.5.3.
......
......@@ -3,6 +3,7 @@ package main
import (
"./internal/api"
"./internal/helper"
"./internal/testhelper"
"fmt"
"net/http"
"net/http/httptest"
......@@ -29,7 +30,7 @@ func runPreAuthorizeHandler(t *testing.T, suffix string, url *regexp.Regexp, api
response := httptest.NewRecorder()
a.PreAuthorizeHandler(okHandler, suffix).ServeHTTP(response, httpRequest)
helper.AssertResponseCode(t, response, expectedCode)
testhelper.AssertResponseCode(t, response, expectedCode)
return response
}
......
package staticpages
import (
"../helper"
"../testhelper"
"io/ioutil"
"net/http"
"net/http/httptest"
......@@ -51,6 +51,6 @@ func TestIfDeployPageExist(t *testing.T) {
}
w.Flush()
helper.AssertResponseCode(t, w, 200)
helper.AssertResponseBody(t, w, deployPage)
testhelper.AssertResponseCode(t, w, 200)
testhelper.AssertResponseBody(t, w, deployPage)
}
......@@ -60,8 +60,8 @@ func (s *errorPageResponseWriter) Flush() {
s.WriteHeader(http.StatusOK)
}
func (st *Static) ErrorPages(enabled bool, handler http.Handler) http.Handler {
if !enabled {
func (st *Static) ErrorPagesUnless(disabled bool, handler http.Handler) http.Handler {
if disabled {
return handler
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
......
package staticpages
import (
"../helper"
"../testhelper"
"fmt"
"io/ioutil"
"net/http"
......@@ -27,11 +27,11 @@ func TestIfErrorPageIsPresented(t *testing.T) {
fmt.Fprint(w, "Not Found")
})
st := &Static{dir}
st.ErrorPages(true, h).ServeHTTP(w, nil)
st.ErrorPagesUnless(false, h).ServeHTTP(w, nil)
w.Flush()
helper.AssertResponseCode(t, w, 404)
helper.AssertResponseBody(t, w, errorPage)
testhelper.AssertResponseCode(t, w, 404)
testhelper.AssertResponseBody(t, w, errorPage)
}
func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
......@@ -48,11 +48,11 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
fmt.Fprint(w, errorResponse)
})
st := &Static{dir}
st.ErrorPages(true, h).ServeHTTP(w, nil)
st.ErrorPagesUnless(false, h).ServeHTTP(w, nil)
w.Flush()
helper.AssertResponseCode(t, w, 404)
helper.AssertResponseBody(t, w, errorResponse)
testhelper.AssertResponseCode(t, w, 404)
testhelper.AssertResponseBody(t, w, errorResponse)
}
func TestIfErrorPageIsIgnoredInDevelopment(t *testing.T) {
......@@ -72,8 +72,8 @@ func TestIfErrorPageIsIgnoredInDevelopment(t *testing.T) {
fmt.Fprint(w, serverError)
})
st := &Static{dir}
st.ErrorPages(false, h).ServeHTTP(w, nil)
st.ErrorPagesUnless(true, h).ServeHTTP(w, nil)
w.Flush()
helper.AssertResponseCode(t, w, 500)
helper.AssertResponseBody(t, w, serverError)
testhelper.AssertResponseCode(t, w, 500)
testhelper.AssertResponseBody(t, w, serverError)
}
package staticpages
import (
"../helper"
"../testhelper"
"bytes"
"compress/gzip"
"io/ioutil"
......@@ -19,7 +19,7 @@ func TestServingNonExistingFile(t *testing.T) {
w := httptest.NewRecorder()
st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
testhelper.AssertResponseCode(t, w, 404)
}
func TestServingDirectory(t *testing.T) {
......@@ -33,7 +33,7 @@ func TestServingDirectory(t *testing.T) {
w := httptest.NewRecorder()
st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
testhelper.AssertResponseCode(t, w, 404)
}
func TestServingMalformedUri(t *testing.T) {
......@@ -43,7 +43,7 @@ func TestServingMalformedUri(t *testing.T) {
w := httptest.NewRecorder()
st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 404)
testhelper.AssertResponseCode(t, w, 404)
}
func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
......@@ -75,7 +75,7 @@ func TestServingTheActualFile(t *testing.T) {
w := httptest.NewRecorder()
st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 200)
testhelper.AssertResponseCode(t, w, 200)
if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String())
}
......@@ -107,15 +107,15 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
w := httptest.NewRecorder()
st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 200)
testhelper.AssertResponseCode(t, w, 200)
if enableGzip {
helper.AssertResponseHeader(t, w, "Content-Encoding", "gzip")
testhelper.AssertResponseHeader(t, w, "Content-Encoding", "gzip")
if bytes.Compare(w.Body.Bytes(), fileGzipContent.Bytes()) != 0 {
t.Error("We should serve the pregzipped file")
}
} else {
helper.AssertResponseCode(t, w, 200)
helper.AssertResponseHeader(t, w, "Content-Encoding", "")
testhelper.AssertResponseCode(t, w, 200)
testhelper.AssertResponseHeader(t, w, "Content-Encoding", "")
if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String())
}
......
package helper
package testhelper
import (
"log"
......
......@@ -3,6 +3,7 @@ package upload
import (
"../helper"
"../proxy"
"../testhelper"
"bytes"
"fmt"
"io"
......@@ -22,11 +23,11 @@ func TestUploadTempPathRequirement(t *testing.T) {
response := httptest.NewRecorder()
request := &http.Request{}
handleFileUploads(nilHandler).ServeHTTP(response, request)
helper.AssertResponseCode(t, response, 500)
testhelper.AssertResponseCode(t, response, 500)
}
func TestUploadHandlerForwardingRawData(t *testing.T) {
ts := helper.TestServerWithHandler(regexp.MustCompile(`/url/path\z`), func(w http.ResponseWriter, r *http.Request) {
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`/url/path\z`), func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PATCH" {
t.Fatal("Expected PATCH request")
}
......@@ -58,7 +59,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
httpRequest.Header.Set(tempPathHeader, tempPath)
handleFileUploads(proxy.NewProxy(helper.URLMustParse(ts.URL), "123", nil)).ServeHTTP(response, httpRequest)
helper.AssertResponseCode(t, response, 202)
testhelper.AssertResponseCode(t, response, 202)
if response.Body.String() != "RESPONSE" {
t.Fatal("Expected RESPONSE in response body")
}
......@@ -73,7 +74,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
}
defer os.RemoveAll(tempPath)
ts := helper.TestServerWithHandler(regexp.MustCompile(`/url/path\z`), func(w http.ResponseWriter, r *http.Request) {
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`/url/path\z`), func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
t.Fatal("Expected PUT request")
}
......@@ -132,7 +133,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
response := httptest.NewRecorder()
handleFileUploads(proxy.NewProxy(helper.URLMustParse(ts.URL), "123", nil)).ServeHTTP(response, httpRequest)
helper.AssertResponseCode(t, response, 202)
testhelper.AssertResponseCode(t, response, 202)
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
t.Fatal("expected the file to be deleted")
......
package upstream
import (
"../helper"
"../testhelper"
"net/http"
"net/http/httptest"
"testing"
......@@ -35,5 +35,5 @@ func TestDevelopmentModeDisabled(t *testing.T) {
if executed {
t.Error("The handler should not get executed")
}
helper.AssertResponseCode(t, w, 404)
testhelper.AssertResponseCode(t, w, 404)
}
package upstream
import (
"../helper"
"../testhelper"
"bytes"
"compress/gzip"
"fmt"
......@@ -37,7 +37,7 @@ func TestGzipEncoding(t *testing.T) {
}
})).ServeHTTP(resp, req)
helper.AssertResponseCode(t, resp, 200)
testhelper.AssertResponseCode(t, resp, 200)
}
func TestNoEncoding(t *testing.T) {
......@@ -61,7 +61,7 @@ func TestNoEncoding(t *testing.T) {
}
})).ServeHTTP(resp, req)
helper.AssertResponseCode(t, resp, 200)
testhelper.AssertResponseCode(t, resp, 200)
}
func TestInvalidEncoding(t *testing.T) {
......@@ -77,5 +77,5 @@ func TestInvalidEncoding(t *testing.T) {
t.Fatal("it shouldn't be executed")
})).ServeHTTP(resp, req)
helper.AssertResponseCode(t, resp, 500)
testhelper.AssertResponseCode(t, resp, 500)
}
......@@ -85,13 +85,13 @@ func (u *Upstream) configureRoutes() {
// To prevent anybody who knows/guesses the URL of a user-uploaded file
// from downloading it we make sure requests to /uploads/ do _not_ pass
// through static.ServeExisting.
route{"", regexp.MustCompile(`^/uploads/`), static.ErrorPages(u.DevelopmentMode, proxy)},
route{"", regexp.MustCompile(`^/uploads/`), static.ErrorPagesUnless(u.DevelopmentMode, proxy)},
// Serve static files or forward the requests
route{"", nil,
static.ServeExisting(u.URLPrefix, staticpages.CacheDisabled,
static.DeployPage(
static.ErrorPages(u.DevelopmentMode,
static.ErrorPagesUnless(u.DevelopmentMode,
proxy,
),
),
......
......@@ -3,6 +3,7 @@ package main
import (
"./internal/api"
"./internal/helper"
"./internal/testhelper"
"./internal/upstream"
"bytes"
"encoding/json"
......@@ -329,7 +330,7 @@ func TestAllowedStaticFile(t *testing.T) {
}
proxied := false
ts := helper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
proxied = true
w.WriteHeader(404)
})
......@@ -369,7 +370,7 @@ func TestAllowedPublicUploadsFile(t *testing.T) {
}
proxied := false
ts := helper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
proxied = true
w.Header().Add("X-Sendfile", *documentRoot+r.URL.Path)
w.WriteHeader(200)
......@@ -410,7 +411,7 @@ func TestDeniedPublicUploadsFile(t *testing.T) {
}
proxied := false
ts := helper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, _ *http.Request) {
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, _ *http.Request) {
proxied = true
w.WriteHeader(404)
})
......@@ -453,7 +454,7 @@ func TestArtifactsUpload(t *testing.T) {
fmt.Fprint(file, "SHOULD BE ON DISK, NOT IN MULTIPART")
writer.Close()
ts := helper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/authorize") {
if _, err := fmt.Fprintf(w, `{"TempPath":"%s"}`, scratchDir); err != nil {
t.Fatal(err)
......@@ -525,7 +526,7 @@ func newBranch() string {
}
func testAuthServer(url *regexp.Regexp, code int, body interface{}) *httptest.Server {
return helper.TestServerWithHandler(url, func(w http.ResponseWriter, r *http.Request) {
return testhelper.TestServerWithHandler(url, func(w http.ResponseWriter, r *http.Request) {
// Write pure string
if data, ok := body.(string); ok {
log.Println("UPSTREAM", r.Method, r.URL, code)
......
......@@ -4,6 +4,7 @@ import (
"./internal/badgateway"
"./internal/helper"
"./internal/proxy"
"./internal/testhelper"
"bytes"
"fmt"
"io"
......@@ -20,7 +21,7 @@ func newProxy(url string, rt *badgateway.RoundTripper) *proxy.Proxy {
}
func TestProxyRequest(t *testing.T) {
ts := helper.TestServerWithHandler(regexp.MustCompile(`/url/path\z`), func(w http.ResponseWriter, r *http.Request) {
ts := testhelper.TestServerWithHandler(regexp.MustCompile(`/url/path\z`), func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatal("Expected POST request")
}
......@@ -48,8 +49,8 @@ func TestProxyRequest(t *testing.T) {
w := httptest.NewRecorder()
newProxy(ts.URL, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 202)
helper.AssertResponseBody(t, w, "RESPONSE")
testhelper.AssertResponseCode(t, w, 202)
testhelper.AssertResponseBody(t, w, "RESPONSE")
if w.Header().Get("Custom-Response-Header") != "test" {
t.Fatal("Expected custom response header")
......@@ -65,12 +66,12 @@ func TestProxyError(t *testing.T) {
w := httptest.NewRecorder()
newProxy("http://localhost:655575/", nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 502)
helper.AssertResponseBody(t, w, "dial tcp: invalid port 655575")
testhelper.AssertResponseCode(t, w, 502)
testhelper.AssertResponseBody(t, w, "dial tcp: invalid port 655575")
}
func TestProxyReadTimeout(t *testing.T) {
ts := helper.TestServerWithHandler(nil, func(w http.ResponseWriter, r *http.Request) {
ts := testhelper.TestServerWithHandler(nil, func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Minute)
})
......@@ -94,12 +95,12 @@ func TestProxyReadTimeout(t *testing.T) {
p := newProxy(ts.URL, rt)
w := httptest.NewRecorder()
p.ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 502)
helper.AssertResponseBody(t, w, "net/http: timeout awaiting response headers")
testhelper.AssertResponseCode(t, w, 502)
testhelper.AssertResponseBody(t, w, "net/http: timeout awaiting response headers")
}
func TestProxyHandlerTimeout(t *testing.T) {
ts := helper.TestServerWithHandler(nil,
ts := testhelper.TestServerWithHandler(nil,
http.TimeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Second)
}), time.Millisecond, "Request took too long").ServeHTTP,
......@@ -112,6 +113,6 @@ func TestProxyHandlerTimeout(t *testing.T) {
w := httptest.NewRecorder()
newProxy(ts.URL, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 503)
helper.AssertResponseBody(t, w, "Request took too long")
testhelper.AssertResponseCode(t, w, 503)
testhelper.AssertResponseBody(t, w, "Request took too long")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment