-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limitador: add client with create and delete
- Loading branch information
Showing
2 changed files
with
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package limitador | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
limitadorv1alpha1 "github.com/3scale/limitador-operator/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type Client struct { | ||
url url.URL | ||
} | ||
|
||
func NewClient(url url.URL) Client { | ||
return Client{url: url} | ||
} | ||
|
||
func (client *Client) CreateLimit(rateLimitSpec *limitadorv1alpha1.RateLimitSpec) error { | ||
jsonLimit, err := json.Marshal(rateLimitSpec) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = http.Post( | ||
fmt.Sprintf("%s/limits", client.url.String()), | ||
"application/json", | ||
bytes.NewBuffer(jsonLimit), | ||
) | ||
|
||
return err | ||
} | ||
|
||
func (client *Client) DeleteLimit(rateLimitSpec *limitadorv1alpha1.RateLimitSpec) error { | ||
jsonLimit, err := json.Marshal(rateLimitSpec) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequest( | ||
"DELETE", | ||
fmt.Sprintf("%s/limits", client.url.String()), | ||
bytes.NewBuffer(jsonLimit), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
|
||
httpClient := &http.Client{} | ||
_, err = httpClient.Do(req) | ||
|
||
return err | ||
} |
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,70 @@ | ||
package limitador | ||
|
||
import ( | ||
limitadorv1alpha1 "github.com/3scale/limitador-operator/api/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestCreateLimit(t *testing.T) { | ||
rateLimitSpec := exampleRateLimitSpec() | ||
rateLimitSpecJson, err := json.Marshal(rateLimitSpec) | ||
assert.NoError(t, err) | ||
|
||
testServerUrl, closeServerFunc := newTestServer(t, "POST", "/limits", string(rateLimitSpecJson)) | ||
defer closeServerFunc() | ||
|
||
limitadorClient := NewClient(*testServerUrl) | ||
err = limitadorClient.CreateLimit(rateLimitSpec) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestDeleteLimit(t *testing.T) { | ||
rateLimitSpec := exampleRateLimitSpec() | ||
rateLimitSpecJson, err := json.Marshal(rateLimitSpec) | ||
assert.NoError(t, err) | ||
|
||
testServerUrl, closeServerFunc := newTestServer(t, "DELETE", "/limits", string(rateLimitSpecJson)) | ||
defer closeServerFunc() | ||
|
||
limitadorClient := NewClient(*testServerUrl) | ||
err = limitadorClient.DeleteLimit(rateLimitSpec) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
// Creates a test server that checks the given HTTP request fields | ||
func newTestServer(t *testing.T, expectedMethod string, expectedPath string, expectedBody string) (*url.URL, func()) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, expectedMethod, r.Method) | ||
|
||
assert.Equal(t, expectedPath, r.URL.Path) | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
assert.NoError(t, err) | ||
err = r.Body.Close() | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedBody, string(body)) | ||
})) | ||
|
||
serverUrl, err := url.Parse(testServer.URL) | ||
assert.Nil(t, err) | ||
|
||
return serverUrl, testServer.Close | ||
} | ||
|
||
func exampleRateLimitSpec() *limitadorv1alpha1.RateLimitSpec { | ||
return &limitadorv1alpha1.RateLimitSpec{ | ||
Conditions: []string{"req.method == GET"}, | ||
MaxValue: 10, | ||
Namespace: "test-namespace", | ||
Seconds: 60, | ||
Variables: []string{"user_id"}, | ||
} | ||
} |