diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2020-01-29 15:39:35 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2020-01-29 15:39:35 +0300 |
commit | fdf482dd8574695ac93c8240acc4fdf496efba6d (patch) | |
tree | 6864e2e1618e8cb210b96e741a7d6f17b5098871 /internal | |
parent | 7e8b22c4070baba5425760cf7285486a8c97b335 (diff) |
Add test cases to serverless serving covering typical responses
Diffstat (limited to 'internal')
-rw-r--r-- | internal/serving/serverless/serverless_test.go | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/internal/serving/serverless/serverless_test.go b/internal/serving/serverless/serverless_test.go index 2d0b7df0..33754239 100644 --- a/internal/serving/serverless/serverless_test.go +++ b/internal/serving/serverless/serverless_test.go @@ -107,4 +107,76 @@ func TestServeFileHTTP(t *testing.T) { assert.Contains(t, string(body), "cluster error: x509: certificate") }) }) + + t.Run("when a cluster responds with an error", func(t *testing.T) { + withTestCluster(t, fixture.Certificate, fixture.Key, func(mux *http.ServeMux, server *url.URL, certs *Certs) { + serverless := New( + Function{ + Name: "my-func", + Namespace: "my-namespace-123", + BaseDomain: "knative.example.com", + }, + Cluster{ + Name: "knative.gitlab-example.com", + Address: server.Hostname(), + Port: server.Port(), + Certs: certs, + }, + ) + + writer := httptest.NewRecorder() + request := httptest.NewRequest("GET", "http://example.gitlab.com/", nil) + handler := serving.Handler{Writer: writer, Request: request} + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusServiceUnavailable) + w.Write([]byte("sorry, service unavailable")) + }) + + served := serverless.ServeFileHTTP(handler) + result := writer.Result() + body, err := ioutil.ReadAll(writer.Body) + require.NoError(t, err) + + assert.True(t, served) + assert.Equal(t, http.StatusServiceUnavailable, result.StatusCode) + assert.Contains(t, string(body), "sorry, service unavailable") + }) + }) + + t.Run("when a cluster responds correctly", func(t *testing.T) { + withTestCluster(t, fixture.Certificate, fixture.Key, func(mux *http.ServeMux, server *url.URL, certs *Certs) { + serverless := New( + Function{ + Name: "my-func", + Namespace: "my-namespace-123", + BaseDomain: "knative.example.com", + }, + Cluster{ + Name: "knative.gitlab-example.com", + Address: server.Hostname(), + Port: server.Port(), + Certs: certs, + }, + ) + + writer := httptest.NewRecorder() + request := httptest.NewRequest("GET", "http://example.gitlab.com/", nil) + handler := serving.Handler{Writer: writer, Request: request} + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK")) + }) + + served := serverless.ServeFileHTTP(handler) + result := writer.Result() + body, err := ioutil.ReadAll(writer.Body) + require.NoError(t, err) + + assert.True(t, served) + assert.Equal(t, http.StatusOK, result.StatusCode) + assert.Contains(t, string(body), "OK") + }) + }) } |