Skip to content

Commit

Permalink
limitador: add client with create and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Jan 14, 2021
1 parent 1ad2ad6 commit ed0387d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/limitador/client.go
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
}
70 changes: 70 additions & 0 deletions pkg/limitador/client_test.go
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"},
}
}

0 comments on commit ed0387d

Please sign in to comment.