MCPcopy Index your code
hub / github.com/coder/coder / tallymanHandler

Function tallymanHandler

enterprise/coderd/usage/publisher_test.go:689–734  ·  view source on GitHub ↗
(t *testing.T, expectDeploymentID string, expectLicenseJWT string, handler func(req usagetypes.TallymanV1IngestRequest) any)

Source from the content-addressed store, hash-verified

687}
688
689func tallymanHandler(t *testing.T, expectDeploymentID string, expectLicenseJWT string, handler func(req usagetypes.TallymanV1IngestRequest) any) http.Handler {
690 t.Helper()
691 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
692 t.Helper()
693 licenseJWT := r.Header.Get(usagetypes.TallymanCoderLicenseKeyHeader)
694 if expectLicenseJWT != "" && !assert.Equal(t, expectLicenseJWT, licenseJWT, "license JWT in request did not match") {
695 rw.WriteHeader(http.StatusUnauthorized)
696 _ = json.NewEncoder(rw).Encode(usagetypes.TallymanV1Response{
697 Message: "license JWT in request did not match",
698 })
699 return
700 }
701
702 deploymentID := r.Header.Get(usagetypes.TallymanCoderDeploymentIDHeader)
703 if expectDeploymentID != "" && !assert.Equal(t, expectDeploymentID, deploymentID, "deployment ID in request did not match") {
704 rw.WriteHeader(http.StatusUnauthorized)
705 _ = json.NewEncoder(rw).Encode(usagetypes.TallymanV1Response{
706 Message: "deployment ID in request did not match",
707 })
708 return
709 }
710
711 var req usagetypes.TallymanV1IngestRequest
712 err := json.NewDecoder(r.Body).Decode(&req)
713 if !assert.NoError(t, err, "could not decode request body") {
714 rw.WriteHeader(http.StatusBadRequest)
715 _ = json.NewEncoder(rw).Encode(usagetypes.TallymanV1Response{
716 Message: "could not decode request body",
717 })
718 return
719 }
720
721 resp := handler(req)
722 switch resp.(type) {
723 case usagetypes.TallymanV1Response:
724 rw.WriteHeader(http.StatusInternalServerError)
725 default:
726 rw.WriteHeader(http.StatusOK)
727 }
728 err = json.NewEncoder(rw).Encode(resp)
729 if !assert.NoError(t, err, "could not encode response body") {
730 rw.WriteHeader(http.StatusInternalServerError)
731 return
732 }
733 })
734}
735
736func tallymanAcceptAllHandler(req usagetypes.TallymanV1IngestRequest) usagetypes.TallymanV1IngestResponse {
737 acceptedEvents := make([]usagetypes.TallymanV1IngestAcceptedEvent, len(req.Events))

Callers 6

TestIntegrationFunction · 0.85
TestPublisherClaimExpiryFunction · 0.85

Calls 6

EncodeMethod · 0.80
HelperMethod · 0.65
GetMethod · 0.65
handlerFunction · 0.50
EqualMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected