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