Skip to content

Commit

Permalink
Update tests, changelog, docs
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Le <[email protected]>
  • Loading branch information
CharlieTLe committed Apr 24, 2024
1 parent 12c679b commit d2c6ed9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Order should be `CHANGE`, `FEATURE`, `ENHANCEMENT`, and `BUGFIX`
* [CHANGE] Upgrade to v1.13.2
* [FEATURE] Make rulerAPI Path configurable
* [FEATURE] Add tool to deserialize alertmanager state file
* [BUGFIX] Set tenant id in prom analyse command

## v0.11.1
* [BUGFIX] Fix check for new version
Expand Down
9 changes: 8 additions & 1 deletion pkg/httpmiddleware/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package httpmiddleware

import "net/http"

// TenantIDRoundTripper is a custom implementation of http.RoundTripper.
// It adds a tenant name to the request header before passing it to the next RoundTripper.
type TenantIDRoundTripper struct {
// TenantName is the name of the tenant to be added to the request header.
TenantName string
Next http.RoundTripper
// Next is the next RoundTripper in the chain.
Next http.RoundTripper
}

// RoundTrip adds the tenant name to the request header and then passes the request to the next RoundTripper.
// If TenantName is not set, it simply passes the request to the next RoundTripper.
// It returns the response from the next RoundTripper and any error encountered.
func (r *TenantIDRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if r.TenantName != "" {
req.Header.Set("X-Scope-OrgID", r.TenantName)
Expand Down
36 changes: 36 additions & 0 deletions pkg/httpmiddleware/roundtripper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package httpmiddleware

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

// TestTenantIDRoundTripper_RoundTrip tests the RoundTrip method of TenantIDRoundTripper.
// It creates a new TenantIDRoundTripper with a TenantName and the default http transport as Next.
// It then sends a request and checks if the X-Scope-OrgID header of the request is correctly set to the TenantName.
// It also checks if the status code of the response is OK (200).
func TestTenantIDRoundTripper_RoundTrip(t *testing.T) {
// Create a new TenantIDRoundTripper with a TenantName and the default http transport as Next.
roundTripper := &TenantIDRoundTripper{
TenantName: "test-tenant",
Next: http.DefaultTransport,
}

// Create a new request.
req := httptest.NewRequest("GET", "https://example.com", nil)

// Send the request using the RoundTrip method of the TenantIDRoundTripper.
resp, err := roundTripper.RoundTrip(req)

// Check if there was an error.
require.NoError(t, err)

// Check if the status code of the response is OK (200).
require.Equal(t, http.StatusOK, resp.StatusCode)

// Check if the X-Scope-OrgID header of the request is correctly set to the TenantName.
require.Equal(t, "test-tenant", req.Header.Get("X-Scope-OrgID"))
}

0 comments on commit d2c6ed9

Please sign in to comment.