Skip to content

Commit

Permalink
test: Test extensions for all exposed auth/authrep/report interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
philipgough committed Feb 25, 2019
1 parent 1d9f0bb commit 219d10e
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package client

import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"path/filepath"
Expand All @@ -12,6 +14,8 @@ import (
"testing"
"time"
"unsafe"

"github.com/3scale/3scale-go-client/fake"
)

var ext_tested bool
Expand Down Expand Up @@ -83,6 +87,92 @@ func TestNewThreeScale(t *testing.T) {
equals(t, threeScaleTwo.backend, DefaultBackend())
}

// Asserts correct behaviour from response when specific extensions are enabled
func TestExtensions(t *testing.T) {
const empty = ""

tokenAuth := TokenAuth{Type: serviceToken, Value: empty}

inputs := []struct {
name string
extensions map[string]string
xmlResponse string
headers http.Header
// function which should error out or complete if no error detected
isOK func(r ApiResponse, e error)
}{
{
name: "Test Hierarchy Extension",
extensions: map[string]string{"hierarchy": "1"},
xmlResponse: fake.GetHierarchyEnabledResponse(),
isOK: func(r ApiResponse, e error) {
if e != nil {
t.Errorf("expected nil error")
}
if len(r.GetHierarchy()) != 1 {
t.Errorf("expected only one parent in hierarchy")
}
if len(r.GetHierarchy()["hits"]) != 3 {
t.Errorf("expected three children for hits metric")
}

},
headers: make(http.Header),
},
{
name: "Test Limit Extension",
extensions: map[string]string{"limit_headers": "1"},
xmlResponse: fake.GetAuthSuccess(),
isOK: func(r ApiResponse, e error) {
if e != nil {
t.Errorf("expected nil error")
}

if limRem := r.GetLimitRemaining(); *limRem != 10 {
t.Errorf("unexpected limit parsing - limit remaining")
}

if limRes := r.GetLimitReset(); *limRes != 500 {
t.Errorf("unexpected limit parsing - limit reset")
}
},
headers: http.Header{limitRemainingHeaderKey: []string{"10"}, limitResetHeaderKey: []string{"500"}},
},
}
for _, input := range inputs {
t.Run(input.name, func(t *testing.T) {
httpClient := NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(input.xmlResponse)),
Header: input.headers,
}
})
c := threeScaleTestClient(httpClient)

r, err := c.AuthRepAppID(tokenAuth, empty, empty, AuthRepParams{}, input.extensions)
input.isOK(r, err)

r, err = c.AuthRepUserKey(tokenAuth, empty, empty, AuthRepParams{}, input.extensions)
input.isOK(r, err)

r, err = c.Authorize(empty, empty, empty, AuthorizeParams{}, input.extensions)
input.isOK(r, err)

r, err = c.AuthorizeKey(empty, empty, empty, AuthorizeKeyParams{}, input.extensions)
input.isOK(r, err)

r, err = c.ReportAppID(tokenAuth, empty, ReportTransactions{}, input.extensions)
input.isOK(r, err)

r, err = c.ReportUserKey(tokenAuth, empty, ReportTransactions{}, input.extensions)
input.isOK(r, err)
})

}

}

// Returns a default client for testing
func threeScaleTestClient(hc *http.Client) *ThreeScaleClient {
client := NewThreeScale(DefaultBackend(), hc)
Expand Down

0 comments on commit 219d10e

Please sign in to comment.