Skip to content

Commit

Permalink
feat: add True-Client-IP as a custom header to be passed along BAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasLopes7 committed May 29, 2024
1 parent 39bb339 commit 6b0abf0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion clerk.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ type Params interface {
// CustomRequestHeaders contains predefined values that can be
// used as custom headers in Backend API requests.
type CustomRequestHeaders struct {
Application string
Application string
TrueClientIP string
}

// Apply the custom headers to the HTTP request.
Expand All @@ -169,6 +170,7 @@ func (h *CustomRequestHeaders) apply(req *http.Request) {
return
}
req.Header.Add("X-Clerk-Application", h.Application)
req.Header.Add("True-Client-IP", h.TrueClientIP)
}

// BackendConfig is used to configure a new Clerk Backend.
Expand Down
6 changes: 5 additions & 1 deletion clerk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ func TestBackendCall_RequestHeaders(t *testing.T) {
path := "/resources"
secretKey := "sk_test_123"
customHeaders := CustomRequestHeaders{
Application: "custom-application",
Application: "custom-application",
TrueClientIP: "127.0.0.1",
UserAgent: "custom-user-agent",

Check failure on line 180 in clerk_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

unknown field 'UserAgent' in struct literal of type CustomRequestHeaders

Check failure on line 180 in clerk_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.20

unknown field UserAgent in struct literal of type CustomRequestHeaders

Check failure on line 180 in clerk_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.21

unknown field UserAgent in struct literal of type CustomRequestHeaders
}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -193,6 +195,8 @@ func TestBackendCall_RequestHeaders(t *testing.T) {
assert.Equal(t, fmt.Sprintf("go/%s", sdkVersion), r.Header.Get("X-Clerk-SDK"))
// Custom headers are added correctly.
assert.Equal(t, customHeaders.Application, r.Header.Get("X-Clerk-Application"))
assert.Equal(t, customHeaders.TrueClientIP, r.Header.Get("True-Client-IP"))
assert.Equal(t, customHeaders.UserAgent, r.Header.Get("User-Agent"))

Check failure on line 199 in clerk_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

customHeaders.UserAgent undefined (type CustomRequestHeaders has no field or method UserAgent)

Check failure on line 199 in clerk_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.20

customHeaders.UserAgent undefined (type CustomRequestHeaders has no field or method UserAgent)

Check failure on line 199 in clerk_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.21

customHeaders.UserAgent undefined (type CustomRequestHeaders has no field or method UserAgent)

_, err := w.Write([]byte(`{}`))
require.NoError(t, err)
Expand Down

0 comments on commit 6b0abf0

Please sign in to comment.