-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a ProxyChecksService with a method to Create a proxy check. Triggers a request to the Backend API POST /v1/proxy_checks endpoint.
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package clerk | ||
|
||
import "net/http" | ||
|
||
type ProxyChecksService service | ||
|
||
type ProxyCheck struct { | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
DomainID string `json:"domain_id"` | ||
ProxyURL string `json:"proxy_url"` | ||
Successful bool `json:"successful"` | ||
LastRunAt *int64 `json:"last_run_at"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} | ||
|
||
type CreateProxyCheckParams struct { | ||
DomainID string `json:"domain_id"` | ||
ProxyURL string `json:"proxy_url"` | ||
} | ||
|
||
func (s *ProxyChecksService) Create(params CreateProxyCheckParams) (*ProxyCheck, error) { | ||
req, _ := s.client.NewRequest(http.MethodPost, ProxyChecksURL, ¶ms) | ||
var proxyCheck ProxyCheck | ||
_, err := s.client.Do(req, &proxyCheck) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &proxyCheck, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package clerk | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestProxyChecksService_Create(t *testing.T) { | ||
client, mux, _, teardown := setup("token") | ||
defer teardown() | ||
var params CreateProxyCheckParams | ||
payload := `{ | ||
"proxy_url":"https://example.com/__clerk", | ||
"domain_id": "dmn_1mebQggrD3xO5JfuHk7clQ94ysA" | ||
}` | ||
_ = json.Unmarshal([]byte(payload), ¶ms) | ||
|
||
mux.HandleFunc("/proxy_checks", func(w http.ResponseWriter, req *http.Request) { | ||
testHttpMethod(t, req, "POST") | ||
testHeader(t, req, "Authorization", "Bearer token") | ||
fmt.Fprint(w, dummyProxyCheckJSON) | ||
}) | ||
|
||
got, err := client.ProxyChecks().Create(params) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var want ProxyCheck | ||
err = json.Unmarshal([]byte(dummyProxyCheckJSON), &want) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("Response = %v, want %v", got, &want) | ||
} | ||
} | ||
|
||
const dummyProxyCheckJSON = `{ | ||
"object": "proxy_check", | ||
"id": "proxychk_1mebQggrD3xO5JfuHk7clQ94ysA", | ||
"successful": true, | ||
"domain_id": "dmn_1mebQggrD3xO5JfuHk7clQ94ysA", | ||
"proxy_url": "https://example.com/__clerk", | ||
"last_run_at": 1610783813, | ||
"created_at": 1610783813, | ||
"updated_at": 1610783813 | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v6" | ||
"github.com/clerkinc/clerk-sdk-go/clerk" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProxyChecks(t *testing.T) { | ||
client := createClient() | ||
// Create a domain first | ||
domainName := gofakeit.DomainName() | ||
domain, err := client.Domains().Create(clerk.CreateDomainParams{ | ||
Name: domainName, | ||
IsSatellite: true, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Now trigger a proxy check. Most likely a proxy is not configured. | ||
_, err = client.ProxyChecks().Create(clerk.CreateProxyCheckParams{ | ||
DomainID: domain.ID, | ||
ProxyURL: "https://" + domainName + "/__clerk", | ||
}) | ||
assert.Error(t, err) | ||
} |