Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIXIT: refactor http testing functions to builder pattern. Fixes #426 #440

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packager/certcache/certcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (this *CertCacheSuite) DecodeCBOR(r io.Reader) map[string][]byte {
}

func (this *CertCacheSuite) TestServesCertificate() {
resp := pkgt.Get(this.T(), this.mux(), "/amppkg/cert/"+pkgt.CertName)
resp := pkgt.NewRequest(this.T(), this.mux(), "/amppkg/cert/"+pkgt.CertName).Do()
this.Assert().Equal(http.StatusOK, resp.StatusCode, "incorrect status: %#v", resp)
this.Assert().Equal("nosniff", resp.Header.Get("X-Content-Type-Options"))
cbor := this.DecodeCBOR(resp.Body)
Expand Down Expand Up @@ -218,7 +218,7 @@ func (this *CertCacheSuite) TestCertCacheIsNotHealthy() {
}

func (this *CertCacheSuite) TestServes404OnMissingCertificate() {
resp := pkgt.Get(this.T(), this.mux(), "/amppkg/cert/lalala")
resp := pkgt.NewRequest(this.T(), this.mux(), "/amppkg/cert/lalala").Do()
this.Assert().Equal(http.StatusNotFound, resp.StatusCode, "incorrect status: %#v", resp)
body, _ := ioutil.ReadAll(resp.Body)
// Small enough not to fit a cert or key:
Expand All @@ -227,7 +227,7 @@ func (this *CertCacheSuite) TestServes404OnMissingCertificate() {

func (this *CertCacheSuite) TestOCSP() {
// Verify it gets included in the cert-chain+cbor payload.
resp := pkgt.Get(this.T(), this.mux(), "/amppkg/cert/"+pkgt.CertName)
resp := pkgt.NewRequest(this.T(), this.mux(), "/amppkg/cert/"+pkgt.CertName).Do()
this.Assert().Equal(http.StatusOK, resp.StatusCode, "incorrect status: %#v", resp)
// 302400 is 3.5 days. max-age is slightly less because of the time between fake OCSP generation and cert-chain response.
this.Assert().Equal("public, max-age=302387", resp.Header.Get("Cache-Control"))
Expand Down Expand Up @@ -261,7 +261,7 @@ func (this *CertCacheSuite) TestOCSPExpiry() {
}))

// Verify HTTP response expires immediately:
resp := pkgt.Get(this.T(), this.mux(), "/amppkg/cert/"+pkgt.CertName)
resp := pkgt.NewRequest(this.T(), this.mux(), "/amppkg/cert/"+pkgt.CertName).Do()
this.Assert().Equal("public, max-age=0", resp.Header.Get("Cache-Control"))

// On update, verify network is called:
Expand Down
4 changes: 2 additions & 2 deletions packager/healthz/healthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (this fakeNotHealthyCertHandler) IsHealthy() error {
func TestHealthzOk(t *testing.T) {
handler, err := New(fakeHealthyCertHandler{})
require.NoError(t, err)
resp := pkgt.Get(t, mux.New(nil, nil, nil, handler, nil), "/healthz")
resp := pkgt.NewRequest(t, mux.New(nil, nil, nil, handler, nil), "/healthz").Do()
assert.Equal(t, http.StatusOK, resp.StatusCode, "ok", resp)
}

func TestHealthzFail(t *testing.T) {
handler, err := New(fakeNotHealthyCertHandler{})
require.NoError(t, err)
resp := pkgt.Get(t, mux.New(nil, nil, nil, handler, nil), "/healthz")
resp := pkgt.NewRequest(t, mux.New(nil, nil, nil, handler, nil), "/healthz").Do()
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode, "error", resp)
}
34 changes: 17 additions & 17 deletions packager/mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestServeHTTPSuccess(t *testing.T) {

// Run.
mux := New(mocks["cert"], mocks["signer"], mocks["validityMap"], mocks["healthz"], mocks["metrics"])
actualResp = pkgt.Get(t, mux, tt.testURL)
actualResp = pkgt.NewRequest(t, mux, tt.testURL).Do()
})
}
}
Expand All @@ -185,7 +185,7 @@ func expectError(t *testing.T, url string, expectErrorMessage string, expectErro
mux := New(mockedHandler, mockedHandler, mockedHandler, mockedHandler, mockedHandler)

// Run and extract error.
actualResp = pkgt.GetBHH(t, mux, url, "", body, http.Header{})
actualResp = pkgt.NewRequest(t, mux, url).SetBody(body).Do()
actualErrorMessageBuffer, _ := ioutil.ReadAll(actualResp.Body)
actualErrorMessage = fmt.Sprintf("%s", actualErrorMessageBuffer)

Expand All @@ -212,7 +212,7 @@ func TestServeHTTPexpect404s(t *testing.T) {
}

func TestServeHTTPexpect405(t *testing.T) {
body := strings.NewReader("Non empty body so GetBHH sends a POST request")
body := strings.NewReader("Non empty body so this sends a POST request")
expectError(t, expand("$HOST/healthz"), "405 method not allowed\n", http.StatusMethodNotAllowed, body)
}

Expand Down Expand Up @@ -251,11 +251,11 @@ func TestPrometheusMetricRequestsTotal(t *testing.T) {
`,
/* testFunc= */ func() {
mux := New(nopHandler, nopHandler, nopHandler, nopHandler, nopHandler)
pkgt.Get(t, mux, expand(`$HOST/priv/doc?fetch=$FETCH&sign=$SIGN`))
pkgt.Get(t, mux, expand(`$HOST/amppkg/cert/$CERT`))
pkgt.Get(t, mux, expand(`$HOST/amppkg/validity`))
pkgt.Get(t, mux, expand(`$HOST/healthz`))
pkgt.Get(t, mux, expand(`$HOST/healthz`))
pkgt.NewRequest(t, mux, expand(`$HOST/priv/doc?fetch=$FETCH&sign=$SIGN`)).Do()
pkgt.NewRequest(t, mux, expand(`$HOST/amppkg/cert/$CERT`)).Do()
pkgt.NewRequest(t, mux, expand(`$HOST/amppkg/validity`)).Do()
pkgt.NewRequest(t, mux, expand(`$HOST/healthz`)).Do()
pkgt.NewRequest(t, mux, expand(`$HOST/healthz`)).Do()
},
/* expectedMetrics = */ `
total_requests_by_code_and_url{code="200",handler="signer"} 1
Expand All @@ -272,7 +272,7 @@ func TestPrometheusMetricRequestsTotal(t *testing.T) {
`,
/* testFunc= */ func() {
mux := New(nopHandler, nopHandler, nopHandler, nopHandler, nopHandler)
pkgt.Get(t, mux, expand(`$HOST/healthzSOME_SUFFIX`))
pkgt.NewRequest(t, mux, expand(`$HOST/healthzSOME_SUFFIX`)).Do()
},
/* expectedMetrics = */ `
total_requests_by_code_and_url{code="404",handler="healthz"} 1
Expand All @@ -285,9 +285,9 @@ func TestPrometheusMetricRequestsTotal(t *testing.T) {
`,
/* testFunc= */ func() {
mux := New(nopHandler, nopHandler, nopHandler, nopHandler, nopHandler)
pkgt.Get(t, mux, expand(`$HOST/abc`))
pkgt.Get(t, mux, expand(`$HOST/def`))
pkgt.Get(t, mux, expand(`$HOST/ghi`))
pkgt.NewRequest(t, mux, expand(`$HOST/abc`)).Do()
pkgt.NewRequest(t, mux, expand(`$HOST/def`)).Do()
pkgt.NewRequest(t, mux, expand(`$HOST/ghi`)).Do()
},
/* expectedMetrics = */ `
total_requests_by_code_and_url{code="404",handler="handler_not_assigned"} 3
Expand All @@ -300,8 +300,8 @@ func TestPrometheusMetricRequestsTotal(t *testing.T) {
`,
/* testFunc= */ func() {
mux := New(nopHandler, nopHandler, nopHandler, nopHandler, nopHandler)
body := strings.NewReader("Non empty body so GetBHH sends a POST request")
pkgt.GetBHH(t, mux, expand("$HOST/healthz"), "", body, http.Header{})
body := strings.NewReader("Non empty body so this will be a POST request")
pkgt.NewRequest(t, mux, expand("$HOST/healthz")).SetBody(body).Do()
},
/* expectedMetrics = */ `
total_requests_by_code_and_url{code="405",handler="healthz"} 1
Expand All @@ -318,7 +318,7 @@ func TestPrometheusMetricRequestsTotal(t *testing.T) {
/* testFunc= */ func() {
signerMockReturning400 := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, "Bad Request", 400) }))
mux := New(nopHandler, signerMockReturning400, nopHandler, nopHandler, nopHandler)
pkgt.Get(t, mux, expand("$HOST/priv/doc/abc"))
pkgt.NewRequest(t, mux, expand("$HOST/priv/doc/abc")).Do()
},
/* expectedMetrics = */ `
total_requests_by_code_and_url{code="400",handler="signer"} 1
Expand All @@ -338,7 +338,7 @@ func TestPrometheusMetricRequestsTotal(t *testing.T) {
`,
/* testFunc= */ func() {
mux := New(nopHandler, nopHandler, nopHandler, nopHandler, promhttp.Handler())
pkgt.Get(t, mux, expand(`$HOST/metrics`))
pkgt.NewRequest(t, mux, expand(`$HOST/metrics`)).Do()
},
/* expectedMetrics = */ `
total_requests_by_code_and_url{code="200",handler="metrics"} 1
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestPrometheusMetricRequestsLatency(t *testing.T) {
}
}))
mux := New(mockHandler, mockHandler, mockHandler, mockHandler, mockHandler)
pkgt.Get(t, mux, expand(req.urlTemplate))
pkgt.NewRequest(t, mux, expand(req.urlTemplate)).Do()

}

Expand Down
Loading