Commit e3246cbe authored by Alessio Caiazza's avatar Alessio Caiazza

Merge branch 'jv-require-helpers' into 'master'

Tests: consistent helper function names

See merge request gitlab-org/gitlab-workhorse!566
parents 3cdfdd0c eec72348
...@@ -38,7 +38,7 @@ func runPreAuthorizeHandler(t *testing.T, ts *httptest.Server, suffix string, ur ...@@ -38,7 +38,7 @@ func runPreAuthorizeHandler(t *testing.T, ts *httptest.Server, suffix string, ur
response := httptest.NewRecorder() response := httptest.NewRecorder()
a.PreAuthorizeHandler(okHandler, suffix).ServeHTTP(response, httpRequest) a.PreAuthorizeHandler(okHandler, suffix).ServeHTTP(response, httpRequest)
testhelper.AssertResponseCode(t, response, expectedCode) testhelper.RequireResponseCode(t, response, expectedCode)
return response return response
} }
......
...@@ -35,10 +35,10 @@ func TestSingleBackend(t *testing.T) { ...@@ -35,10 +35,10 @@ func TestSingleBackend(t *testing.T) {
defer server.Close() defer server.Close()
require.NoError(t, say(client, "hello")) require.NoError(t, say(client, "hello"))
assertReadMessage(t, server, websocket.TextMessage, "hello") requireReadMessage(t, server, websocket.TextMessage, "hello")
require.NoError(t, say(server, "world")) require.NoError(t, say(server, "world"))
assertReadMessage(t, client, websocket.TextMessage, "world") requireReadMessage(t, client, websocket.TextMessage, "world")
} }
func TestSeparateCableBackend(t *testing.T) { func TestSeparateCableBackend(t *testing.T) {
...@@ -62,10 +62,10 @@ func TestSeparateCableBackend(t *testing.T) { ...@@ -62,10 +62,10 @@ func TestSeparateCableBackend(t *testing.T) {
defer server.Close() defer server.Close()
require.NoError(t, say(client, "hello")) require.NoError(t, say(client, "hello"))
assertReadMessage(t, server, websocket.TextMessage, "hello") requireReadMessage(t, server, websocket.TextMessage, "hello")
require.NoError(t, say(server, "world")) require.NoError(t, say(server, "world"))
assertReadMessage(t, client, websocket.TextMessage, "world") requireReadMessage(t, client, websocket.TextMessage, "world")
} }
func startCableServer() (chan connWithReq, *httptest.Server) { func startCableServer() (chan connWithReq, *httptest.Server) {
......
...@@ -58,18 +58,18 @@ func TestChannelHappyPath(t *testing.T) { ...@@ -58,18 +58,18 @@ func TestChannelHappyPath(t *testing.T) {
if err := say(server, "\x01"+message); err != nil { if err := say(server, "\x01"+message); err != nil {
t.Fatal(err) t.Fatal(err)
} }
assertReadMessage(t, client, websocket.BinaryMessage, message) requireReadMessage(t, client, websocket.BinaryMessage, message)
if err := say(client, message); err != nil { if err := say(client, message); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// channel.k8s.io: client writes get put on channel 0, STDIN // channel.k8s.io: client writes get put on channel 0, STDIN
assertReadMessage(t, server, websocket.BinaryMessage, "\x00"+message) requireReadMessage(t, server, websocket.BinaryMessage, "\x00"+message)
// Closing the client should send an EOT signal to the server's STDIN // Closing the client should send an EOT signal to the server's STDIN
client.Close() client.Close()
assertReadMessage(t, server, websocket.BinaryMessage, "\x00\x04") requireReadMessage(t, server, websocket.BinaryMessage, "\x00\x04")
}) })
} }
} }
...@@ -260,7 +260,7 @@ func say(conn *websocket.Conn, message string) error { ...@@ -260,7 +260,7 @@ func say(conn *websocket.Conn, message string) error {
return conn.WriteMessage(websocket.TextMessage, []byte(message)) return conn.WriteMessage(websocket.TextMessage, []byte(message))
} }
func assertReadMessage(t *testing.T, conn *websocket.Conn, expectedMessageType int, expectedData string) { func requireReadMessage(t *testing.T, conn *websocket.Conn, expectedMessageType int, expectedData string) {
messageType, data, err := conn.ReadMessage() messageType, data, err := conn.ReadMessage()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
...@@ -177,7 +177,7 @@ func TestAllowedGetGitBlob(t *testing.T) { ...@@ -177,7 +177,7 @@ func TestAllowedGetGitBlob(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode, "GET %q: status code", resp.Request.URL) assert.Equal(t, 200, resp.StatusCode, "GET %q: status code", resp.Request.URL)
assert.Equal(t, expectedBody, shortBody, "GET %q: response body", resp.Request.URL) assert.Equal(t, expectedBody, shortBody, "GET %q: response body", resp.Request.URL)
testhelper.AssertResponseHeader(t, resp, "Content-Length", strconv.Itoa(bodyLen)) testhelper.RequireResponseHeader(t, resp, "Content-Length", strconv.Itoa(bodyLen))
assertNginxResponseBuffering(t, "no", resp, "GET %q: nginx response buffering", resp.Request.URL) assertNginxResponseBuffering(t, "no", resp, "GET %q: nginx response buffering", resp.Request.URL)
} }
...@@ -317,7 +317,7 @@ func TestAllowedGetGitFormatPatch(t *testing.T) { ...@@ -317,7 +317,7 @@ func TestAllowedGetGitFormatPatch(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode, "GET %q: status code", resp.Request.URL) assert.Equal(t, 200, resp.StatusCode, "GET %q: status code", resp.Request.URL)
assertNginxResponseBuffering(t, "no", resp, "GET %q: nginx response buffering", resp.Request.URL) assertNginxResponseBuffering(t, "no", resp, "GET %q: nginx response buffering", resp.Request.URL)
testhelper.AssertPatchSeries( testhelper.RequirePatchSeries(
t, t,
body, body,
"372ab6950519549b14d220271ee2322caa44d4eb", "372ab6950519549b14d220271ee2322caa44d4eb",
......
...@@ -218,7 +218,7 @@ func TestPostReceivePackProxiedToGitalySuccessfully(t *testing.T) { ...@@ -218,7 +218,7 @@ func TestPostReceivePackProxiedToGitalySuccessfully(t *testing.T) {
assert.Equal(t, 200, resp.StatusCode, "POST %q", resource) assert.Equal(t, 200, resp.StatusCode, "POST %q", resource)
require.Equal(t, string(testhelper.GitalyReceivePackResponseMock), split[1]) require.Equal(t, string(testhelper.GitalyReceivePackResponseMock), split[1])
testhelper.AssertResponseHeader(t, resp, "Content-Type", "application/x-git-receive-pack-result") testhelper.RequireResponseHeader(t, resp, "Content-Type", "application/x-git-receive-pack-result")
} }
func TestPostReceivePackProxiedToGitalyInterrupted(t *testing.T) { func TestPostReceivePackProxiedToGitalyInterrupted(t *testing.T) {
...@@ -314,7 +314,7 @@ func TestPostUploadPackProxiedToGitalySuccessfully(t *testing.T) { ...@@ -314,7 +314,7 @@ func TestPostUploadPackProxiedToGitalySuccessfully(t *testing.T) {
) )
defer resp.Body.Close() defer resp.Body.Close()
require.Equal(t, 200, resp.StatusCode, "POST %q", resource) require.Equal(t, 200, resp.StatusCode, "POST %q", resource)
testhelper.AssertResponseHeader(t, resp, "Content-Type", "application/x-git-upload-pack-result") testhelper.RequireResponseHeader(t, resp, "Content-Type", "application/x-git-upload-pack-result")
m.Lock() m.Lock()
requestFinished := requestReadFinished requestFinished := requestReadFinished
...@@ -609,10 +609,10 @@ func TestGetSnapshotProxiedToGitalySuccessfully(t *testing.T) { ...@@ -609,10 +609,10 @@ func TestGetSnapshotProxiedToGitalySuccessfully(t *testing.T) {
assert.Equal(t, expectedBody, string(body), "GET %q: body", resp.Request.URL) assert.Equal(t, expectedBody, string(body), "GET %q: body", resp.Request.URL)
assert.Equal(t, archiveLength, len(body), "GET %q: body size", resp.Request.URL) assert.Equal(t, archiveLength, len(body), "GET %q: body size", resp.Request.URL)
testhelper.AssertResponseHeader(t, resp, "Content-Disposition", `attachment; filename="snapshot.tar"`) testhelper.RequireResponseHeader(t, resp, "Content-Disposition", `attachment; filename="snapshot.tar"`)
testhelper.AssertResponseHeader(t, resp, "Content-Type", "application/x-tar") testhelper.RequireResponseHeader(t, resp, "Content-Type", "application/x-tar")
testhelper.AssertResponseHeader(t, resp, "Content-Transfer-Encoding", "binary") testhelper.RequireResponseHeader(t, resp, "Content-Transfer-Encoding", "binary")
testhelper.AssertResponseHeader(t, resp, "Cache-Control", "private") testhelper.RequireResponseHeader(t, resp, "Cache-Control", "private")
} }
func TestGetSnapshotProxiedToGitalyInterruptedStream(t *testing.T) { func TestGetSnapshotProxiedToGitalyInterruptedStream(t *testing.T) {
......
...@@ -136,8 +136,8 @@ func TestUploadHandlerSendingToExternalStorage(t *testing.T) { ...@@ -136,8 +136,8 @@ func TestUploadHandlerSendingToExternalStorage(t *testing.T) {
contentBuffer, contentType := createTestMultipartForm(t, archiveData) contentBuffer, contentType := createTestMultipartForm(t, archiveData)
response := testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer) response := testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.RequireResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent) testhelper.RequireResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent)
assert.Equal(t, 1, storeServerCalled, "store should be called only once") assert.Equal(t, 1, storeServerCalled, "store should be called only once")
assert.Equal(t, 1, responseProcessorCalled, "response processor should be called only once") assert.Equal(t, 1, responseProcessorCalled, "response processor should be called only once")
}) })
...@@ -167,7 +167,7 @@ func TestUploadHandlerSendingToExternalStorageAndStorageServerUnreachable(t *tes ...@@ -167,7 +167,7 @@ func TestUploadHandlerSendingToExternalStorageAndStorageServerUnreachable(t *tes
defer ts.Close() defer ts.Close()
response := testUploadArtifactsFromTestZip(t, ts) response := testUploadArtifactsFromTestZip(t, ts)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.RequireResponseCode(t, response, http.StatusInternalServerError)
} }
func TestUploadHandlerSendingToExternalStorageAndInvalidURLIsUsed(t *testing.T) { func TestUploadHandlerSendingToExternalStorageAndInvalidURLIsUsed(t *testing.T) {
...@@ -193,7 +193,7 @@ func TestUploadHandlerSendingToExternalStorageAndInvalidURLIsUsed(t *testing.T) ...@@ -193,7 +193,7 @@ func TestUploadHandlerSendingToExternalStorageAndInvalidURLIsUsed(t *testing.T)
defer ts.Close() defer ts.Close()
response := testUploadArtifactsFromTestZip(t, ts) response := testUploadArtifactsFromTestZip(t, ts)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.RequireResponseCode(t, response, http.StatusInternalServerError)
} }
func TestUploadHandlerSendingToExternalStorageAndItReturnsAnError(t *testing.T) { func TestUploadHandlerSendingToExternalStorageAndItReturnsAnError(t *testing.T) {
...@@ -231,7 +231,7 @@ func TestUploadHandlerSendingToExternalStorageAndItReturnsAnError(t *testing.T) ...@@ -231,7 +231,7 @@ func TestUploadHandlerSendingToExternalStorageAndItReturnsAnError(t *testing.T)
defer ts.Close() defer ts.Close()
response := testUploadArtifactsFromTestZip(t, ts) response := testUploadArtifactsFromTestZip(t, ts)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.RequireResponseCode(t, response, http.StatusInternalServerError)
assert.Equal(t, 1, putCalledTimes, "upload should be called only once") assert.Equal(t, 1, putCalledTimes, "upload should be called only once")
} }
...@@ -272,7 +272,7 @@ func TestUploadHandlerSendingToExternalStorageAndSupportRequestTimeout(t *testin ...@@ -272,7 +272,7 @@ func TestUploadHandlerSendingToExternalStorageAndSupportRequestTimeout(t *testin
defer ts.Close() defer ts.Close()
response := testUploadArtifactsFromTestZip(t, ts) response := testUploadArtifactsFromTestZip(t, ts)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.RequireResponseCode(t, response, http.StatusInternalServerError)
assert.Equal(t, 1, putCalledTimes, "upload should be called only once") assert.Equal(t, 1, putCalledTimes, "upload should be called only once")
} }
...@@ -308,7 +308,7 @@ func TestUploadHandlerMultipartUploadSizeLimit(t *testing.T) { ...@@ -308,7 +308,7 @@ func TestUploadHandlerMultipartUploadSizeLimit(t *testing.T) {
contentBuffer, contentType := createTestMultipartForm(t, make([]byte, uploadSize)) contentBuffer, contentType := createTestMultipartForm(t, make([]byte, uploadSize))
response := testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer) response := testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer)
testhelper.AssertResponseCode(t, response, http.StatusRequestEntityTooLarge) testhelper.RequireResponseCode(t, response, http.StatusRequestEntityTooLarge)
// Poll because AbortMultipartUpload is async // Poll because AbortMultipartUpload is async
for i := 0; os.IsMultipartUpload(test.ObjectPath) && i < 100; i++ { for i := 0; os.IsMultipartUpload(test.ObjectPath) && i < 100; i++ {
......
...@@ -184,8 +184,8 @@ func TestUploadHandlerAddingMetadata(t *testing.T) { ...@@ -184,8 +184,8 @@ func TestUploadHandlerAddingMetadata(t *testing.T) {
require.NoError(t, s.writer.Close()) require.NoError(t, s.writer.Close())
response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.RequireResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent) testhelper.RequireResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent)
} }
func TestUploadHandlerForUnsupportedArchive(t *testing.T) { func TestUploadHandlerForUnsupportedArchive(t *testing.T) {
...@@ -194,8 +194,8 @@ func TestUploadHandlerForUnsupportedArchive(t *testing.T) { ...@@ -194,8 +194,8 @@ func TestUploadHandlerForUnsupportedArchive(t *testing.T) {
require.NoError(t, s.writer.Close()) require.NoError(t, s.writer.Close())
response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.RequireResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderMissing) testhelper.RequireResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderMissing)
} }
func TestUploadHandlerForMultipleFiles(t *testing.T) { func TestUploadHandlerForMultipleFiles(t *testing.T) {
...@@ -208,7 +208,7 @@ func TestUploadHandlerForMultipleFiles(t *testing.T) { ...@@ -208,7 +208,7 @@ func TestUploadHandlerForMultipleFiles(t *testing.T) {
require.NoError(t, s.writer.Close()) require.NoError(t, s.writer.Close())
response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.RequireResponseCode(t, response, http.StatusInternalServerError)
} }
func TestUploadFormProcessing(t *testing.T) { func TestUploadFormProcessing(t *testing.T) {
...@@ -217,7 +217,7 @@ func TestUploadFormProcessing(t *testing.T) { ...@@ -217,7 +217,7 @@ func TestUploadFormProcessing(t *testing.T) {
require.NoError(t, s.writer.Close()) require.NoError(t, s.writer.Close())
response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.RequireResponseCode(t, response, http.StatusInternalServerError)
} }
func TestLsifFileProcessing(t *testing.T) { func TestLsifFileProcessing(t *testing.T) {
...@@ -236,8 +236,8 @@ func TestLsifFileProcessing(t *testing.T) { ...@@ -236,8 +236,8 @@ func TestLsifFileProcessing(t *testing.T) {
require.NoError(t, s.writer.Close()) require.NoError(t, s.writer.Close())
response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.RequireResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent) testhelper.RequireResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent)
} }
func TestInvalidLsifFileProcessing(t *testing.T) { func TestInvalidLsifFileProcessing(t *testing.T) {
...@@ -256,5 +256,5 @@ func TestInvalidLsifFileProcessing(t *testing.T) { ...@@ -256,5 +256,5 @@ func TestInvalidLsifFileProcessing(t *testing.T) {
require.NoError(t, s.writer.Close()) require.NoError(t, s.writer.Close())
response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.RequireResponseCode(t, response, http.StatusInternalServerError)
} }
...@@ -50,16 +50,16 @@ func TestDownloadingFromValidArchive(t *testing.T) { ...@@ -50,16 +50,16 @@ func TestDownloadingFromValidArchive(t *testing.T) {
response := testEntryServer(t, tempFile.Name(), "test.txt") response := testEntryServer(t, tempFile.Name(), "test.txt")
testhelper.AssertResponseCode(t, response, 200) testhelper.RequireResponseCode(t, response, 200)
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Type", "Content-Type",
"text/plain; charset=utf-8") "text/plain; charset=utf-8")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Disposition", "Content-Disposition",
"attachment; filename=\"test.txt\"") "attachment; filename=\"test.txt\"")
testhelper.AssertResponseBody(t, response, "testtest") testhelper.RequireResponseBody(t, response, "testtest")
} }
func TestDownloadingFromValidHTTPArchive(t *testing.T) { func TestDownloadingFromValidHTTPArchive(t *testing.T) {
...@@ -84,16 +84,16 @@ func TestDownloadingFromValidHTTPArchive(t *testing.T) { ...@@ -84,16 +84,16 @@ func TestDownloadingFromValidHTTPArchive(t *testing.T) {
response := testEntryServer(t, fileServer.URL+"/archive.zip", "test.txt") response := testEntryServer(t, fileServer.URL+"/archive.zip", "test.txt")
testhelper.AssertResponseCode(t, response, 200) testhelper.RequireResponseCode(t, response, 200)
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Type", "Content-Type",
"text/plain; charset=utf-8") "text/plain; charset=utf-8")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Disposition", "Content-Disposition",
"attachment; filename=\"test.txt\"") "attachment; filename=\"test.txt\"")
testhelper.AssertResponseBody(t, response, "testtest") testhelper.RequireResponseBody(t, response, "testtest")
} }
func TestDownloadingNonExistingFile(t *testing.T) { func TestDownloadingNonExistingFile(t *testing.T) {
...@@ -107,17 +107,17 @@ func TestDownloadingNonExistingFile(t *testing.T) { ...@@ -107,17 +107,17 @@ func TestDownloadingNonExistingFile(t *testing.T) {
archive.Close() archive.Close()
response := testEntryServer(t, tempFile.Name(), "test") response := testEntryServer(t, tempFile.Name(), "test")
testhelper.AssertResponseCode(t, response, 404) testhelper.RequireResponseCode(t, response, 404)
} }
func TestDownloadingFromInvalidArchive(t *testing.T) { func TestDownloadingFromInvalidArchive(t *testing.T) {
response := testEntryServer(t, "path/to/non/existing/file", "test") response := testEntryServer(t, "path/to/non/existing/file", "test")
testhelper.AssertResponseCode(t, response, 404) testhelper.RequireResponseCode(t, response, 404)
} }
func TestIncompleteApiResponse(t *testing.T) { func TestIncompleteApiResponse(t *testing.T) {
response := testEntryServer(t, "", "") response := testEntryServer(t, "", "")
testhelper.AssertResponseCode(t, response, 500) testhelper.RequireResponseCode(t, response, 500)
} }
func TestDownloadingFromNonExistingHTTPArchive(t *testing.T) { func TestDownloadingFromNonExistingHTTPArchive(t *testing.T) {
...@@ -130,5 +130,5 @@ func TestDownloadingFromNonExistingHTTPArchive(t *testing.T) { ...@@ -130,5 +130,5 @@ func TestDownloadingFromNonExistingHTTPArchive(t *testing.T) {
response := testEntryServer(t, fileServer.URL+"/not-existing-archive-file.zip", "test.txt") response := testEntryServer(t, fileServer.URL+"/not-existing-archive-file.zip", "test.txt")
testhelper.AssertResponseCode(t, response, 404) testhelper.RequireResponseCode(t, response, 404)
} }
...@@ -61,7 +61,7 @@ var ( ...@@ -61,7 +61,7 @@ var (
fakeOther = fake(other, []byte("foo"), nil) fakeOther = fake(other, []byte("foo"), nil)
) )
func assertEqual(t *testing.T, expected, actual *fakeConn, msg string, args ...interface{}) { func requireEqualConn(t *testing.T, expected, actual *fakeConn, msg string, args ...interface{}) {
if expected.mt != actual.mt { if expected.mt != actual.mt {
t.Logf("messageType expected to be %v but was %v", expected.mt, actual.mt) t.Logf("messageType expected to be %v but was %v", expected.mt, actual.mt)
t.Fatalf(msg, args...) t.Fatalf(msg, args...)
...@@ -111,7 +111,7 @@ func TestReadMessage(t *testing.T) { ...@@ -111,7 +111,7 @@ func TestReadMessage(t *testing.T) {
conn := Wrap(tc.input, subprotocol) conn := Wrap(tc.input, subprotocol)
mt, data, err := conn.ReadMessage() mt, data, err := conn.ReadMessage()
actual := fake(mt, data, err) actual := fake(mt, data, err)
assertEqual(t, tc.expected, actual, "%s test case %v", subprotocol, i) requireEqualConn(t, tc.expected, actual, "%s test case %v", subprotocol, i)
} }
} }
} }
...@@ -149,7 +149,7 @@ func TestWriteMessage(t *testing.T) { ...@@ -149,7 +149,7 @@ func TestWriteMessage(t *testing.T) {
actual := fake(0, nil, tc.input.err) actual := fake(0, nil, tc.input.err)
conn := Wrap(actual, subprotocol) conn := Wrap(actual, subprotocol)
actual.err = conn.WriteMessage(tc.input.mt, tc.input.data) actual.err = conn.WriteMessage(tc.input.mt, tc.input.data)
assertEqual(t, tc.expected, actual, "%s test case %v", subprotocol, i) requireEqualConn(t, tc.expected, actual, "%s test case %v", subprotocol, i)
} }
} }
} }
...@@ -76,10 +76,10 @@ func TestSetArchiveHeaders(t *testing.T) { ...@@ -76,10 +76,10 @@ func TestSetArchiveHeaders(t *testing.T) {
setArchiveHeaders(w, testCase.in, "filename") setArchiveHeaders(w, testCase.in, "filename")
testhelper.AssertResponseWriterHeader(t, w, "Content-Type", testCase.out) testhelper.RequireResponseWriterHeader(t, w, "Content-Type", testCase.out)
testhelper.AssertResponseWriterHeader(t, w, "Content-Length") testhelper.RequireResponseWriterHeader(t, w, "Content-Length")
testhelper.AssertResponseWriterHeader(t, w, "Content-Disposition", `attachment; filename="filename"`) testhelper.RequireResponseWriterHeader(t, w, "Content-Disposition", `attachment; filename="filename"`)
testhelper.AssertResponseWriterHeader(t, w, "Cache-Control", "public, max-age=3600") testhelper.RequireResponseWriterHeader(t, w, "Cache-Control", "public, max-age=3600")
testhelper.AssertAbsentResponseWriterHeader(t, w, "Set-Cookie") testhelper.RequireAbsentResponseWriterHeader(t, w, "Set-Cookie")
} }
} }
...@@ -13,5 +13,5 @@ func TestSetBlobHeaders(t *testing.T) { ...@@ -13,5 +13,5 @@ func TestSetBlobHeaders(t *testing.T) {
setBlobHeaders(w) setBlobHeaders(w)
testhelper.AssertAbsentResponseWriterHeader(t, w, "Set-Cookie") testhelper.RequireAbsentResponseWriterHeader(t, w, "Set-Cookie")
} }
...@@ -84,16 +84,16 @@ func testEntryServer(t *testing.T, requestURL string, httpHeaders http.Header, a ...@@ -84,16 +84,16 @@ func testEntryServer(t *testing.T, requestURL string, httpHeaders http.Header, a
func TestDownloadingUsingSendURL(t *testing.T) { func TestDownloadingUsingSendURL(t *testing.T) {
response := testEntryServer(t, "/get/request", nil, false) response := testEntryServer(t, "/get/request", nil, false)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.RequireResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Type", "Content-Type",
"text/plain; charset=utf-8") "text/plain; charset=utf-8")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Disposition", "Content-Disposition",
"attachment; filename=\"archive.txt\"") "attachment; filename=\"archive.txt\"")
testhelper.AssertResponseBody(t, response, testData) testhelper.RequireResponseBody(t, response, testData)
} }
func TestDownloadingAChunkOfDataWithSendURL(t *testing.T) { func TestDownloadingAChunkOfDataWithSendURL(t *testing.T) {
...@@ -104,19 +104,19 @@ func TestDownloadingAChunkOfDataWithSendURL(t *testing.T) { ...@@ -104,19 +104,19 @@ func TestDownloadingAChunkOfDataWithSendURL(t *testing.T) {
} }
response := testEntryServer(t, "/get/request", httpHeaders, false) response := testEntryServer(t, "/get/request", httpHeaders, false)
testhelper.AssertResponseCode(t, response, http.StatusPartialContent) testhelper.RequireResponseCode(t, response, http.StatusPartialContent)
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Type", "Content-Type",
"text/plain; charset=utf-8") "text/plain; charset=utf-8")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Disposition", "Content-Disposition",
"attachment; filename=\"archive.txt\"") "attachment; filename=\"archive.txt\"")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Range", "Content-Range",
"bytes 1-2/30") "bytes 1-2/30")
testhelper.AssertResponseBody(t, response, "23") testhelper.RequireResponseBody(t, response, "23")
} }
func TestAccessingAlreadyDownloadedFileWithSendURL(t *testing.T) { func TestAccessingAlreadyDownloadedFileWithSendURL(t *testing.T) {
...@@ -125,22 +125,22 @@ func TestAccessingAlreadyDownloadedFileWithSendURL(t *testing.T) { ...@@ -125,22 +125,22 @@ func TestAccessingAlreadyDownloadedFileWithSendURL(t *testing.T) {
} }
response := testEntryServer(t, "/get/request", httpHeaders, false) response := testEntryServer(t, "/get/request", httpHeaders, false)
testhelper.AssertResponseCode(t, response, http.StatusNotModified) testhelper.RequireResponseCode(t, response, http.StatusNotModified)
} }
func TestAccessingRedirectWithSendURL(t *testing.T) { func TestAccessingRedirectWithSendURL(t *testing.T) {
response := testEntryServer(t, "/get/redirect", nil, false) response := testEntryServer(t, "/get/redirect", nil, false)
testhelper.AssertResponseCode(t, response, http.StatusTemporaryRedirect) testhelper.RequireResponseCode(t, response, http.StatusTemporaryRedirect)
} }
func TestAccessingAllowedRedirectWithSendURL(t *testing.T) { func TestAccessingAllowedRedirectWithSendURL(t *testing.T) {
response := testEntryServer(t, "/get/redirect", nil, true) response := testEntryServer(t, "/get/redirect", nil, true)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.RequireResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Type", "Content-Type",
"text/plain; charset=utf-8") "text/plain; charset=utf-8")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Disposition", "Content-Disposition",
"attachment; filename=\"archive.txt\"") "attachment; filename=\"archive.txt\"")
} }
...@@ -153,45 +153,45 @@ func TestAccessingAllowedRedirectWithChunkOfDataWithSendURL(t *testing.T) { ...@@ -153,45 +153,45 @@ func TestAccessingAllowedRedirectWithChunkOfDataWithSendURL(t *testing.T) {
} }
response := testEntryServer(t, "/get/redirect", httpHeaders, true) response := testEntryServer(t, "/get/redirect", httpHeaders, true)
testhelper.AssertResponseCode(t, response, http.StatusPartialContent) testhelper.RequireResponseCode(t, response, http.StatusPartialContent)
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Type", "Content-Type",
"text/plain; charset=utf-8") "text/plain; charset=utf-8")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Disposition", "Content-Disposition",
"attachment; filename=\"archive.txt\"") "attachment; filename=\"archive.txt\"")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Content-Range", "Content-Range",
"bytes 1-2/30") "bytes 1-2/30")
testhelper.AssertResponseBody(t, response, "23") testhelper.RequireResponseBody(t, response, "23")
} }
func TestOriginalCacheHeadersPreservedWithSendURL(t *testing.T) { func TestOriginalCacheHeadersPreservedWithSendURL(t *testing.T) {
response := testEntryServer(t, "/get/redirect", nil, true) response := testEntryServer(t, "/get/redirect", nil, true)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.RequireResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Cache-Control", "Cache-Control",
"no-cache") "no-cache")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Expires", "Expires",
"") "")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Date", "Date",
"Wed, 21 Oct 2015 05:28:00 GMT") "Wed, 21 Oct 2015 05:28:00 GMT")
testhelper.AssertResponseWriterHeader(t, response, testhelper.RequireResponseWriterHeader(t, response,
"Pragma", "Pragma",
"no-cache") "no-cache")
} }
func TestDownloadingNonExistingFileUsingSendURL(t *testing.T) { func TestDownloadingNonExistingFileUsingSendURL(t *testing.T) {
response := testEntryServer(t, "/invalid/path", nil, false) response := testEntryServer(t, "/invalid/path", nil, false)
testhelper.AssertResponseCode(t, response, http.StatusNotFound) testhelper.RequireResponseCode(t, response, http.StatusNotFound)
} }
func TestDownloadingNonExistingRemoteFileWithSendURL(t *testing.T) { func TestDownloadingNonExistingRemoteFileWithSendURL(t *testing.T) {
response := testEntryServer(t, "/get/file-not-existing", nil, false) response := testEntryServer(t, "/get/file-not-existing", nil, false)
testhelper.AssertResponseCode(t, response, http.StatusNotFound) testhelper.RequireResponseCode(t, response, http.StatusNotFound)
} }
...@@ -52,6 +52,6 @@ func TestIfDeployPageExist(t *testing.T) { ...@@ -52,6 +52,6 @@ func TestIfDeployPageExist(t *testing.T) {
} }
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 200) testhelper.RequireResponseCode(t, w, 200)
testhelper.AssertResponseBody(t, w, deployPage) testhelper.RequireResponseBody(t, w, deployPage)
} }
...@@ -36,9 +36,9 @@ func TestIfErrorPageIsPresented(t *testing.T) { ...@@ -36,9 +36,9 @@ func TestIfErrorPageIsPresented(t *testing.T) {
st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil) st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil)
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
testhelper.AssertResponseBody(t, w, errorPage) testhelper.RequireResponseBody(t, w, errorPage)
testhelper.AssertResponseHeader(t, w, "Content-Type", "text/html; charset=utf-8") testhelper.RequireResponseHeader(t, w, "Content-Type", "text/html; charset=utf-8")
} }
func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) { func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
...@@ -58,8 +58,8 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) { ...@@ -58,8 +58,8 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil) st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil)
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
testhelper.AssertResponseBody(t, w, errorResponse) testhelper.RequireResponseBody(t, w, errorResponse)
} }
func TestIfErrorPageIsIgnoredInDevelopment(t *testing.T) { func TestIfErrorPageIsIgnoredInDevelopment(t *testing.T) {
...@@ -81,8 +81,8 @@ func TestIfErrorPageIsIgnoredInDevelopment(t *testing.T) { ...@@ -81,8 +81,8 @@ func TestIfErrorPageIsIgnoredInDevelopment(t *testing.T) {
st := &Static{dir} st := &Static{dir}
st.ErrorPagesUnless(true, ErrorFormatHTML, h).ServeHTTP(w, nil) st.ErrorPagesUnless(true, ErrorFormatHTML, h).ServeHTTP(w, nil)
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 500) testhelper.RequireResponseCode(t, w, 500)
testhelper.AssertResponseBody(t, w, serverError) testhelper.RequireResponseBody(t, w, serverError)
} }
func TestIfErrorPageIsIgnoredIfCustomError(t *testing.T) { func TestIfErrorPageIsIgnoredIfCustomError(t *testing.T) {
...@@ -105,8 +105,8 @@ func TestIfErrorPageIsIgnoredIfCustomError(t *testing.T) { ...@@ -105,8 +105,8 @@ func TestIfErrorPageIsIgnoredIfCustomError(t *testing.T) {
st := &Static{dir} st := &Static{dir}
st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil) st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil)
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 500) testhelper.RequireResponseCode(t, w, 500)
testhelper.AssertResponseBody(t, w, serverError) testhelper.RequireResponseBody(t, w, serverError)
} }
func TestErrorPageInterceptedByContentType(t *testing.T) { func TestErrorPageInterceptedByContentType(t *testing.T) {
...@@ -140,12 +140,12 @@ func TestErrorPageInterceptedByContentType(t *testing.T) { ...@@ -140,12 +140,12 @@ func TestErrorPageInterceptedByContentType(t *testing.T) {
st := &Static{dir} st := &Static{dir}
st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil) st.ErrorPagesUnless(false, ErrorFormatHTML, h).ServeHTTP(w, nil)
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 500) testhelper.RequireResponseCode(t, w, 500)
if tc.intercepted { if tc.intercepted {
testhelper.AssertResponseBody(t, w, errorPage) testhelper.RequireResponseBody(t, w, errorPage)
} else { } else {
testhelper.AssertResponseBody(t, w, serverError) testhelper.RequireResponseBody(t, w, serverError)
} }
} }
} }
...@@ -165,9 +165,9 @@ func TestIfErrorPageIsPresentedJSON(t *testing.T) { ...@@ -165,9 +165,9 @@ func TestIfErrorPageIsPresentedJSON(t *testing.T) {
st.ErrorPagesUnless(false, ErrorFormatJSON, h).ServeHTTP(w, nil) st.ErrorPagesUnless(false, ErrorFormatJSON, h).ServeHTTP(w, nil)
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
testhelper.AssertResponseBody(t, w, errorPage) testhelper.RequireResponseBody(t, w, errorPage)
testhelper.AssertResponseHeader(t, w, "Content-Type", "application/json; charset=utf-8") testhelper.RequireResponseHeader(t, w, "Content-Type", "application/json; charset=utf-8")
} }
func TestIfErrorPageIsPresentedText(t *testing.T) { func TestIfErrorPageIsPresentedText(t *testing.T) {
...@@ -185,7 +185,7 @@ func TestIfErrorPageIsPresentedText(t *testing.T) { ...@@ -185,7 +185,7 @@ func TestIfErrorPageIsPresentedText(t *testing.T) {
st.ErrorPagesUnless(false, ErrorFormatText, h).ServeHTTP(w, nil) st.ErrorPagesUnless(false, ErrorFormatText, h).ServeHTTP(w, nil)
w.Flush() w.Flush()
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
testhelper.AssertResponseBody(t, w, errorPage) testhelper.RequireResponseBody(t, w, errorPage)
testhelper.AssertResponseHeader(t, w, "Content-Type", "text/plain; charset=utf-8") testhelper.RequireResponseHeader(t, w, "Content-Type", "text/plain; charset=utf-8")
} }
...@@ -20,7 +20,7 @@ func TestServingNonExistingFile(t *testing.T) { ...@@ -20,7 +20,7 @@ func TestServingNonExistingFile(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
st := &Static{dir} st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest) st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
} }
func TestServingDirectory(t *testing.T) { func TestServingDirectory(t *testing.T) {
...@@ -34,7 +34,7 @@ func TestServingDirectory(t *testing.T) { ...@@ -34,7 +34,7 @@ func TestServingDirectory(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
st := &Static{dir} st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest) st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
} }
func TestServingMalformedUri(t *testing.T) { func TestServingMalformedUri(t *testing.T) {
...@@ -44,7 +44,7 @@ func TestServingMalformedUri(t *testing.T) { ...@@ -44,7 +44,7 @@ func TestServingMalformedUri(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
st := &Static{dir} st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest) st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
} }
func TestExecutingHandlerWhenNoFileFound(t *testing.T) { func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
...@@ -76,7 +76,7 @@ func TestServingTheActualFile(t *testing.T) { ...@@ -76,7 +76,7 @@ func TestServingTheActualFile(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
st := &Static{dir} st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest) st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 200) testhelper.RequireResponseCode(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())
} }
...@@ -108,15 +108,15 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) { ...@@ -108,15 +108,15 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
st := &Static{dir} st := &Static{dir}
st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest) st.ServeExisting("/", CacheDisabled, nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 200) testhelper.RequireResponseCode(t, w, 200)
if enableGzip { if enableGzip {
testhelper.AssertResponseWriterHeader(t, w, "Content-Encoding", "gzip") testhelper.RequireResponseWriterHeader(t, w, "Content-Encoding", "gzip")
if !bytes.Equal(w.Body.Bytes(), fileGzipContent.Bytes()) { if !bytes.Equal(w.Body.Bytes(), fileGzipContent.Bytes()) {
t.Error("We should serve the pregzipped file") t.Error("We should serve the pregzipped file")
} }
} else { } else {
testhelper.AssertResponseCode(t, w, 200) testhelper.RequireResponseCode(t, w, 200)
testhelper.AssertResponseWriterHeader(t, w, "Content-Encoding") testhelper.RequireResponseWriterHeader(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())
} }
......
...@@ -33,9 +33,10 @@ func ConfigureSecret() { ...@@ -33,9 +33,10 @@ func ConfigureSecret() {
var extractPatchSeriesMatcher = regexp.MustCompile(`^From (\w+)`) var extractPatchSeriesMatcher = regexp.MustCompile(`^From (\w+)`)
// AssertPatchSeries takes a `git format-patch` blob, extracts the From xxxxx // RequirePatchSeries takes a `git format-patch` blob, extracts the From xxxxx
// lines and compares the SHAs to expected list. // lines and compares the SHAs to expected list.
func AssertPatchSeries(t *testing.T, blob []byte, expected ...string) { func RequirePatchSeries(t *testing.T, blob []byte, expected ...string) {
t.Helper()
var actual []string var actual []string
footer := make([]string, 3) footer := make([]string, 3)
...@@ -60,31 +61,36 @@ func AssertPatchSeries(t *testing.T, blob []byte, expected ...string) { ...@@ -60,31 +61,36 @@ func AssertPatchSeries(t *testing.T, blob []byte, expected ...string) {
} }
} }
func AssertResponseCode(t *testing.T, response *httptest.ResponseRecorder, expectedCode int) { func RequireResponseCode(t *testing.T, response *httptest.ResponseRecorder, expectedCode int) {
t.Helper()
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 RequireResponseBody(t *testing.T, response *httptest.ResponseRecorder, expectedBody string) {
t.Helper()
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 AssertResponseBodyRegexp(t *testing.T, response *httptest.ResponseRecorder, expectedBody *regexp.Regexp) { func RequireResponseBodyRegexp(t *testing.T, response *httptest.ResponseRecorder, expectedBody *regexp.Regexp) {
t.Helper()
if !expectedBody.MatchString(response.Body.String()) { if !expectedBody.MatchString(response.Body.String()) {
t.Fatalf("for HTTP request expected to receive body matching %q, got %q instead", expectedBody.String(), response.Body.String()) t.Fatalf("for HTTP request expected to receive body matching %q, got %q instead", expectedBody.String(), response.Body.String())
} }
} }
func AssertResponseWriterHeader(t *testing.T, w http.ResponseWriter, header string, expected ...string) { func RequireResponseWriterHeader(t *testing.T, w http.ResponseWriter, header string, expected ...string) {
t.Helper()
actual := w.Header()[http.CanonicalHeaderKey(header)] actual := w.Header()[http.CanonicalHeaderKey(header)]
assertHeaderExists(t, header, actual, expected) requireHeaderExists(t, header, actual, expected)
} }
func AssertAbsentResponseWriterHeader(t *testing.T, w http.ResponseWriter, header string) { func RequireAbsentResponseWriterHeader(t *testing.T, w http.ResponseWriter, header string) {
t.Helper()
actual := w.Header()[http.CanonicalHeaderKey(header)] actual := w.Header()[http.CanonicalHeaderKey(header)]
if len(actual) != 0 { if len(actual) != 0 {
...@@ -92,7 +98,8 @@ func AssertAbsentResponseWriterHeader(t *testing.T, w http.ResponseWriter, heade ...@@ -92,7 +98,8 @@ func AssertAbsentResponseWriterHeader(t *testing.T, w http.ResponseWriter, heade
} }
} }
func AssertResponseHeader(t *testing.T, w interface{}, header string, expected ...string) { func RequireResponseHeader(t *testing.T, w interface{}, header string, expected ...string) {
t.Helper()
var actual []string var actual []string
header = http.CanonicalHeaderKey(header) header = http.CanonicalHeaderKey(header)
...@@ -104,13 +111,14 @@ func AssertResponseHeader(t *testing.T, w interface{}, header string, expected . ...@@ -104,13 +111,14 @@ func AssertResponseHeader(t *testing.T, w interface{}, header string, expected .
} else if resp, ok := w.(*httptest.ResponseRecorder); ok { } else if resp, ok := w.(*httptest.ResponseRecorder); ok {
actual = resp.Header()[header] actual = resp.Header()[header]
} else { } else {
t.Fatalf("invalid type of w passed AssertResponseHeader") t.Fatalf("invalid type of w passed RequireResponseHeader")
} }
assertHeaderExists(t, header, actual, expected) requireHeaderExists(t, header, actual, expected)
} }
func assertHeaderExists(t *testing.T, header string, actual, expected []string) { func requireHeaderExists(t *testing.T, header string, actual, expected []string) {
t.Helper()
if len(expected) != len(actual) { if len(expected) != len(actual) {
t.Fatalf("for HTTP request expected to receive the header %q with %+v, got %+v", header, expected, actual) t.Fatalf("for HTTP request expected to receive the header %q with %+v, got %+v", header, expected, actual)
} }
......
...@@ -62,7 +62,7 @@ func TestUploadTempPathRequirement(t *testing.T) { ...@@ -62,7 +62,7 @@ func TestUploadTempPathRequirement(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
HandleFileUploads(response, request, nilHandler, apiResponse, &testFormProcessor{}, opts) HandleFileUploads(response, request, nilHandler, apiResponse, &testFormProcessor{}, opts)
testhelper.AssertResponseCode(t, response, 500) testhelper.RequireResponseCode(t, response, 500)
} }
func TestUploadHandlerForwardingRawData(t *testing.T) { func TestUploadHandlerForwardingRawData(t *testing.T) {
...@@ -103,7 +103,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) { ...@@ -103,7 +103,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
HandleFileUploads(response, httpRequest, handler, apiResponse, nil, opts) HandleFileUploads(response, httpRequest, handler, apiResponse, nil, opts)
testhelper.AssertResponseCode(t, response, 202) testhelper.RequireResponseCode(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")
} }
...@@ -209,7 +209,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) { ...@@ -209,7 +209,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts) HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts)
testhelper.AssertResponseCode(t, response, 202) testhelper.RequireResponseCode(t, response, 202)
cancel() // this will trigger an async cleanup cancel() // this will trigger an async cleanup
waitUntilDeleted(t, filePath) waitUntilDeleted(t, filePath)
...@@ -286,7 +286,7 @@ func TestUploadHandlerDetectingInjectedMultiPartData(t *testing.T) { ...@@ -286,7 +286,7 @@ func TestUploadHandlerDetectingInjectedMultiPartData(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts) HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts)
testhelper.AssertResponseCode(t, response, test.response) testhelper.RequireResponseCode(t, response, test.response)
cancel() // this will trigger an async cleanup cancel() // this will trigger an async cleanup
waitUntilDeleted(t, filePath) waitUntilDeleted(t, filePath)
...@@ -321,7 +321,7 @@ func TestUploadProcessingField(t *testing.T) { ...@@ -321,7 +321,7 @@ func TestUploadProcessingField(t *testing.T) {
HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &testFormProcessor{}, opts) HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &testFormProcessor{}, opts)
testhelper.AssertResponseCode(t, response, 500) testhelper.RequireResponseCode(t, response, 500)
} }
func TestUploadProcessingFile(t *testing.T) { func TestUploadProcessingFile(t *testing.T) {
...@@ -382,7 +382,7 @@ func TestUploadProcessingFile(t *testing.T) { ...@@ -382,7 +382,7 @@ func TestUploadProcessingFile(t *testing.T) {
HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &testFormProcessor{}, opts) HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &testFormProcessor{}, opts)
testhelper.AssertResponseCode(t, response, 200) testhelper.RequireResponseCode(t, response, 200)
}) })
} }
...@@ -430,7 +430,7 @@ func TestInvalidFileNames(t *testing.T) { ...@@ -430,7 +430,7 @@ func TestInvalidFileNames(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &SavedFileTracker{Request: httpRequest}, opts) HandleFileUploads(response, httpRequest, nilHandler, apiResponse, &SavedFileTracker{Request: httpRequest}, opts)
testhelper.AssertResponseCode(t, response, testCase.code) testhelper.RequireResponseCode(t, response, testCase.code)
} }
} }
...@@ -486,7 +486,7 @@ func TestUploadHandlerRemovingExif(t *testing.T) { ...@@ -486,7 +486,7 @@ func TestUploadHandlerRemovingExif(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts) HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts)
testhelper.AssertResponseCode(t, response, 200) testhelper.RequireResponseCode(t, response, 200)
} }
func TestUploadHandlerRemovingInvalidExif(t *testing.T) { func TestUploadHandlerRemovingInvalidExif(t *testing.T) {
...@@ -528,7 +528,7 @@ func TestUploadHandlerRemovingInvalidExif(t *testing.T) { ...@@ -528,7 +528,7 @@ func TestUploadHandlerRemovingInvalidExif(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts) HandleFileUploads(response, httpRequest, handler, apiResponse, &testFormProcessor{}, opts)
testhelper.AssertResponseCode(t, response, 422) testhelper.RequireResponseCode(t, response, 422)
} }
func newProxy(url string) *proxy.Proxy { func newProxy(url string) *proxy.Proxy {
......
...@@ -36,5 +36,5 @@ func TestDevelopmentModeDisabled(t *testing.T) { ...@@ -36,5 +36,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")
} }
testhelper.AssertResponseCode(t, w, 404) testhelper.RequireResponseCode(t, w, 404)
} }
...@@ -38,7 +38,7 @@ func TestGzipEncoding(t *testing.T) { ...@@ -38,7 +38,7 @@ func TestGzipEncoding(t *testing.T) {
} }
})).ServeHTTP(resp, req) })).ServeHTTP(resp, req)
testhelper.AssertResponseCode(t, resp, 200) testhelper.RequireResponseCode(t, resp, 200)
} }
func TestNoEncoding(t *testing.T) { func TestNoEncoding(t *testing.T) {
...@@ -62,7 +62,7 @@ func TestNoEncoding(t *testing.T) { ...@@ -62,7 +62,7 @@ func TestNoEncoding(t *testing.T) {
} }
})).ServeHTTP(resp, req) })).ServeHTTP(resp, req)
testhelper.AssertResponseCode(t, resp, 200) testhelper.RequireResponseCode(t, resp, 200)
} }
func TestInvalidEncoding(t *testing.T) { func TestInvalidEncoding(t *testing.T) {
...@@ -78,5 +78,5 @@ func TestInvalidEncoding(t *testing.T) { ...@@ -78,5 +78,5 @@ func TestInvalidEncoding(t *testing.T) {
t.Fatal("it shouldn't be executed") t.Fatal("it shouldn't be executed")
})).ServeHTTP(resp, req) })).ServeHTTP(resp, req)
testhelper.AssertResponseCode(t, resp, 500) testhelper.RequireResponseCode(t, resp, 500)
} }
...@@ -503,7 +503,7 @@ func TestAPIFalsePositivesAreProxied(t *testing.T) { ...@@ -503,7 +503,7 @@ func TestAPIFalsePositivesAreProxied(t *testing.T) {
assert.NoError(t, err, "%s %q: reading body", tc.method, tc.path) assert.NoError(t, err, "%s %q: reading body", tc.method, tc.path)
assert.Equal(t, 200, resp.StatusCode, "%s %q: status code", tc.method, tc.path) assert.Equal(t, 200, resp.StatusCode, "%s %q: status code", tc.method, tc.path)
testhelper.AssertResponseHeader(t, resp, "Content-Type", "text/html") testhelper.RequireResponseHeader(t, resp, "Content-Type", "text/html")
assert.Equal(t, string(goodResponse), string(respBody), "%s %q: response body", tc.method, tc.path) assert.Equal(t, string(goodResponse), string(respBody), "%s %q: response body", tc.method, tc.path)
} }
} }
......
...@@ -66,8 +66,8 @@ func TestProxyRequest(t *testing.T) { ...@@ -66,8 +66,8 @@ func TestProxyRequest(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
newProxy(ts.URL, nil).ServeHTTP(w, httpRequest) newProxy(ts.URL, nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 202) testhelper.RequireResponseCode(t, w, 202)
testhelper.AssertResponseBody(t, w, "RESPONSE") testhelper.RequireResponseBody(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")
...@@ -83,8 +83,8 @@ func TestProxyError(t *testing.T) { ...@@ -83,8 +83,8 @@ func TestProxyError(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
newProxy("http://localhost:655575/", nil).ServeHTTP(w, httpRequest) newProxy("http://localhost:655575/", nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 502) testhelper.RequireResponseCode(t, w, 502)
testhelper.AssertResponseBodyRegexp(t, w, regexp.MustCompile("dial tcp:.*invalid port.*")) testhelper.RequireResponseBodyRegexp(t, w, regexp.MustCompile("dial tcp:.*invalid port.*"))
} }
func TestProxyReadTimeout(t *testing.T) { func TestProxyReadTimeout(t *testing.T) {
...@@ -110,8 +110,8 @@ func TestProxyReadTimeout(t *testing.T) { ...@@ -110,8 +110,8 @@ func TestProxyReadTimeout(t *testing.T) {
p := newProxy(ts.URL, rt) p := newProxy(ts.URL, rt)
w := httptest.NewRecorder() w := httptest.NewRecorder()
p.ServeHTTP(w, httpRequest) p.ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 502) testhelper.RequireResponseCode(t, w, 502)
testhelper.AssertResponseBody(t, w, "GitLab is not responding") testhelper.RequireResponseBody(t, w, "GitLab is not responding")
} }
func TestProxyHandlerTimeout(t *testing.T) { func TestProxyHandlerTimeout(t *testing.T) {
...@@ -128,6 +128,6 @@ func TestProxyHandlerTimeout(t *testing.T) { ...@@ -128,6 +128,6 @@ func TestProxyHandlerTimeout(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
newProxy(ts.URL, nil).ServeHTTP(w, httpRequest) newProxy(ts.URL, nil).ServeHTTP(w, httpRequest)
testhelper.AssertResponseCode(t, w, 503) testhelper.RequireResponseCode(t, w, 503)
testhelper.AssertResponseBody(t, w, "Request took too long") testhelper.RequireResponseBody(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