Commit 27900114 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Move errorpage tests into their package

parent 244e1b98
......@@ -10,6 +10,7 @@ install: gitlab-workhorse
.PHONY: test
test: test/data/group/test.git clean-workhorse gitlab-workhorse
go fmt | awk '{ print "Please run go fmt"; exit 1 }'
go test ./internal/...
go test
coverage: test/data/group/test.git
......
......@@ -2,6 +2,7 @@ package main
import (
"./internal/api"
"./internal/helper"
"fmt"
"net/http"
"net/http/httptest"
......@@ -28,7 +29,7 @@ func runPreAuthorizeHandler(t *testing.T, suffix string, url *regexp.Regexp, api
response := httptest.NewRecorder()
api.PreAuthorizeHandler(okHandler, suffix)(response, httpRequest)
assertResponseCode(t, response, expectedCode)
helper.AssertResponseCode(t, response, expectedCode)
return response
}
......
package main
import (
"./internal/helper"
"io/ioutil"
"net/http"
"net/http/httptest"
......@@ -48,6 +49,6 @@ func TestIfDeployPageExist(t *testing.T) {
}
w.Flush()
assertResponseCode(t, w, 200)
assertResponseBody(t, w, deployPage)
helper.AssertResponseCode(t, w, 200)
helper.AssertResponseBody(t, w, deployPage)
}
package main
import (
"./internal/helper"
"net/http"
"net/http/httptest"
"testing"
......@@ -34,5 +35,5 @@ func TestDevelopmentModeDisabled(t *testing.T) {
if executed {
t.Error("The handler should not get executed")
}
assertResponseCode(t, w, 404)
helper.AssertResponseCode(t, w, 404)
}
package main
import (
"./internal/helper"
"bytes"
"compress/gzip"
"fmt"
......@@ -36,7 +37,7 @@ func TestGzipEncoding(t *testing.T) {
}
})(resp, req)
assertResponseCode(t, resp, 200)
helper.AssertResponseCode(t, resp, 200)
}
func TestNoEncoding(t *testing.T) {
......@@ -60,7 +61,7 @@ func TestNoEncoding(t *testing.T) {
}
})(resp, req)
assertResponseCode(t, resp, 200)
helper.AssertResponseCode(t, resp, 200)
}
func TestInvalidEncoding(t *testing.T) {
......@@ -76,5 +77,5 @@ func TestInvalidEncoding(t *testing.T) {
t.Fatal("it shouldn't be executed")
})(resp, req)
assertResponseCode(t, resp, 500)
helper.AssertResponseCode(t, resp, 500)
}
package errorpage
import (
"../helper"
"../helper"
"fmt"
"io/ioutil"
"log"
......
package main
package errorpage
import (
"./internal/errorpage"
"../helper"
"fmt"
"io/ioutil"
"net/http"
......@@ -26,11 +26,11 @@ func TestIfErrorPageIsPresented(t *testing.T) {
w.WriteHeader(404)
fmt.Fprint(w, "Not Found")
})
errorpage.Inject(dir, h)(w, nil)
Inject(dir, h)(w, nil)
w.Flush()
assertResponseCode(t, w, 404)
assertResponseBody(t, w, errorPage)
helper.AssertResponseCode(t, w, 404)
helper.AssertResponseBody(t, w, errorPage)
}
func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
......@@ -46,9 +46,9 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
w.WriteHeader(404)
fmt.Fprint(w, errorResponse)
})
errorpage.Inject(dir, h)(w, nil)
Inject(dir, h)(w, nil)
w.Flush()
assertResponseCode(t, w, 404)
assertResponseBody(t, w, errorResponse)
helper.AssertResponseCode(t, w, 404)
helper.AssertResponseBody(t, w, errorResponse)
}
package main
package helper
import (
"net/http/httptest"
"testing"
)
func assertResponseCode(t *testing.T, response *httptest.ResponseRecorder, expectedCode int) {
func AssertResponseCode(t *testing.T, response *httptest.ResponseRecorder, expectedCode int) {
if response.Code != expectedCode {
t.Fatalf("for HTTP request expected to get %d, got %d instead", expectedCode, response.Code)
}
}
func assertResponseBody(t *testing.T, response *httptest.ResponseRecorder, expectedBody string) {
func AssertResponseBody(t *testing.T, response *httptest.ResponseRecorder, expectedBody string) {
if response.Body.String() != expectedBody {
t.Fatalf("for HTTP request expected to receive %q, got %q instead as body", expectedBody, response.Body.String())
}
}
func assertResponseHeader(t *testing.T, response *httptest.ResponseRecorder, header string, expectedValue string) {
func AssertResponseHeader(t *testing.T, response *httptest.ResponseRecorder, header string, expectedValue string) {
if response.Header().Get(header) != expectedValue {
t.Fatalf("for HTTP request expected to receive the header %q with %q, got %q", header, expectedValue, response.Header().Get(header))
}
......
package main
import (
"./internal/helper"
"./internal/proxy"
"bytes"
"fmt"
......@@ -44,8 +45,8 @@ func TestProxyRequest(t *testing.T) {
u := newUpstream(ts.URL, "")
w := httptest.NewRecorder()
u.Proxy.ServeHTTP(w, httpRequest)
assertResponseCode(t, w, 202)
assertResponseBody(t, w, "RESPONSE")
helper.AssertResponseCode(t, w, 202)
helper.AssertResponseBody(t, w, "RESPONSE")
if w.Header().Get("Custom-Response-Header") != "test" {
t.Fatal("Expected custom response header")
......@@ -62,8 +63,8 @@ func TestProxyError(t *testing.T) {
u := newUpstream("http://localhost:655575/", "")
w := httptest.NewRecorder()
u.Proxy.ServeHTTP(w, httpRequest)
assertResponseCode(t, w, 502)
assertResponseBody(t, w, "dial tcp: invalid port 655575")
helper.AssertResponseCode(t, w, 502)
helper.AssertResponseBody(t, w, "dial tcp: invalid port 655575")
}
func TestProxyReadTimeout(t *testing.T) {
......@@ -97,8 +98,8 @@ func TestProxyReadTimeout(t *testing.T) {
w := httptest.NewRecorder()
u.Proxy.ServeHTTP(w, httpRequest)
assertResponseCode(t, w, 502)
assertResponseBody(t, w, "net/http: timeout awaiting response headers")
helper.AssertResponseCode(t, w, 502)
helper.AssertResponseBody(t, w, "net/http: timeout awaiting response headers")
}
func TestProxyHandlerTimeout(t *testing.T) {
......@@ -117,6 +118,6 @@ func TestProxyHandlerTimeout(t *testing.T) {
w := httptest.NewRecorder()
u.Proxy.ServeHTTP(w, httpRequest)
assertResponseCode(t, w, 503)
assertResponseBody(t, w, "Request took too long")
helper.AssertResponseCode(t, w, 503)
helper.AssertResponseBody(t, w, "Request took too long")
}
package main
import (
"./internal/helper"
"bytes"
"compress/gzip"
"io/ioutil"
......@@ -19,7 +20,7 @@ func TestServingNonExistingFile(t *testing.T) {
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(&dir, CacheDisabled, nil)(w, httpRequest)
assertResponseCode(t, w, 404)
helper.AssertResponseCode(t, w, 404)
}
func TestServingDirectory(t *testing.T) {
......@@ -32,7 +33,7 @@ func TestServingDirectory(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/file", nil)
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(&dir, CacheDisabled, nil)(w, httpRequest)
assertResponseCode(t, w, 404)
helper.AssertResponseCode(t, w, 404)
}
func TestServingMalformedUri(t *testing.T) {
......@@ -41,7 +42,7 @@ func TestServingMalformedUri(t *testing.T) {
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(&dir, CacheDisabled, nil)(w, httpRequest)
assertResponseCode(t, w, 404)
helper.AssertResponseCode(t, w, 404)
}
func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
......@@ -71,7 +72,7 @@ func TestServingTheActualFile(t *testing.T) {
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(&dir, CacheDisabled, nil)(w, httpRequest)
assertResponseCode(t, w, 200)
helper.AssertResponseCode(t, w, 200)
if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String())
}
......@@ -102,15 +103,15 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
w := httptest.NewRecorder()
dummyUpstream.handleServeFile(&dir, CacheDisabled, nil)(w, httpRequest)
assertResponseCode(t, w, 200)
helper.AssertResponseCode(t, w, 200)
if enableGzip {
assertResponseHeader(t, w, "Content-Encoding", "gzip")
helper.AssertResponseHeader(t, w, "Content-Encoding", "gzip")
if bytes.Compare(w.Body.Bytes(), fileGzipContent.Bytes()) != 0 {
t.Error("We should serve the pregzipped file")
}
} else {
assertResponseCode(t, w, 200)
assertResponseHeader(t, w, "Content-Encoding", "")
helper.AssertResponseCode(t, w, 200)
helper.AssertResponseHeader(t, w, "Content-Encoding", "")
if w.Body.String() != fileContent {
t.Error("We should serve the file: ", w.Body.String())
}
......
package main
import (
"./internal/helper"
"bytes"
"fmt"
"io"
......@@ -18,7 +19,7 @@ func TestUploadTempPathRequirement(t *testing.T) {
response := httptest.NewRecorder()
request := &http.Request{}
handleFileUploads(dummyUpstream.Proxy).ServeHTTP(response, request)
assertResponseCode(t, response, 500)
helper.AssertResponseCode(t, response, 500)
}
func TestUploadHandlerForwardingRawData(t *testing.T) {
......@@ -54,7 +55,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
u := newUpstream(ts.URL, "")
handleFileUploads(u.Proxy).ServeHTTP(response, httpRequest)
assertResponseCode(t, response, 202)
helper.AssertResponseCode(t, response, 202)
if response.Body.String() != "RESPONSE" {
t.Fatal("Expected RESPONSE in response body")
}
......@@ -129,7 +130,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
u := newUpstream(ts.URL, "")
handleFileUploads(u.Proxy).ServeHTTP(response, httpRequest)
assertResponseCode(t, response, 202)
helper.AssertResponseCode(t, response, 202)
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
t.Fatal("expected the file to be deleted")
......
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