Skip to content

Commit

Permalink
proxy: test template caching/lookup
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hutchinson <[email protected]>
  • Loading branch information
Jason Hutchinson authored and arekkas committed Aug 16, 2018
1 parent 188748d commit ab8a402
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 4 additions & 4 deletions proxy/credentials_issuer_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
type CredentialsHeadersConfig map[string]string

type CredentialsHeaders struct {
rulesCache *template.Template
RulesCache *template.Template
}

func NewCredentialsIssuerHeaders() *CredentialsHeaders {
return &CredentialsHeaders{
rulesCache: template.New("rules").
RulesCache: template.New("rules").
Option("missingkey=zero").
Funcs(template.FuncMap{
"print": func(i interface{}) string {
Expand Down Expand Up @@ -52,9 +52,9 @@ func (a *CredentialsHeaders) Issue(r *http.Request, session *AuthenticationSessi
var err error

templateId := fmt.Sprintf("%s:%s", rl.ID, hdr)
tmpl = a.rulesCache.Lookup(templateId)
tmpl = a.RulesCache.Lookup(templateId)
if tmpl == nil {
tmpl, err = a.rulesCache.New(templateId).Parse(templateString)
tmpl, err = a.RulesCache.New(templateId).Parse(templateString)
if err != nil {
return errors.Wrapf(err, `error parsing header template "%s" in rule "%s"`, templateString, rl.ID)
}
Expand Down
29 changes: 29 additions & 0 deletions proxy/credentials_issuer_headers_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package proxy

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"testing"
"text/template"

"github.com/ory/oathkeeper/rule"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -106,4 +109,30 @@ func TestCredentialsIssuerHeaders(t *testing.T) {
assert.Equal(t, specs.Match, specs.Request.Header)
})
}

t.Run("Caching", func(t *testing.T) {
for _, specs := range testMap {
issuer := NewCredentialsIssuerHeaders()

overrideHeaders := http.Header{}

cache := template.New("rules")

var cfg CredentialsHeadersConfig
d := json.NewDecoder(bytes.NewBuffer(specs.Config))
require.NoError(t, d.Decode(&cfg))

for hdr, _ := range cfg {
templateId := fmt.Sprintf("%s:%s", specs.Rule.ID, hdr)
cache.New(templateId).Parse("override")
overrideHeaders.Add(hdr, "override")
}

issuer.RulesCache = cache

require.NoError(t, issuer.Issue(specs.Request, specs.Session, specs.Config, specs.Rule))

assert.Equal(t, overrideHeaders, specs.Request.Header)
}
})
}

0 comments on commit ab8a402

Please sign in to comment.